C# 将数据表导出到 Excel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12533626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:36:12  来源:igfitidea点击:

Export DataTable to Excel

c#excel

提问by G?zim Shabani

Possible Duplicate:
How to export DataTable to Excel in C#

可能的重复:
如何在 C# 中将 DataTable 导出到 Excel

I have TemplateExcel included in my project, and I want to Export my datatable to Excel, I want to copy and save this template with data everywhere I want to save it, how can I do it??

我的项目中包含 TemplateExcel,我想将我的数据表导出到 Excel,我想复制并保存此模板和数据,我想在任何地方保存它,我该怎么做?

回答by Aghilas Yakoub

1 You can use this article - based on RenderControl, but you work on your binded Grid

1 您可以使用本文 - 基于RenderControl,但您在绑定的网格上工作

Link : http://www.codeproject.com/Tips/344604/Export-to-EXCEL-from-Datatable-in-Csharp-Net

链接:http: //www.codeproject.com/Tips/344604/Export-to-EXCEL-from-Datatable-in-Csharp-Net

2 You can base you developement on Table

2 您可以基于 Table 进行开发

Link : http://www.codeproject.com/Tips/406704/Export-DataTable-to-Excel-with-Formatting-in-Cshar

链接:http: //www.codeproject.com/Tips/406704/Export-DataTable-to-Excel-with-Formatting-in-Cshar

回答by JMK

You can do it through Excel Interop like this:

您可以通过 Excel Interop 这样做:

using System;
using System.Data;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main()
        {
            Excel.Application application = new Excel.Application();
            Excel.Workbook workbook = application.Workbooks.Add();
            Excel.Worksheet worksheet = workbook.Sheets[1];

            DataTable dataTable = new DataTable();
            DataColumn column = new DataColumn("My Datacolumn");

            dataTable.Columns.Add(column);
            dataTable.Rows.Add(new object[] {"Foobar"});

            var columns = dataTable.Columns.Count;
            var rows = dataTable.Rows.Count;

            Excel.Range range = worksheet.Range["A1", String.Format("{0}{1}", GetExcelColumnName(columns), rows)];

            object[,] data = new object[rows,columns];

            for (int rowNumber = 0; rowNumber < rows; rowNumber++)
            {
                for (int columnNumber = 0; columnNumber < columns; columnNumber++)
                {
                    data[rowNumber, columnNumber] = dataTable.Rows[rowNumber][columnNumber].ToString();
                }
            }

            range.Value = data;

            workbook.SaveAs(@"C:\test\whatever123.xlsx");
            workbook.Close();

            Marshal.ReleaseComObject(application);
        }

        private static string GetExcelColumnName(int columnNumber)
        {
            int dividend = columnNumber;
            string columnName = String.Empty;
            int modulo;

            while (dividend > 0)
            {
                modulo = (dividend - 1) % 26;
                columnName = Convert.ToChar(65 + modulo).ToString() + columnName;
                dividend = (int)((dividend - modulo) / 26);
            }

            return columnName;
        }
    }
}

All I am doing here is creating a System.Data.DataTableobject, filling it with some data and then exporting it to Excel. This needs cleaned up, error handling added, refactored etc but the basis is there.

我在这里所做的就是创建一个System.Data.DataTable对象,用一些数据填充它,然后将它导出到 Excel。这需要清理,添加错误处理,重构等,但基础就在那里。

Credit to Grahamfor the GetExcelColumnNamemethod.

感谢格雷厄姆GetExcelColumnName方法。