Hey! 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. 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. What I have tried: 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). How could I fix this? Thanks in advance!
int excelrow = 1; for (int i = 0; i < dataGridView1.Rows.Count - 1; ) // do not increment i here { for (int j = 1; j < 12; j++) { xlsht.Cells[excelrow, j] = dataGridView1.Rows[i].Cells[0].Value.ToString(); i++; // increment i here to go to the next row of the DataGridView } excelrow++; }
Rows[i].Cells[0]
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)