Gridview导出到Excel - 格式化问题

时间:2013-06-22 13:14:44

标签: c# excel datagridview

我不确定这里发生了什么。我使用这个按钮事件将数据从datagridview导出到excel,数据导出,文件保存等等,所以看起来对我来说工作正常。

private void button2_Click(object sender, EventArgs e)
    {
        Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
        Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
        Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
        app.Visible = true;
        try
        {
            worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["Sheet1"];
            worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;
            worksheet.Name = "Gioietta Environment Data";
            for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
            {
                worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
            }
            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();
                }
            }
            string fileName = String.Empty;
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "Excel files |*.xls|All files (*.*)|*.*";
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.RestoreDirectory = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                fileName = saveFileDialog1.FileName;
                workbook.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            }
            else
                return;               
        }
        catch (System.Exception ex)
        {

        }
        finally
        {
            app.Quit();
            workbook = null;
            app = null;
        }
    }

我遇到的问题在于excel方面。基本上甚至将导出的值格式化为数字,我不能使用它们。我甚至无法总结它们!如果我手动重新键入同一单元格中的值而不是它可用。这与输出过程有关吗?

这是我将数据加载到datagridview的方式:

var time = DateTime.Now.ToString("HH:mm:ss");
        dataGridView1.Rows.Add(new string[] { time, textBox1.Text, textBox2.Text });

enter image description here

1 个答案:

答案 0 :(得分:1)

问题来自这一行以及如何填充DataGridView:

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

数据导出为string,无论.ToString()的类型是什么,它们都是字符串。

由于DataGridViewCell.Value包含string,因此您必须在导出之前将dgvCell.Value强制转换为 numeric 值。例如:

if (j == 1 || j == 2) 
{
    worksheet.Cells(i   2, j   1) = Convert.ToDecimal(dataGridView1.Rows(i).Cells(j).Value)
} 
else 
{
    worksheet.Cells[i   2, j   1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}

如果DataGridViewCell.Value包含 numeric 值(Decimal,Double,Integer,...),只需删除.toString(),它应该按预期工作(Excel将设置相应的类型)。

在这两种情况下,如果您要应用自定义格式在Excel中显示,您将在导出期间将其丢失,并且必须使用Range.NumberFormat属性。这些Q / A有助于实现这一目标: