c#导出表格到Excel

本文关键字:Excel 表格 | 更新日期: 2023-09-27 18:17:44

如何将此数据表导出到excel使用:"Microsoft.Office.Interop. "Excel"我有这个代码抓取所有的数据形式的主表,并希望将其导出到Excel更好的视图,不想使用数据网格。我认为有很多关于这个主题的帖子,但通常只是推荐使用一些广告,如"closexml"

        OleDbConnection mycon;           
        DataTable Table = new DataTable("AllData");
        mycon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:'Users'jm11321'Documents'DB.mdb;Persist Security Info=False");
        string command = "Select *From Master";
        OleDbCommand oleDbCmd = new OleDbCommand(command,mycon);
        OleDbDataAdapter adapt = new OleDbDataAdapter(oleDbCmd);
        mycon.Open();
        adapt.Fill(Table);
        mycon.Close();

任何帮助都是感激的。

c#导出表格到Excel

你必须从这里导入Microsoft.Office.Interop.Excel.dll库。在项目中添加新的类文件,例如ExcelUtility。只需在其中写下以下代码。

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Excel = Microsoft.Office.Interop.Excel;
namespace ExcelDemo
{
    public class ExcelUtility
    {
        public static void CreateExcel(DataSet ds, string excelPath)
        {
            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;
            try
            {
                //Previous code was referring to the wrong class, throwing an exception
                xlApp = new Excel.Application();
                xlWorkBook = xlApp.Workbooks.Add(misValue);
                xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
                {
                    for (int j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
                    {
                        xlWorkSheet.Cells[i + 1, j + 1] = ds.Tables[0].Rows[i].ItemArray[j].ToString();
                    }
                }
                xlWorkBook.SaveAs(excelPath, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                xlWorkBook.Close(true, misValue, misValue);
                xlApp.Quit();
                releaseObject(xlApp);
                releaseObject(xlWorkBook);
                releaseObject(xlWorkSheet);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private static void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch
            {
                obj = null;
            }
            finally
            {
                GC.Collect();
            }
        } 
    }
}

现在在主代码中只传递数据集对象和excel路径如下。

ExcelUtility.CreateExcel(ds, "D:''Demo.xls");

我已经在我的项目中测试并使用了这个

我知道已经有答案了,尽管这里有另一种方法

 var workbook = new XLWorkbook();
        var worksheet = workbook.Worksheets.Add("Sample Sheet");
        worksheet.Cell("A1").Value = "Hello World!";
        workbook.SaveAs("HelloWorld.xlsx");

这种方法的另一个神奇之处在于,你可以把你的数据集表或表放入工作簿,就像

 var wb = new XLWorkbook();
        var dataTable = GetTable("Information");
        // Add a DataTable as a worksheet
        wb.Worksheets.Add(dataTable);
        wb.SaveAs("AddingDataTableAsWorksheet.xlsx");

DLL在这里