将数据 DataGridView 导出到 Excel

本文关键字:Excel 数据 DataGridView | 更新日期: 2023-09-27 18:30:38

我正在创建一个类来将数据从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);
    }

将数据 DataGridView 导出到 Excel

exportToExcel(DataGridView dt)

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

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

我不确定您希望您的类如何工作,但您可能需要考虑设置 dt 并且不将另一个网格视图传递到 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
}
  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