我正在尝试将汇总的值导出到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),并且没有找到合理的方法来执行此操作,部分原因是因为我对词典和方法缺乏经验它的。
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)
只需从第一行开始计数器,如果第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
}
}