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

Dataset export to excel and write multiple sheets in excel

我的项目要求来自多个数据库 table 的数据应该按照 sheet 明智地导出到 excel 文件中,如果数据集包含 4 table s 然后 table 0 应该是国家/地区,table 1 应该是州等等...所以请建议我任何可以将多个数据 sheets 导出到 excel 的建议一次生成一个文件,excel 应该在运行时生成。我正在使用 C# ASP.NET

我只是将新工作簿添加到每个 table 的工作簿对象,然后遍历 table 将每个工作簿写入工作簿。默认情况下会创建 3 个工作簿,因此如果您的数据集中有超过三个 table,您将需要删除额外的工作簿。

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();

另一种方法是使用报告服务。报告的每一页都将在 excel.

中呈现给它自己的 sheet