【问题标题】:exporting datagridview with combobox to excel c#将带有组合框的datagridview导出到excel c#
【发布时间】:2017-09-10 17:25:32
【问题描述】:

获取错误对象引用未设置为对象的实例。如果我删除 DataGridViewComboBoxColumn,那么它可以正常导出,但我需要在 excel 中导出带有选定值的下拉列表。我刚刚在 datagrid 视图中添加了一些数据,如下所示:

 dataGridView1.ColumnCount = 3;
        dataGridView1.Columns[0].Name = "Product ID";
        dataGridView1.Columns[1].Name = "Product Name";
        dataGridView1.Columns[2].Name = "Product Price";

        string[] row = new string[] { "1", "Product 1", "1000" };
        dataGridView1.Rows.Add(row);
        row = new string[] { "2", "Product 2", "2000" };
        dataGridView1.Rows.Add(row);
        row = new string[] { "3", "Product 3", "3000" };
        dataGridView1.Rows.Add(row);
        row = new string[] { "4", "Product 4", "4000" };
        dataGridView1.Rows.Add(row);

        DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
        cmb.HeaderText = "Select Data";
        cmb.Name = "cmb";
        cmb.MaxDropDownItems = 4;
        cmb.Items.Add("True");
        cmb.Items.Add("False");
        dataGridView1.Columns.Add(cmb);

//导出到Excel

        worksheet = workbook.ActiveSheet;

            worksheet.Name = "test";

            int cellRowIndex = 1;
            int cellColumnIndex = 1;
            for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
            {
                worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
            }
            MessageBox.Show(dataGridView1.ColumnCount.ToString());



            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                for (int j = 0; j < dataGridView1.Columns.Count; j++)
                {

                        worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();

                    cellColumnIndex++;
                }
                cellColumnIndex = 1;
                cellRowIndex++;
            }

            //Getting the location and file name of the excel to save from user.
            SaveFileDialog saveDialog = new SaveFileDialog();
            //saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
            saveDialog.Filter = "Excel files (All files (*.*)|*.*";
            saveDialog.FilterIndex = 2;

            if (saveDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                workbook.SaveAs(saveDialog.FileName);
                MessageBox.Show("Export Successful");
            }

【问题讨论】:

  • 你选择了组合框中的值吗?或将默认值设置为组合框
  • 我尝试过使用和不使用 cmb.DefaultCellStyle.NullValue = "True";没有成功。

标签: c# excel datagridview datagridcomboboxcolumn


【解决方案1】:

试试这个从组合框中检索选定的值

dataGridView1.Rows[0].Cells[i].FormattedValue.ToString();

您可以使用相同的方法从单元格值中检索而不是 Rows[0].Cells[i].value

你的情况

for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
        {
            for (int j = 0; j < dataGridView1.Columns.Count; j++)
            {

                    worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].FormattedValue.ToString();

                cellColumnIndex++;
            }
            cellColumnIndex = 1;
            cellRowIndex++;
        }

【讨论】:

  • 正在导出,但excel中没有显示下拉值。
  • 我需要在 excel 中下拉 true false
  • 我最后的下拉选择值你设置了默认值吗?`cmb.DefaultCellStyle.NullValue = "True";
  • 是的,值“true”正在导出,但我需要 excel 中的下拉菜单,以便用户可以将字段更改为 false。
  • 你的意思是你需要csv中的组合框?您提到您“需要使用下拉值导出”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多