将数据从datagridview导出为ex​​cel格式

时间:2018-12-09 13:33:12

标签: c# excel gridview

当我导出网格视图数据以导出到Excel文件时,出现错误null值。请帮助我更正我的代码。

object mis = System.Reflection.Missing.Value;
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 = false;
worksheet = (Worksheet)workbook.Sheets["Sheet1"];
worksheet = (Worksheet)workbook.ActiveSheet;
worksheet.Name = "Export";
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();
    }
}

SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Excel Document(*.xlsx)|*.xlsx";
sfd.FileName = "Export";
if (sfd.ShowDialog() == DialogResult.OK)
{
    workbook.SaveAs(sfd.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);
}
app.Quit();

2 个答案:

答案 0 :(得分:0)

using Microsoft.Office.Interop.Excel;
using Application = Microsoft.Office.Interop.Excel.Application;

 var app = new Application();
 _Workbook workbook = app.Workbooks.Add(Type.Missing);
 app.Visible = false;
 _Worksheet worksheet = (Worksheet) workbook.Sheets["Sayfa1"];
 worksheet.Name = "Export";
 for (var i = 1; i < dataGridView1.Columns.Count + 1; i++)
        worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
 for (var i = 0; i < dataGridView1.Rows.Count - 1; i++)
 for (var j = 0; j < dataGridView1.Columns.Count; j++)
        worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value;

 var sfd = new SaveFileDialog();
 sfd.Filter = "Excel Document(*.xlsx)|*.xlsx";
 sfd.FileName = "Export";
 if (sfd.ShowDialog() == DialogResult.OK)
        workbook.SaveAs(sfd.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
 app.Quit();

答案 1 :(得分:0)

var app = new Application();
_Workbook workbook = app.Workbooks.Add(Type.Missing);
app.Visible = true;
_Worksheet worksheet = (Worksheet) workbook.Sheets["Sayfa1"];
worksheet.Name = "Export";
dataGridView1.Columns[0].Visible = false;
List < DataGridViewColumn > listVisible = new List < DataGridViewColumn > ();
foreach(DataGridViewColumn col in dataGridView1.Columns) {
    if (col.Visible)
        listVisible.Add(col);
}
for (int i = 0; i < listVisible.Count; i++) {
    worksheet.Cells[1, i + 1] = listVisible[i].HeaderText;
}
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) {
    for (int j = 0; j < listVisible.Count; j++) {
        worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[listVisible[j].Name].Value.ToString();
    }
}