如何将DATA GRID VIEW中的整数值导出到EXCEL ..? [英] How to EXPORT integer value from DATA GRID VIEW to EXCEL..?

查看:106
本文介绍了如何将DATA GRID VIEW中的整数值导出到EXCEL ..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据从datagridview导出到excel。我成功地能够导出所有值而不是INTEGER值任何帮助。

在这种情况下,Mewmber ID没有显示。



这是我的CODE。

I'm trying to export data from datagridview to excel. I'm successfully able to export all value but not INTEGER value any Help.
In this case Mewmber ID is not getting displayed.

Here is my CODE.

private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application();
                Workbook wb = Excel.Workbooks.Add(XlSheetType.xlWorksheet);
                Worksheet ws = (Worksheet)Excel.ActiveSheet;
                Excel.Visible = true;


                ws.Cells[1, 1] = "Mewmber ID"; //this column value is not getting displayed. 
                ws.Cells[1, 2] = "First Name";
                ws.Cells[1, 3] = "Last Name";
                ws.Cells[1, 4] = "Start Date";
                ws.Cells[1, 5] = "Amount";
                ws.Cells[1, 6] = "Pnd Amt";

                for (int j = 2; j <= dataGridView1.Rows.Count + 1; j++)
                {
                    for (int i = 2; i <= 6; i++)
                    {
                        ws.Cells[j, i] = dataGridView1.Rows[j - 2].Cells[i - 1].Value;
                    }
                }


            }
            catch (Exception ex)
            {
                MessageBox.Show("Please make Sure weather Microsoft Office is installed or not");
 
            }
}

推荐答案

想一想! DataGridView的行和列索引以 0 (零)开头。你需要减去多少 i



Think of it! Row and Column index of DataGridView starts with 0 (zero). How many you need to subtract from i?

ws.Cells[j, i] = dataGridView1.Rows[j - 2].Cells[i - 1].Value;


oops如果i = 1,我错误地插入了i = 2。 />
通过将i值更改为1来解决它。

感谢您的建议成员..
oops i was had mistakenly inserted i=2 instead if i=1.
by changing the i value to 1 it got solved.
thanks for your suggestion members..


这篇关于如何将DATA GRID VIEW中的整数值导出到EXCEL ..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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