How to export DataTable to Excel

后端 未结 21 2344
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 15:36

How can I export a DataTable to Excel in C#? I am using Windows Forms. The DataTable is associated with a DataGridView control. I have

21条回答
  •  心在旅途
    2020-11-22 16:03

    You can use my SwiftExcel library. It is especially good when performance and low memory usage come in place as it writes data directly to the file:

    using (var ew = new ExcelWriter("C:\\temp\\test.xlsx"))
    {
        for (var row = 1; row <= 100; row++)
        {
            for (var col = 1; col <= 10; col++)
            {
                ew.Write($"row:{row}-col:{col}", col, row);
            }
        }
    }
    

    Nuget command to install:

    Install-Package SwiftExcel
    

提交回复
热议问题