2014-08-27 55 views
1

我有不同类别的信息的几个不同的字典和我需要将它们输出所有与多个电子表格XLS或CSV文件。目前,我要下载的每个excel文件指定日期范围分别,然后复制并粘贴在一起,使他们在同一个文件的不同表。有什么方法可以将它们全部下载到一个文档中?目前,我用下面的代码输出他们的文件:转移到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刚刚创造了个人文件中的文本。

回答

0

如果你是开放地学习一个新的图书馆,我强烈建议EPPlus

我在这里做一些假设,因为你没有张贴很多代码转换,但是使用的一个例子可能是这样的:

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

有,你可以使用一些不同的选择。

  • ADO.NET Excel驱动程序 - 使用此API,您可以使用SQL样式语法将数据填充到Excel文档中。工作簿中的每个工作表是一个表,工作表中的每个列标题是表等

这里一列使用ADO.NET的导出到Excel代码项目的文章: http://www.codeproject.com/Articles/567155/Work-with-MS-Excel-and-ADO-NET

ADO.NET的方法是安全的在多用户的网络应用环境中使用。

  • 使用的OpenXML导出数据 OpenXML的是不同类型的文档和Excel的更高版本(使用的.xlsx的那些,.XLSM等,而不是仅仅的.xls)架构定义使用这种格式为文件。 OpenXML模式非常庞大而且有点麻烦,但是你可以用它做很多事情。

下面是使用的OpenXML将数据导出到Excel中的代码项目文章: http://www.codeproject.com/Articles/692121/Csharp-Export-data-to-Excel-using-OpenXML-librarie

的OpenXML的方法是安全的在多用户的网络应用环境中使用。

  • 第三种方法是使用COM自动化,它与以编程方式运行Excel桌面应用程序的实例并使用COM来控制该实例的操作相同。

下面是对这样一个话题的文章: http://support.microsoft.com/kb/302084

注意,这第三种方法(办公自动化)是在一个多用户,网络应用环境的安全。即它不应该在服务器上使用,只能从独立桌面应用程序使用。

0

感谢您的想法!我最终能找出以下

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