简体   繁体   English

数据集导出到excel并在excel中写多个工作表

[英]Dataset export to excel and write multiple sheets in excel

I have requirement in my project that data from multiple database tables should be exported as per sheet wise in excel file say if dataset contains 4 tables then table 0 should be country, table 1 should be state and so on...So please suggest me anything that works with exporting multiple datasheets to excel file at a time and excel should be generated at runtime. 我在我的项目中要求从多个数据库表中的数据应该按照excel文件中的表格导出,如果数据集包含4个表,则表0应该是国家,表1应该是州,依此类推...所以请建议我一次可将多个数据表导出到excel文件并且excel的所有操作都应在运行时生成。 I am using C# ASP.NET 我正在使用C#ASP.NET

I would just add new workbook to the workbooks object for each table and then iterate through the tables writing each to the workbook. 我只是将新工作簿添加到每个表的工作簿对象中,然后遍历表,将每个表写入工作簿。 3 workbooks are created by default so you will need to delete extra workbooks if you have more than three tables in your dataset. 默认情况下会创建3个工作簿,因此,如果数据集中有三个以上的表,则需要删除其他工作簿。

var ds = [Your dataset here];
Worksheet objWorkSheet1 = null;
Application objExcel = new Application {Visible = false};
Workbooks objWorkbooks = objExcel.Workbooks;
Workbook objWorkbook = objWorkbooks.Add(Missing.Value);
Sheets objSheets = objWorkbook.Worksheets;

if (ds.Tables.Count > objSheets.Count)
{
    // add extra sheets
    for (var i = objSheets.Count; i < ds.Tables.Count; i++)
    {
        var objWorkSheet2 = (Worksheet)objSheets[i];
        objWorkSheet1 = (Worksheet)objSheets.Add(Missing.Value, objWorkSheet2, 1, XlSheetType.xlWorksheet);
        Marshal.ReleaseComObject(objWorkSheet2);
        Marshal.ReleaseComObject(objWorkSheet1);
        objWorkSheet1 = null;
    }
}
while (ds.Tables.Count < objSheets.Count)
{
    // remove unnecessary sheets
    var objWorkSheet2 = (Worksheet)objSheets[ds.Tables.Count];
    objWorkSheet2.Delete();
    Marshal.ReleaseComObject(objWorkSheet2);
}
Range objCells;
Range myCell;
for (var t = 0; t < ds.Tables.Count; t++)
{// for each table in the dataset fill in the sheet 
    var iCurrentRow = 1;
    var dt = ds.Tables[t];
    objWorkSheet1 = (Worksheet)(objSheets[t + 1]);
    objCells = objWorkSheet1.Cells;

    //Start writing the table to the worksheet
    // Get the sheet and write the headers
    for (var h = 0; h < dt.Columns.Count; h++)
    {
        myCell = (Range)objCells[iCurrentRow, h + 1];
        myCell.Value2 = dt.Columns[h].ColumnName;
        Marshal.ReleaseComObject(myCell);
    }
    iCurrentRow++;
    // write the data rows
    for (var r = 0; r < dt.Rows.Count; r++)
    {
        // Get the sheet and write the headers
        for (var c = 0; c < dt.Columns.Count; c++)
        {
            myCell = (Range)objCells[r + iCurrentRow, c + 1];
            myCell.Value2 = dt.Rows[r][c].ToString();
            Marshal.ReleaseComObject(myCell);
        }
    }
    Marshal.ReleaseComObject(objCells);
    objCells = null;
    Marshal.ReleaseComObject(objWorkSheet1);
    objWorkSheet1 = null;
}
//End writing the data table to the sheet

// select the first cell in the first sheet
objWorkSheet1 = (Worksheet)(objSheets[1]);
objCells = objWorkSheet1.Cells;
myCell = (Range)objCells[1, 1];
Marshal.ReleaseComObject(myCell);
myCell = null;
Marshal.ReleaseComObject(objCells);
objCells = null;
Marshal.ReleaseComObject(objWorkSheet1);
objWorkSheet1 = null;

// save the file to the new location with new name.
objWorkbook.Close(true, [your filename here to save to], Missing.Value);

objWorkbooks.Close();
objExcel.Quit();

An alternate way to do this would be to use reporting services. 另一种执行此操作的方法是使用报告服务。 Each page of the report will be rendered to its own sheet in excel. 报告的每一页都将在excel中呈现到自己的工作表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM