将数据DataGridView导出到Excel

时间:2013-09-06 23:13:18

标签: c# datagridview export-to-excel

我正在创建一个类,用于将数据从DataGridView导出到Excel文件。当我调用该方法时,我得到一个空的excel表。但是,当我直接在代码中插入方法而不使用类时,它确实有效。

知道为什么这个班不起作用。

class ExportToExcel
{
       public Microsoft.Office.Interop.Excel.Application ExcelApp =  new Microsoft.Office.Interop.Excel.Application();
       public Microsoft.Office.Interop.Excel._Workbook ExcelBook;
       public Microsoft.Office.Interop.Excel._Worksheet ExcelSheet;
       DataGridView dt = new DataGridView();

       public  DataGridView Dt { set { this.dt = value; } }


       public Microsoft.Office.Interop.Excel.Application exportToExcel(DataGridView dt)
        {
        int i = 0;
        int j = 0;



        ExcelBook = (Microsoft.Office.Interop.Excel._Workbook)ExcelApp.Workbooks.Add(1);
        ExcelSheet = (Microsoft.Office.Interop.Excel._Worksheet)ExcelBook.ActiveSheet;
        //export header
        for (i = 1; i <= this.dt.Columns.Count; i++)
        {
            ExcelSheet.Cells[1, i] = this.dt.Columns[i - 1].HeaderText;
        }

        //export data
        for (i = 1; i <= this.dt.RowCount; i++)
        {
            for (j = 1; j <= dt.Columns.Count; j++)
            {
                ExcelSheet.Cells[i + 1, j] = dt.Rows[i - 1].Cells[j - 1].Value;
            }
        }


            ExcelApp.Visible = true;
            ExcelSheet = null;
            ExcelBook = null;
            ExcelApp = null;

         return ExcelApp;

    }
}

private void exportToExcel_Click(object sender, EventArgs e)
    {

        ExportToExcel ex = new ExportToExcel();
        ex.exportToExcel(dtReport2);
    }

2 个答案:

答案 0 :(得分:3)

exportToExcel(DataGridView dt)

看起来你的方法是指this.dt(类变量)和本地版本的dt(你作为参数传递给方法的那个。)

根据您的代码 - dt的类版本永远不会被设置为任何内容。

我不确定您希望班级如何工作,但您可能需要考虑设置dt而不是将另一个gridview传递到您的exportToExcel方法。

 private void exportToExcel_Click(object sender, EventArgs e)
{

    ExportToExcel ex = new ExportToExcel();
    ex.dt = dtReport2; // set the public class variable here
    ex.exportToExcel(); // removed the datagrid from your parameter in the exportToExcel() method
}

答案 1 :(得分:0)

  use these codes its perfectly working
Imports System.Linq
Imports System.Data.SqlClient
Imports System.Data.OleDb
Imports Microsoft.Office.Core
Imports Excel = Microsoft.Office.Interop.Excel
Imports ExcelAutoFormat = Microsoft.Office.Interop.Excel.XlRangeAutoFormat
Imports Microsoft.Office.Interop
Imports System.IO
Imports System.Xml.XPath
Imports System.Data
Imports System.Xml

 Dim xlApp As Microsoft.Office.Interop.Excel.Application
        Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
        Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value
        Dim i As Integer
        Dim j As Integer
        xlApp = New Microsoft.Office.Interop.Excel.Application
        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets("sheet1")
        For i = 0 To TAPDataGridView.RowCount - 2
            For j = 0 To TAPDataGridView.ColumnCount - 1
                For k As Integer = 1 To TAPDataGridView.Columns.Count
                   xlWorkSheet.Cells(1, k) = TAPDataGridView.Columns(k - 1).HeaderText
              xlWorkSheet.Cells(i + 2, j + 1) = TAPDataGridView(j, i).Value.ToString()
                Next
            Next
        Next


        xlWorkSheet.SaveAs("C:\Users\P A R\Documents\?.xlsx")
        xlWorkBook.Close()
        xlApp.Quit()
        releaseObject(xlApp)
        releaseObject(xlWorkBook)
        releaseObject(xlWorkSheet)

        MsgBox("You can find the file D:\Todays_record_excel.xlsx")
    End Sub

要使其正常工作,请添加新参考,找到Microsoft Excel