简体   繁体   中英

Datagridview to a specific excel cell

I got 2 datgridviews, trying to copy the header and it values to a excel sheet. The first loop is working fine for the first datagirdview

for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
            {
                worksheet.Cells[7, i] = dataGridView1.Columns[i - 1].HeaderText;
            }



            // storing Each row and column value to excel sheet
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                for (int j = 0; j < dataGridView1.Columns.Count; j++)
                {
                    worksheet.Cells[i + 8, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
                }
            }

This is the second datagridview and I'm trying to copy the header and it's values.

The first datagridview header values finished at 7nth row cell D7 and the row values finished at 8th row cell D8 .

I want to put the second datagridview header from E7 of 7nth row and the row values from E8 of 8th row.

//Primary Continuation
        for (int i = 7; i < dataGridView2.Columns.Count + 1; i++)
        {
            worksheet.Cells[7, i] = dataGridView2.Columns[i - 1].HeaderText;
        }



        // storing Each row and column value to excel sheet
        for (int i = 0; i < dataGridView2.Rows.Count - 1; i++)
        {
            for (int j = 0; j < dataGridView2.Columns.Count; j++)
            {
                worksheet.Cells[i + 8, j + 1] = dataGridView2.Rows[i].Cells[j].Value.ToString();
            }
        }

So if I understand you correctly, you want to put your two tables next to each other on the Excel sheet? In which case it looks like you're getting your offsets mixed up in the loops for the second DataGridView . In this loop:

//Primary Continuation
for (int i = 7; i < dataGridView2.Columns.Count + 1; i++)
{
    worksheet.Cells[7, i] = dataGridView2.Columns[i - 1].HeaderText;
}

Instead of offsetting your count by 7 (which drives the source data location in the grid), you need to offset the destination in the Excel sheet, since the location in the second DataGridView will be the same as the first. You can replicate your first loop but just change the destination slightly. In fact the same will work for your final loop as well.

It's also worth noting that your loop to copy the values will miss out the final row. I'm not sure whether this was intentional or not, but I've corrected it. Here is how your second set of loops should look.

//Primary Continuation
for (int i = 1; i < dataGridView2.Columns.Count + 1; i++)
{
    worksheet.Cells[7, i + dataGridView2.Columns.Count] = dataGridView2.Columns[i - 1].HeaderText;
}

// storing Each row and column value to excel sheet
for (int i = 0; i < dataGridView2.Rows.Count; i++)
{
    for (int j = 0; j < dataGridView2.Columns.Count; j++)
    {
        worksheet.Cells[i + 8, j + 1 + dataGridView2.Columns.Count] = dataGridView2.Rows[i].Cells[j].Value.ToString();
    }
}

You'll notice that since the difference between your two sets of loops is now negligible, you could easily combine them together, cutting down on execution time (although obviously the impact would only be noticeable with much more data).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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