0

我正在创建一个将数据导出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);
    }
4

2 回答 2

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
}
于 2013-09-06T23:24:10.917 回答
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

为它工作添加一个新的参考找到微软的excel

于 2020-12-18T02:16:26.823 回答