使用多张工作表从c#转换到excel

使用多张工作表从c#转换到excel,c#,csv,dictionary,export,xls,C#,Csv,Dictionary,Export,Xls,我有一些不同的字典,其中包含不同类别的信息,我需要将它们全部输出到包含多个电子表格的xls或csv文件中。目前,我必须分别下载特定日期范围的每个excel文件,然后将它们复制并粘贴在一起,以便它们位于同一文件的不同页面上。有没有办法在一个文档中下载所有这些内容?目前,我使用以下代码输出他们的文件: writeCsvToStream( organize.ToDictionary(k => k.Key, v => v.Value as IacTransmittal), wri

我有一些不同的字典,其中包含不同类别的信息,我需要将它们全部输出到包含多个电子表格的xls或csv文件中。目前,我必须分别下载特定日期范围的每个excel文件,然后将它们复制并粘贴在一起,以便它们位于同一文件的不同页面上。有没有办法在一个文档中下载所有这些内容?目前,我使用以下代码输出他们的文件:

 writeCsvToStream(
     organize.ToDictionary(k => k.Key, v => v.Value as IacTransmittal), writer
 );
 ms.Seek(0, SeekOrigin.Begin);
 Response.Clear();
 Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
 Response.AddHeader("Content-Length", ms.Length.ToString());
 Response.ContentType = "application/octet-stream";
 ms.CopyTo(Response.OutputStream);

 Response.End();

其中
writeCsvToStream
只是为单个文件创建文本。

您可以使用一些不同的选项

  • ADO.NET Excel驱动程序-使用此API,您可以使用SQL样式语法将数据填充到Excel文档中。工作簿中的每个工作表都是一个表,工作表中的每个列标题都是该表中的一列,以此类推
下面是一篇关于使用ADO.NET导出到Excel的代码项目文章:

在多用户web应用程序环境中使用ADO.NET方法是安全的

  • 使用OpenXML导出数据 OpenXML是不同类型文档的模式定义,Excel的更高版本(使用.xlsx、.xlsm等而不仅仅是.xls的版本)对文档使用此格式。OpenXML模式非常庞大,有些麻烦,但是您可以用它做几乎任何事情
下面是一篇关于使用OpenXML将数据导出到Excel的代码项目文章:

OpenXML方法在多用户web应用程序环境中使用是安全的

  • 第三种方法是使用COM自动化,这与以编程方式运行Excel桌面应用程序实例并使用COM控制该实例的操作相同
以下是一篇关于该主题的文章:


请注意,第三种方法(办公自动化)在多用户web应用程序环境中并不安全。也就是说,它不应该在服务器上使用,只能从独立的桌面应用程序中使用。

如果您愿意学习新的库,我强烈建议

我在这里做了一些假设,因为您没有发布太多要翻译的代码,但使用示例可能如下所示:

using OfficeOpenXml;
using OfficeOpenXml.Style;

public static void WriteXlsOutput(Dictionary<string, IacTransmittal> collection) //accepting one dictionary as a parameter
{
    using (FileStream outFile = new FileStream("Example.xlsx", FileMode.Create))
    {
        using (ExcelPackage ePackage = new ExcelPackage(outFile))
        {
            //group the collection by date property on your class
            foreach (IGrouping<DateTime, IacTransmittal> collectionByDate in collection
                .OrderBy(i => i.Value.Date.Date)
                .GroupBy(i => i.Value.Date.Date)) //assuming the property is named Date, using Date property of DateTIme so we only create new worksheets for individual days
            {
                ExcelWorksheet eWorksheet = ePackage.Workbook.Worksheets.Add(collectionByDate.Key.Date.ToString("yyyyMMdd")); //add a new worksheet for each unique day

                Type iacType = typeof(IacTransmittal);
                PropertyInfo[] iacProperties = iacType.GetProperties();
                int colCount = iacProperties.Count(); //number of properties determines how many columns we need
                //set column headers based on properties on your class
                for (int col = 1; col <= colCount; col++)
                {
                    eWorksheet.Cells[1, col].Value = iacProperties[col - 1].Name ; //assign the value of the cell to the name of the property
                }

                int rowCounter = 2;

                foreach (IacTransmittal iacInfo in collectionByDate) //iterate over each instance of this class in this igrouping
                {
                    int interiorColCount = 1;
                    foreach (PropertyInfo iacProp in iacProperties) //iterate over properties on the class
                    {
                        eWorksheet.Cells[rowCounter, interiorColCount].Value = iacProp.GetValue(iacInfo, null); //assign cell values by getting the value of each property in the class
                        interiorColCount++;
                    }
                    rowCounter++;
                }
            }
            ePackage.Save();
        }
    }
}
使用OfficeOpenXml;
使用OfficeOpenXml.Style;
public static void WriteXlsOutput(字典集合)//接受一个字典作为参数
{
使用(filestreamoutfile=newfilestream(“Example.xlsx”,FileMode.Create))
{
使用(ExcelPackage ePackage=新的ExcelPackage(outFile))
{
//在类上按日期对集合属性进行分组
foreach(在集合中按日期分组集合
.OrderBy(i=>i.Value.Date.Date)
.GroupBy(i=>i.Value.Date.Date))//假设属性名为Date,则使用DateTIme的Date属性,因此我们只为单个日期创建新工作表
{
Excel工作表eWorksheet=ePackage.工作簿.Worksheets.Add(collectionByDate.Key.Date.ToString(“yyyyMMdd”);//为每个唯一的日期添加新工作表
类型iacType=类型(IACTransmit);
PropertyInfo[]iacProperties=iacType.GetProperties();
int colCount=iacProperties.Count();//属性的数量决定我们需要多少列
//基于类的属性设置列标题

因为(int col=1;col谢谢你的想法!我最终能够弄明白以下几点

using Excel = Microsoft.Office.Interop.Excel;


Excel.Application ExcelApp = new Excel.Application();
Excel.Workbook ExcelWorkBook = null;
Excel.Worksheet ExcelWorkSheet = null;

ExcelApp.Visible = true;
ExcelWorkBook = ExcelApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);

List<string> SheetNames = new List<string>() 
    { "Sheet1", "Sheet2", "Sheet3", "Sheet4", "Sheet5", "Sheet6", "Sheet7"};

string [] headers = new string [] 
    { "Field 1", "Field 2", "Field 3", "Field 4", "Field 5" };

for (int i = 0; i < SheetNames.Count; i++)
    ExcelWorkBook.Worksheets.Add(); //Adding New sheet in Excel Workbook

for (int k = 0; k < SheetNames.Count; k++ )
{
    int r = 1; // Initialize Excel Row Start Position  = 1

    ExcelWorkSheet = ExcelWorkBook.Worksheets[k + 1];
    //Writing Columns Name in Excel Sheet
    for (int col = 1; col < headers.Length + 1; col++)
        ExcelWorkSheet.Cells[r, col] = headers[col - 1];
    r++;
    switch (k)
    {
        case 0:
            foreach (var kvp in Sheet1)
            {
                ExcelWorkSheet.Cells[r, 1] = kvp.Value.Field1;
                ExcelWorkSheet.Cells[r, 2] = kvp.Value.Field2;
                ExcelWorkSheet.Cells[r, 3] = kvp.Value.Field3;
                ExcelWorkSheet.Cells[r, 4] = kvp.Value.Field4;
                ExcelWorkSheet.Cells[r, 5] = kvp.Value.Field5;
                r++;
            }
            break;

    }
    ExcelWorkSheet.Name = SheetNames[k];//Renaming the ExcelSheets
}

//Activate the first worksheet by default.
((Excel.Worksheet)ExcelApp.ActiveWorkbook.Sheets[1]).Activate();

//Save As the excel file.
ExcelApp.ActiveWorkbook.SaveCopyAs(@"out_My_Book1.xls");
使用Excel=Microsoft.Office.Interop.Excel;
Excel.Application ExcelApp=新的Excel.Application();
Excel.Workbook ExcelWorkBook=null;
Excel.Worksheet ExcelWorkSheet=null;
ExcelApp.Visible=true;
ExcelWorkBook=ExcelApp.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
列表SheetNames=新列表()
{“第1张”、“第2张”、“第3张”、“第4张”、“第5张”、“第6张”、“第7张”};
字符串[]头=新字符串[]
{“字段1”、“字段2”、“字段3”、“字段4”、“字段5”};
对于(int i=0;i