2014-08-27 87 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"); 
相關問題