zoukankan      html  css  js  c++  java
  • Datatable 直接生成 Excel 文件到本地磁盘中

      protected void DatatableExcelC(System.Data.DataTable dt)
        {
            Microsoft.Office.Interop.Excel.Application excelkccx = new Microsoft.Office.Interop.Excel.ApplicationClass();
            Microsoft.Office.Interop.Excel._Workbook wb;
            Microsoft.Office.Interop.Excel._Worksheet ws = null;
            wb = excelkccx.Workbooks.Add(true);
            string tbname = "FileName";
           
            if (ws == null)
            {
                ws = (_Worksheet)wb.Worksheets.Add(Type.Missing, Type.Missing, 1, Type.Missing);
            }
            else
            {
                ws = (_Worksheet)wb.Worksheets.Add(Type.Missing, ws, 1, Type.Missing);
            }
            int row = 2;
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                excelkccx.Cells[1, i + 1] = dt.Columns[i].ColumnName.ToString();
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    excelkccx.Cells[row, j + 1] = dt.Rows[i][j].ToString();
                }
                row++;
            }
            wb.SaveCopyAs("C:\\" + tbname + ".xls");
            wb.Close(falsenullnull);
            excelkccx.Quit();
            wb = null;
            ws = null;
            excelkccx = null;

        }
  • 相关阅读:
    Asp.Net细节性问题技巧精萃
    存储过程(Stored Procedure)及应用
    合并datagrid中内容相同的单元格
    .net 2.0 下发送邮件的方式
    ADO.NET2.0的十大新特性
    sql server 中各个系统表的作用
    DataGrid一些文章的索引,方便查找
    ASP.NET中 WebControls 命名规则
    SQL Server应用程序中的高级SQL注入[转]
    数据操作例子
  • 原文地址:https://www.cnblogs.com/hishanghai/p/2686081.html
Copyright © 2011-2022 走看看