2016-03-04 59 views
0

我想將總結值導出到excel文件。我可以很容易地用CSV文件做到這一點,但用Excel做這件事對我來說很困難,因爲我一次只能向單元格注入一個值。我必須將其導出爲xlsx擴展名。c#:試圖將字典鍵和值注入Excel文件

我在這裏要做的是將第一個參數設置爲行號,將第二個參數設置爲列號,將第三個參數設置爲要在其中注入的值。

  int iColumn = 1; 
      excel.WriteToSheet(int value of row, iColumn, key); 
      excel.WriteToSheet(int value of row, iColumn + 1, iSumOpen); 
      excel.WriteToSheet(int value of row, iColumn + 2, iSumBuy); 
      excel.WriteToSheet(int value of row, iColumn + 3, iSumSell); 
      excel.WriteToSheet(int value of row, iColumn + 4, iSumSettleMM); 

這裏是整個方法。我試圖將行值更改爲key.count,使用for循環並使其成爲key.element(i),並且沒有找到合理的方法來執行此操作,部分原因是我對字典和方法沒有經驗的。

public void printToExcel(string vOutputPath) 
    { 
     string sExcelPath = @"C:\Users\jhochbau\Desktop\Test.xlsx"; 
     ExcelHelper excel = new ExcelHelper(sExcelPath); 
     excel.OpenWorkbook(); 

     foreach (KeyValuePair<string, List<DataRecord>> kvp in vSummaryResults) 
     { 
      string key = kvp.Key; //assigns key 

      List<DataRecord> list = kvp.Value; //assigns value 


      int iSumOpen = 0; 
      int iSumBuy = 0; 
      int iSumSell = 0; 
      double iSumSettleMM = 0; 

      foreach (DataRecord rec in list) 
      { 
       if (vSummaryResults.ContainsKey(key)) 
       { 

        iSumOpen += rec.open; 
        iSumBuy += rec.buy; 
        iSumSell += rec.sell; 
        iSumSettleMM += rec.settleMM; 

       } 

       else 
       { 
        vSummaryResults.Add(key, list); 

       } 

      } 
      //WriteToSheet params: int row, int column, and value injected 
      //need to create some counters here to move the rows and columns? 


       int iColumn = 1; 
       excel.WriteToSheet(int value of row, iColumn, key); 
       excel.WriteToSheet(int value of row, iColumn + 1, iSumOpen); 
       excel.WriteToSheet(int value of row, iColumn + 2, iSumBuy); 
       excel.WriteToSheet(int value of row, iColumn + 3, iSumSell); 
       excel.WriteToSheet(int value of row, iColumn + 4, iSumSettleMM); 


     } 
    } 

而且輸出應該是這樣的:

帳戶iSumOpen iSumBuy iSumSell iSumSettleMM

帳戶2 ...

Account3 ...

+0

你的ExcelHelper類從哪裏來? – Jakotheshadows

+0

這是我的項目中的一個類。該類僅處理Excel的初始化,打開,寫入和關閉。沒想到這裏有必要嗎? –

+0

下一個鍵有沒有方法? –

回答

1

只是做一個櫃檯開始第一行,如果第0行是你的標題行,那麼從1開始,並在字典上使用foreach循環。

public void printToExcel(string vOutputPath) 
{ 
    string sExcelPath = @"C:\Users\jhochbau\Desktop\Test.xlsx"; 
    ExcelHelper excel = new ExcelHelper(sExcelPath); 
    excel.OpenWorkbook(); 
    int curRow = 1; //initialize to your starting row for data printing 
    foreach (KeyValuePair<string, List<DataRecord>> kvp in vSummaryResults) 
    { 
     string key = kvp.Key; //assigns key 

     List<DataRecord> list = kvp.Value; //assigns value 

     int iSumOpen = list.Sum(x => x.open); 
     int iSumBuy = list.Sum(x => x.buy); 
     int iSumSell = list.Sum(x => x.sell); 
     double iSumSettleMM = list.Sum(x => x.settleMM); 
     //No need to check vSummaryResults.ContainsKey(key) here, because you already took the key from the dictionary 
     //in 'string key = kvp.Key;' so you know it exists unless you've removed it 
     //also don't need to bother with a loop here, use LINQ instead 
     //foreach (DataRecord rec in list) 

     //WriteToSheet params: int row, int column, and value injected 
     //need to create some counters here to move the rows and columns? 
     int iColumn = 1; 
     excel.WriteToSheet(curRow, iColumn, key); 
     excel.WriteToSheet(curRow, iColumn + 1, iSumOpen); 
     excel.WriteToSheet(curRow, iColumn + 2, iSumBuy); 
     excel.WriteToSheet(curRow, iColumn + 3, iSumSell); 
     excel.WriteToSheet(curRow, iColumn + 4, iSumSettleMM); 
     curRow += 1; //increment to the next data row in the sheet 
    } 
} 
+0

只是做了2秒鐘之前,一定是引導你的腦波... –