简体   繁体   English

Datagridview到特定的Excel单元格

[英]Datagridview to a specific excel cell

I got 2 datgridviews, trying to copy the header and it values to a excel sheet. 我有2个datgridviews,试图将标头及其值复制到Excel工作表中。 The first loop is working fine for the first datagirdview 对于第一个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. 这是第二个datagridview,我正在尝试复制标头及其值。

The first datagridview header values finished at 7nth row cell D7 and the row values finished at 8th row cell D8 . 第一datagridview头值在第7n行单元D7处完成,并且行值在第8行单元D8处完成。

I want to put the second datagridview header from E7 of 7nth row and the row values from E8 of 8th row. 我想放置第7行E7的第二个datagridview标头和第8行E8的行值。

//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? 因此,如果我对您的理解正确,您是否希望将两个表在Excel工作表中彼此相邻放置? In which case it looks like you're getting your offsets mixed up in the loops for the second DataGridView . 在这种情况下,您似乎在第二个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. 而不是将计数偏移7(这会驱动源数据在网格中的位置),您需要在Excel工作表中偏移目标,因为第二个DataGridView的位置将与第一个相同。 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). 您会注意到,由于两组循环之间的差异现在可以忽略不计,因此您可以轻松地将它们组合在一起,从而减少了执行时间(尽管显然只有更多的数据才会产生明显的影响)。

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

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