DataGridView到Excel [英] DataGridView To Excel

查看:127
本文介绍了DataGridView到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在将DataGridView插入excel工作表的底部添加datagridview列的总计.因此,如果我的Datagrid包含一列,则该列的行中有数字.我想将所有这些数字的SUM添加到具有值的列的末尾.得到总数是没有问题的.目前在文本框中.

这就是我将DataGrid添加到Excel工作表的方式.我可以生成列的总计并将其添加到文本框中.没问题.只需在Excel表格中生成的Datagrid部分末尾的文本框中获得该总计即可.问题在于datagrid具有各种大小(表示行),因此具有总计的单元格将不会总是位于同一位置.....

Hi I want to add the Totals of my datagridview Column at the bottom of where I insert my DataGridView into a excel worksheet. So if my Datagrid contains a column that''s has numbers in the rows of the column. I want to add the SUM of all those numbers to the end of the column that has the values. To get the totals is no problem. It''s in a textbox at the moment.

This is how I add my DataGrid to the Excel Sheet. I can generate the totals of the column and add it into a textbox. No problem. Just need to get that total in the textbox at he end of the Datagrid section where it is generated in the Excel Sheet. Problem is the datagrid has various sizes (meaning rows) so that cell with the total will not always be in the same place.....

int i = 0;
int j = 0;

for (i = 0; i <= dataGridView1.RowCount - 1; i++)
{
    for (j = 0; j <= dataGridView1.ColumnCount - 1; j++)
    {
        DataGridViewCell cell = dataGridView1[j, i];
        xlWorkSheet.Cells[i + 23, j + 1] = cell.Value;
    }

}



谢谢任何帮助都可以.....



Thanks Any help will do.....

推荐答案

德克
您可以使用DataGridView的RowCount属性来访问它的最后一行:
Hi,Dirk
you can use the RowCount property of your DataGridView to access to the last row of it :
dataGridView1.Rows[dataGridView1.RowCount].Cells["CellName"].Value = txtTotal.Text;



然后将其与其他值的datagrid一起添加到Excel Sheet中.



and then add it to Excel Sheet with other values of datagrid.


所以我想到了这个解决方案.在这种情况下,添加int以选择列(k),然后使用i int + 1将其放在数据网格下方.工作100%是!

谢谢您的建议thoung phoenix1167 :-)

So I came up with this solution. Add int to select the column (k) in this case, then use i int + 1 to place it underneath the datagrid. Working 100% Yeh!

Thank for you suggestion thoung phoenix1167 :-)

int i = 0;
           int j = 0;
           int k = 2;
           int l = 1;

           for (i = 0; i <= dataGridView1.RowCount - 1; i++)
           {
               for (j = 0; j <= dataGridView1.ColumnCount - 1; j++)
               {
                   DataGridViewCell cell = dataGridView1[j, i];
                   xlWorkSheet.Cells[i + 18, j + 1] = cell.Value;
                   xlWorkSheet.Cells[i + 18 + 1, k + 1] = label13.Text;
                   xlWorkSheet.Cells[i + 18 + 1, l + 1] = "Total:";

               }

           }


这篇关于DataGridView到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆