简体   繁体   English

C# 从 datagridview 填充 excel 直到到达某个列

[英]C# filling excel from datagridview until getting to a certain column

Basically what I need to do is, I have a lot of data that takes up 1 cell per row loaded into a datagridview, and I want to pivot it, and export it to an excel file.基本上我需要做的是,我有很多数据每行占用 1 个单元格加载到 datagridview 中,我想要 pivot 并将其导出到 excel 文件。 So what I have is a vertical list, and I would like to make it horizontal, but I only need the cells to be filled until getting to column 'L', and when the data gets to it, then it starts again at the next row, and repeating this until dgv has no more data to fill in to the excel.所以我有一个垂直列表,我想让它水平,但我只需要填充单元格直到到达“L”列,当数据到达它时,它会在下一个重新开始行,并重复此操作,直到 dgv 没有更多数据可填写到 excel。 So far what I have is:到目前为止,我所拥有的是:

private void button1_Click(object sender, EventArgs e)
    {
        Excel.Application xlApp = new Excel.Application();
        Excel.Worksheet xlsht = new Excel.Worksheet();
        Microsoft.Office.Interop.Excel.Workbook xlwb;
        xlApp.Visible = true;
        string path = @"myfilepath";
        xlwb = xlApp.Workbooks.Open(path);
        xlsht = xlwb.Worksheets["Tabelle1"];
        
        for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
        {

            for (int j = 1; j < 12; j++)
            {
                xlsht.Cells[i + 1, j] = dataGridView1.Rows[i].Cells[0].Value.ToString();
            }       

        }

The problem with this is the following: it fills the cells with the same value, until getting to the column 'L' (11th column, that's why I have 12 in the nested for loop), and then it starts again in the next row, but again, repeating the next value(to clarify, the value in the first row is 1, so it repeats 1 until getting to the 11th column.Then it starts again with the next value, for example 2, and does the same thing).问题如下:它用相同的值填充单元格,直到到达“L”列(第 11 列,这就是为什么我在嵌套的 for 循环中有 12 个),然后在下一行再次开始,但同样,重复下一个值(澄清一下,第一行中的值是 1,所以它重复 1 直到到达第 11 列。然后它再次从下一个值开始,例如 2,并做同样的事情)。 How could I fix this?我该如何解决这个问题? Thanks in advance!提前致谢!

You need to separate the Excel row and column indexes from the loop through the grids rows.您需要通过网格行将 Excel 行和列索引与循环分开。 All you need is one loop though all the rows to add the first column of that row to the worksheet.您所需要的只是一个循环,通过所有行将该行的第一列添加到工作表中。 To keep it simple, pull the Excel indexs and manually update them.为简单起见,请提取 Excel 索引并手动更新它们。

In the loop through the rows, increment the worksheets column index by 1 until it reaches 12, Then increment the excel row index by 1 and reset the excel column back to 1.在遍历行的循环中,将工作表列索引递增 1 直到达到 12,然后将 excel 行索引递增 1,并将 excel 列重置回 1。

Something like….就像是…。

int xlRow = 1;
int xlCol = 1;
for (int i = 0; i < dataGridView1.Rows.Count; i++) {
  if (!dataGridView1.Rows[i].IsNewRow) {
    xlsht.Cells[xlRow, xlCol] = dataGridView1.Rows[i].Cells[0].Value.ToString();
    if (xlCol == 12) {
      xlRow++;
      xlCol = 1;
    }
    else {
      xlCol++;
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM