c# 导出table中的数据到模板excel中

/// <summary>
    /// 把table中的数据导出到excel中去
    /// </summary>
    /// <param name="dt">要导入的table</param>
    /// <param name="maxcount">模板excel中,一个sheet要显示的行数,如果数据多一个sheet中的最大值,会自动生成新的sheet</param>
    private void ExportExcel(System .Data .DataTable dt,int maxcount)
    {
        Random ran = new Random();
        string fileautoname = Server.MapPath("~/") + "Files//" + DateTime.Now.ToString("yyyyMMddhhmmss") + ran.Next(100, 999) + ".xls";//给新文件命名
        string filepath = Server.MapPath("~/") + "EXCEL_Template//Excel//模板.xls";//模板文件路径
        object missing = Type.Missing;
        Microsoft.Office.Interop.Excel.Application application = new Application();
        Workbook workbook = application.Workbooks.Open(filepath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
        Worksheet worksheet;
        worksheet = (Worksheet)workbook.Sheets.get_Item(1);       
        DataView dv = dt.DefaultView;
        dv.Sort = "id asc"; //table 中的数据按id升序排列
        System.Data.DataTable table = dv.ToTable();
       
        int sheetcount = GetSheetCount(table.Rows.Count, maxcount);
        for (int count = 1; count < sheetcount; count++)
        {
            ((Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(count)).Copy(missing, workbook.Worksheets[count]);
        }
        List<object[,]> list = new List<object[,]>();
        object[,] ret;
        for (int count = 0; count < sheetcount; count++)
        {
            if (count == sheetcount - 1)
            {
                ret = new object[table.Rows.Count - count * maxcount, table.Columns.Count - 2];
                for (int i = 0; i < table.Rows.Count - count * maxcount; i++)
                {
                    for (int j = 0; j < table.Columns.Count - 2; j++)
                    {
                        ret[i, j] = table.Rows[count * maxcount + i][j];
                    }
                }
                list.Add(ret);
            }
            else
            {
                ret = new object[maxcount, table.Columns.Count - 2];
                for (int i = 0; i < maxcount; i++)
                {
                    for (int j = 0; j < table.Columns.Count - 2; j++)
                    {
                        ret[i, j] = table.Rows[i + count * maxcount][j];
                    }
                }
                list.Add(ret);
            }
        }

        object[,] obj;
        for (int p = 0; p < list.Count; p++)
        {
            worksheet = (Worksheet)workbook.Sheets.get_Item(p + 1);
            obj = list[p];
            string cn = "L" + (obj.GetLength(0) + 2).ToString(); //设置填充区域
            worksheet.get_Range("A3", cn).FormulaR1C1 = obj;
        }
        workbook.SaveAs(fileautoname, missing, missing, missing, missing, missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing);
        workbook.Close(missing, missing, missing);
        application.Quit();
        workbook = null;
    }
    /// <summary>
    ///  获取WorkSheet数量
    /// </summary>
    /// <param name="rowCount">记录总行数</param>
    /// <param name="rows">每WorkSheet行数</param>
    /// <returns></returns>
    private int GetSheetCount(int rowCount,int rows)
    {
        int n = rowCount % rows;        //余数

        if(n == 0)
            return rowCount / rows;
        else
            return Convert.ToInt32(rowCount / rows) + 1;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值