C# 将数据DataGridView导出到Excel

C# 将数据DataGridView导出到Excel,c#,datagridview,export-to-excel,C#,Datagridview,Export To Excel,我正在创建一个类,用于将数据从DataGridView导出到Excel文件。当我调用该方法时,我得到了一个空的excel工作表。但是,当我在代码中直接插入方法而不使用类时,它确实起作用 知道这门课为什么不起作用吗 class ExportToExcel { public Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();

我正在创建一个类,用于将数据从
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;

    }
}
exportToExcel(DataGridView dt)

看起来您的方法同时引用了this.dt(类变量)和dt的本地版本(作为参数传递给方法的版本)

根据您的代码-dt的类版本从未设置为任何值

我不确定你希望你的类是如何工作的,但是你可能想考虑设置DT而不将另一个GRIDVIEW导入ExtExtoExcel方法。

 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
}
exportToExcel(DataGridView dt)

看起来您的方法同时引用了this.dt(类变量)和dt的本地版本(作为参数传递给方法的版本)

根据您的代码-dt的类版本从未设置为任何值

我不确定你希望你的类是如何工作的,但是你可能想考虑设置DT而不将另一个GRIDVIEW导入ExtExtoExcel方法。

 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
}
为使其正常工作,请添加新的参考,然后查找microsoft excel


要使其正常工作,请添加新的参考资料,请查找microsoft excel

谢谢您的快速回复!我删除了关键字“this”,并更改了变量名,现在可以工作了。感谢您的快速回复!我删除了关键字“this”,并更改了变量名,现在可以使用了。
  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