一个很好的用C#导出数据到Excel模板的方法

        /// <summary>
        /// 导数据到Excel模板
        /// </summary>
        /// <param name="tab">要输出内容的Table</param>
        /// <param name="fileName">生成后的路径带文件名</param>
        /// <param name="filePath">模板路径带文件名</param>
        /// <param name="lineNumber">表头占了几行</param>
        /// <param name="colNumber">表头占了几列</param>
        /// <param name="tableItem">Excel的第几个工作薄</param>
        public static void ToExcelTemplate(DataTable tab, string fileName, string filePath, int lineNumber,int colNumber, int tableItem)
        {
            //需要添加 Microsoft.Office.Interop.Excel引用 
            Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();

            if (app == null)
            {
                HttpContext.Current.Response.Write("服务器上缺少Excel组件,需要安装Office软件。");
                return;
            }
            app.Visible = false;
            app.UserControl = true;
            Microsoft.Office.Interop.Excel.Workbooks workbooks = app.Workbooks;
            Microsoft.Office.Interop.Excel._Workbook workbook = workbooks.Add(HttpContext.Current.Server.MapPath(filePath)); //加载模板
            Microsoft.Office.Interop.Excel.Sheets sheets = workbook.Sheets;
            Microsoft.Office.Interop.Excel._Worksheet worksheet = (Microsoft.Office.Interop.Excel._Worksheet)sheets.get_Item(tableItem); //第几个工作薄。
            if (worksheet == null)
                return;  //工作薄中没有工作表.
            //1、获取数据。
            //DataSet dt = myDB.Select(sql, null);  //--------------------------------------------------根据实际需要修改--------------!!!!!
            int rowCount = tab.Rows.Count;//总行数
            int col = tab.Columns.Count;//总列数
            if (rowCount < 1)
                return; //没有数据,不需要导出。
            //2、写入数据,Excel索引从1开始。
            for (int i = 1; i <= rowCount; i++)
            {
                int row_ = lineNumber + i;  //Excel模板上表头和标题行占了2行,根据实际模板需要修改;
                int dt_row = i - 1; //dataTable的行是从0开始的。 
                colNumber = colNumber + 1;
                //循环每一列
                for (int b = 0; b < col; b++)
                {
                    worksheet.Cells[row_, b + colNumber] = tab.Rows[i - 1][b];
                }
            }
            //调整Excel的样式。
            //Microsoft.Office.Interop.Excel.Range rg = worksheet.Cells.get_Range("A3", worksheet.Cells[rowCount + 2, 8]);
            //rg.Borders.LineStyle = 1; //单元格加边框。
            worksheet.Columns.AutoFit(); //自动调整列宽。
            //3、保存生成的Excel文件。
            //Missing 在System.Reflection命名空间下。
            string savaPath = fileName;
            //如果已经有了就删除原来的
            if (File.Exists(HttpContext.Current.Server.MapPath(savaPath)))
            {
                File.Delete(HttpContext.Current.Server.MapPath(savaPath));
            }
            workbook.SaveAs(HttpContext.Current.Server.MapPath(savaPath), Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);



            //4、按顺序释放资源。
            NAR(worksheet);
            NAR(sheets);
            NAR(workbook);
            NAR(workbooks);
            //自动下载
            app.Quit();
            NAR(app);
        }
        private static void NAR(object o)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
            }
            catch
            { }
            finally
            {
                o = null;
            }
        }

  

  要导几个包:

using System.Data;
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;

  

转载于:https://www.cnblogs.com/qiywtc/p/3654649.html

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值