c#:尝试将字典键和值注入到Excel文件中

我正试图将总结的值导出到excel文件中。 我可以很容易地用CSV文件做到这一点,但是用excel这样做对我来说很困难,因为我一次只能向单元格注入一个值。 我必须将其导出为xlsx扩展名。

我在这里要做的是将第一个参数设置为行号,将第二个参数设置为列号,将第三个参数设置为要注入的值。

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),并没有运气find这样做的逻辑方式,部分原因是我对字典和方法没有经验的。

  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行是你的标题行,然后从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 } }