如何在C#中导出Excel表

来源:互联网 发布:eclipse node插件 编辑:程序博客网 时间:2024/06/06 09:48

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Data.OleDb; //操作EXECL表要用的命名空间
using Excel;   // Excel 下的名称空间
using System.Reflection;    //反射名称空间
using System.IO;

try
            {
                //首先获得当前程序执行的目录赋给变量CurrentPatch
                string currentPatch = Directory.GetCurrentDirectory();
                //获得当前程序执行的目录父目录
                string patch = Directory.GetParent(Directory.GetCurrentDirectory()).ToString();
                //再得到父目录的父目录
                patch = Directory.GetParent(patch).ToString();
                //再得到父目录的父目录的父目录<最后指定文件夹为项目文件夹下的EXCEL文件夹>
                patch = Directory.GetParent(patch).ToString() + "//ShopManeger//bin//Debug//EXCEL//";
                //用时间来设置命名文件名,以防止出现重得文件名
                string filename = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() +
                    DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();

                //把DataSet 中的数据导出Excel 文件中
                //创建一个Excel 应用程序类,是通过一个接口来创建的
                _Application IExcel = new ApplicationClass();
                Workbooks ibooks = IExcel.Workbooks; //获得工作薄的集合
                Workbook book = ibooks.Add(Missing.Value); //添加一个工作薄采用缺省参数
                Worksheet sheet = (Worksheet)book.Worksheets[1]; //获得第一个SHEET页
                //在EXECL第一行写入列头
                //[]里面第一个数代表行数,第二个数代表列数
                sheet.Cells[1, 1] = "商品编号";
                sheet.Cells[1, 2] = "商品类型编号";
                sheet.Cells[1, 3] = "商品名称";
                sheet.Cells[1, 4] = "商品现价";
                sheet.Cells[1, 5] = "商品原价";
                sheet.Cells[1, 6] = "商品库存量";
                string sql = "select * from shop";
                DataSet set = DataAccess.GetDataSet2(sql);
                int count = 1; //代表填充数据从第二行开始<第一行已经做为列头老>
                foreach (DataRow dr in set.Tables["shop"].Rows)
                {
                    sheet.Cells[count + 1, 1] = dr["ShopID"].ToString(); //从第二行开始写
                    sheet.Cells[count + 1, 2] = dr["ShopTypeID"].ToString();
                    sheet.Cells[count + 1, 3] = dr["ShopName"].ToString();
                    sheet.Cells[count + 1, 4] = dr["NowPrice"].ToString();
                    sheet.Cells[count + 1, 5] = dr["OldPrice"].ToString();
                    sheet.Cells[count + 1, 6] = dr["Quantity"].ToString();

                    count += 1; // 计数器累加<行数累加>
                }
               // E:/APPC软件开发/C#测试程序/winForms/彭伟C#项目/shop/ShopManeger/ShopManeger/bin/Debug/EXCEL
               
                //保存Excel,第一个参数是保存的Excel文件,带路径。后面的参数都采用反射的缺省值
                //Missing.Value ,要引用using System.Reflection;
                sheet.SaveAs(patch + filename + ".xls", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
               
                ibooks.Close();// 关闭工作薄,即关闭Excel
                IExcel.Quit(); //退出IExcel对象
                MessageBox.Show("导出商品数据成功!", "系统信息");
                Directory.SetCurrentDirectory(currentPatch); //设置程序的当前执行路径
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

原创粉丝点击