代码只是反复将最后数据写入Excel

时间:2017-08-07 17:48:58

标签: c# foreach

我的语法只是将foreach循环的最后一个值写入每个单元格,而不是从foreach循环中编写每个元素。我确信这是我忽略的东西,但是我的语法需要改变什么才能在每行上相应地写出foreach循环的每个元素?

foreach (var calcs in dictionary)
{
    var FileInfo = new FileInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "WriteDataToExcelThroughCode.xlsx"));
    using (ExcelPackage package = new ExcelPackage(FileInfo))
    {
        ExcelWorksheet ws = package.Workbook.Worksheets["TTest2"];
        //ExcelWorksheet ws = package.Workbook.Worksheets[0];

        ws.Cells["A24:C36"].Clear();

        int z = 0;
        int x = 24;
        while (z <= numofrows)
        {
            ws.Cells["A" + x + ":A" + x].Value = calcs.Key;
            ws.Cells["B" + x + ":B" + x].Value = dictionary[calcs.Key][0];
            ws.Cells["C" + x + ":C" + x].Value = dictionary[calcs.Key][1];
            //ws.Cells["D" + x + "D" + x].Value = dictionary[calcs.Key][2];
            x = x + 1;
            z = z + 1;
        }
        package.Save();
    }
}

修改
为了更好地说明我的预期结果,如下所示。 假设计算包含以下信息

James 1   2
Jose  3   4
Bob   5   6

我希望将以下信息写入以下单元格中 A24 =詹姆斯B24 = 1 C24 = 2
A25 =何塞B25 = 3 C25 = 4
A26 = Bob B26 = 5 C26 = 6

编辑2
我的变量numofrows设置如此

int start = Convert.ToInt32(txtstart.Text);
int end = Convert.ToInt32(txtEnd.Text) + 1;
int numofrows = end - start;

int[] abc = Enumerable.Range(start, end-start).ToArray();

foreach (int a in abc)
{
    Dictionary<int, int[]> dictionary = new Dictionary<int, int[]>();
    dictionary.Add(a, new int[] { 2, 3 });
    foreach (var calcs in dictionary)
    {
      // original code
     }
}



    private void btnHitIT_Click(object sender, EventArgs e)
{
    int start = Convert.ToInt32(Alpha);
    int end = Convert.ToInt32(AlphaEnd)+1;
    int numofrows = end - start;
    int[] angles = Enumerable.Range(start, end - start).ToArray();
    foreach (int a in angles)
    {
        Dictionary<int, int[]> dictionary = new Dictionary<int, int[]>();
        dictionary.Add("Jack", new int[] { 2, 3 });
        dictionary.Add("James", new int[] { 7, 11 });
        dictionary.Add("Jason", new int[] { 14, 21 });
        pDrawingArea.Invalidate();
        var FileInfo = new FileInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "WriteDataToExcelThroughCode.xlsx"));
        using (ExcelPackage package = new ExcelPackage(FileInfo))
        {
            ExcelWorksheet ws = package.Workbook.Worksheets["TTest2"];
            int x = 24;
            foreach (var calcs in dictionary)
            {
                ws.Cells["A24:C36"].Clear();
                for (int z = 0; z <= numofrows; ++z)
                {
                    ws.Cells["A" + x + ":A" + x].Value = calcs.Key;
                    ws.Cells["B" + x + ":B" + x].Value = dictionary[calcs.Key][0];
                    ws.Cells["C" + x + ":C" + x].Value = dictionary[calcs.Key][1];
                    ++x;
                }
            }
            package.Save();
        }
    }

1 个答案:

答案 0 :(得分:1)

您正在错误的位置定义变量x并将其递增。您需要在foreach循环之外定义它并将其增加到while循环之外(但在foreach循环内)。

我已将您的内部while循环更改为更合适的for循环。

同时将WorkSheet定义移到foreach循环之外,因为没有理由不断打开和关闭excel文件。

我注意到,如果您已在Dictionary

中拥有该值,则会在calcs.Value中搜索两次值
var FileInfo = new FileInfo(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "WriteDataToExcelThroughCode.xlsx"));
using (ExcelPackage package = new ExcelPackage(FileInfo))
{
    ExcelWorksheet ws = package.Workbook.Worksheets["TTest2"];
    //ExcelWorksheet ws = package.Workbook.Worksheets[0];
    int x = 24;
    foreach (var calcs in dictionary)
    {
        ws.Cells["A24:C36"].Clear();

        for (int z = 0; z <= numofrows; ++z)
        {
            ws.Cells["A" + x + ":A" + x].Value = calcs.Key;
            ws.Cells["B" + x + ":B" + x].Value = calcs.Value[0];
            ws.Cells["C" + x + ":C" + x].Value = calcs.Value[1];
            //ws.Cells["D" + x + "D" + x].Value = calcs.Value[2];
            ++x;
        }
        package.Save();
    }
}