【问题标题】:Exporting the values in List to excel将列表中的值导出到excel
【发布时间】:2010-02-05 09:45:25
【问题描述】:

您好,我有一个包含值列表的列表容器。我希望将列表值直接导出到 Excel。有什么办法可以直接做吗?

【问题讨论】:

  • 我想把它导出到excel。谁能给我一个示例代码?
  • 我在帖子中为您提供了一个指向 MSDN 示例的链接,该示例提供了有关如何通过互操作创建数据并将数据写入 Excel 文件的示例代码。

标签: c# excel ms-office office-interop excel-interop


【解决方案1】:

好的,如果你想使用 COM,这里有一个分步指南。

  1. 您必须安装 Excel。
  2. 将项目引用添加到 excel 互操作 dll。去做这个 在 .NET 选项卡上选择 Microsoft.Office.Interop.Excel。 可能有多个程序集 用这个名字。选择 适合您的 Visual Studio 和 Excel 版本。
  3. 这是一个代码示例,用于创建新工作簿并在列中填充 您列表中的项目。

using NsExcel = Microsoft.Office.Interop.Excel;

public void ListToExcel(List<string> list)
{
    //start excel
    NsExcel.ApplicationClass excapp = new Microsoft.Office.Interop.Excel.ApplicationClass();

    //if you want to make excel visible           
    excapp.Visible = true;

    //create a blank workbook
    var workbook = excapp.Workbooks.Add(NsExcel.XlWBATemplate.xlWBATWorksheet);

    //or open one - this is no pleasant, but yue're probably interested in the first parameter
    string workbookPath = "C:\test.xls";
    var workbook = excapp.Workbooks.Open(workbookPath,
        0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
        true, false, 0, true, false, false);

    //Not done yet. You have to work on a specific sheet - note the cast
    //You may not have any sheets at all. Then you have to add one with NsExcel.Worksheet.Add()
    var sheet = (NsExcel.Worksheet)workbook.Sheets[1]; //indexing starts from 1

    //do something usefull: you select now an individual cell
    var range = sheet.get_Range("A1", "A1");
    range.Value2 = "test"; //Value2 is not a typo

    //now the list
    string cellName;
    int counter = 1;
    foreach (var item in list)
    {
        cellName = "A" + counter.ToString();
        var range = sheet.get_Range(cellName, cellName);
        range.Value2 = item.ToString();
        ++counter;
    }

    //you've probably got the point by now, so a detailed explanation about workbook.SaveAs and workbook.Close is not necessary
    //important: if you did not make excel visible terminating your application will terminate excel as well - I tested it
    //but if you did it - to be honest - I don't know how to close the main excel window - maybee somewhere around excapp.Windows or excapp.ActiveWindow
}

【讨论】:

  • 请注意:通常在整个电子表格准备好之前,您不希望 excel 可见。在这种情况下,您应该在设置完工作表后将excapp.Visible = true; 移至。
  • 只适合像我这样对ApplicationClass 有问题的人。这是您的问题的答案(对于 Interop.Excel 参考,将“嵌入互操作类型”设置为“假”):stackoverflow.com/a/2483688/7202022
【解决方案2】:

使用ClosedXMLedit库(无需安装MS Excel

我只是写了一个简单的例子来告诉你如何命名文件、工作表和选择单元格:

    var workbook = new XLWorkbook();
    workbook.AddWorksheet("sheetName");
    var ws = workbook.Worksheet("sheetName");

    int row = 1;
    foreach (object item in itemList)
    {
        ws.Cell("A" + row.ToString()).Value = item.ToString();
        row++;
    }

    workbook.SaveAs("yourExcel.xlsx");

如果您愿意,您可以创建一个包含所有数据的 System.Data.DataSet 或 System.Data.DataTable,然后使用 workbook.AddWorksheet(yourDataset)workbook.AddWorksheet(yourDataTable) 将其添加为工作集;

【讨论】:

  • 我见过的关于 Excel 写作的最简单优雅的解决方案,干杯!
  • 获取“无法访问此站点”,请问我可以获取一个完整的链接吗?
  • 旧链接今天无法访问,我认为这可能会有所帮助:github.com/modulexcite/ClosedXML
【解决方案3】:

使用 CSV 的想法,如果它只是一个字符串列表。假设 l 是您的列表:

using System.IO;

using(StreamWriter sw = File.CreateText("list.csv"))
{
  for(int i = 0; i < l.Count; i++)
  {
    sw.WriteLine(l[i]);
  }
}

【讨论】:

  • 自定义对象会写类名
  • 我只是提到 csv 不是 excel 文件。如果您需要 xls 文件,请使用 Matthew 回答的其他内容;)
【解决方案4】:

使用 ClosedXml 的最简单方法。

Imports ClosedXML.Excel

var dataList = new List<string>() { "a", "b", "c" };
var workbook = new XLWorkbook();     //creates the workbook
var wsDetailedData = workbook.AddWorksheet("data"); //creates the worksheet with sheetname 'data'
wsDetailedData.Cell(1, 1).InsertTable(dataList); //inserts the data to cell A1 including default column name
workbook.SaveAs(@"C:\data.xlsx"); //saves the workbook

更多信息,您还可以查看 ClosedXml 的 wiki。 https://github.com/closedxml/closedxml/wiki

【讨论】:

  • 简洁优雅!像魅力一样工作。
【解决方案5】:

快捷方式-ArrayToExcel (github)

byte[] excel = myList.ToExcel();
File.WriteAllBytes("result.xlsx", excel);

【讨论】:

  • 很遗憾一年没有更新。所以看起来它的用户很少并且维护得不好
【解决方案6】:

将值列表导出到 Excel

  1. 在 nuget 下一个参考中安装
  2. 安装包 Syncfusion.XlsIO.Net.Core -版本 17.2.0.35
  3. Install-Package ClosedXML -Version 0.94.2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ClosedXML;
using ClosedXML.Excel;
using Syncfusion.XlsIO;

namespace ExporteExcel
{
    class Program
    {
        public class Auto
        {
            public string Marca { get; set; }

            public string Modelo { get; set; }
            public int Ano { get; set; }

            public string Color { get; set; }
            public int Peronsas { get; set; }
            public int Cilindros { get; set; }
        }
        static void Main(string[] args)
        {
            //Lista Estatica
            List<Auto> Auto = new List<Program.Auto>()
            {
                new Auto{Marca = "Chevrolet", Modelo = "Sport", Ano = 2019, Color= "Azul", Cilindros=6, Peronsas= 4 },
                new Auto{Marca = "Chevrolet", Modelo = "Sport", Ano = 2018, Color= "Azul", Cilindros=6, Peronsas= 4 },
                new Auto{Marca = "Chevrolet", Modelo = "Sport", Ano = 2017, Color= "Azul", Cilindros=6, Peronsas= 4 }
            };
            //Inizializar Librerias
            var workbook = new XLWorkbook();
            workbook.AddWorksheet("sheetName");
            var ws = workbook.Worksheet("sheetName");
            //Recorrer el objecto
            int row = 1;
            foreach (var c in Auto)
            {
                //Escribrie en Excel en cada celda
                ws.Cell("A" + row.ToString()).Value = c.Marca;
                ws.Cell("B" + row.ToString()).Value = c.Modelo;
                ws.Cell("C" + row.ToString()).Value = c.Ano;
                ws.Cell("D" + row.ToString()).Value = c.Color;
                ws.Cell("E" + row.ToString()).Value = c.Cilindros;
                ws.Cell("F" + row.ToString()).Value = c.Peronsas;
                row++;

            }
            //Guardar Excel 
            //Ruta = Nombre_Proyecto\bin\Debug
            workbook.SaveAs("Coches.xlsx");
        }
    }
}

【讨论】:

  • 这很好用,但是如何添加列标题?
【解决方案7】:

您可以将它们输出到.csv file 并在 excel 中打开文件。够直接吗?

【讨论】:

    【解决方案8】:

    (在我看来)最直接的方法是简单地将 CSV 文件放在一起。如果您想进行格式化并实际写入 *.xlsx 文件,有更复杂的解决方案(和APIs)可以为您完成。

    【讨论】:

      【解决方案9】:

      一种简单的方法是打开 Excel 创建包含要导出的测试数据的工作表,然后说 excel 另存为 xml 打开 xml 查看 excel 所期望的 xml 格式并通过头部替换测试数据来生成它导出数据

      SpreadsheetML Markup Spec

      @lan 这是一个简单的 execel 文件的 xml 文件,其中有一列值我用 office 2003 生成的这种格式适用于 office 2003 及更高版本

      <?xml version="1.0"?>
      <?mso-application progid="Excel.Sheet"?>
      <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
       xmlns:o="urn:schemas-microsoft-com:office:office"
       xmlns:x="urn:schemas-microsoft-com:office:excel"
       xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
       xmlns:html="http://www.w3.org/TR/REC-html40">
       <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
        <Author>Dancho</Author>
        <LastAuthor>Dancho</LastAuthor>
        <Created>2010-02-05T10:15:54Z</Created>
        <Company>cc</Company>
        <Version>11.9999</Version>
       </DocumentProperties>
       <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
        <WindowHeight>13800</WindowHeight>
        <WindowWidth>24795</WindowWidth>
        <WindowTopX>480</WindowTopX>
        <WindowTopY>105</WindowTopY>
        <ProtectStructure>False</ProtectStructure>
        <ProtectWindows>False</ProtectWindows>
       </ExcelWorkbook>
       <Styles>
        <Style ss:ID="Default" ss:Name="Normal">
         <Alignment ss:Vertical="Bottom"/>
         <Borders/>
         <Font/>
         <Interior/>
         <NumberFormat/>
         <Protection/>
        </Style>
       </Styles>
       <Worksheet ss:Name="Sheet1">
        <Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="6" x:FullColumns="1"
         x:FullRows="1">
         <Row>
          <Cell><Data ss:Type="String">Value1</Data></Cell>
         </Row>
         <Row>
          <Cell><Data ss:Type="String">Value2</Data></Cell>
         </Row>
         <Row>
          <Cell><Data ss:Type="String">Value3</Data></Cell>
         </Row>
         <Row>
          <Cell><Data ss:Type="String">Value4</Data></Cell>
         </Row>
         <Row>
          <Cell><Data ss:Type="String">Value5</Data></Cell>
         </Row>
         <Row>
          <Cell><Data ss:Type="String">Value6</Data></Cell>
         </Row>
        </Table>
        <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
         <Selected/>
         <Panes>
          <Pane>
           <Number>3</Number>
           <ActiveRow>5</ActiveRow>
          </Pane>
         </Panes>
         <ProtectObjects>False</ProtectObjects>
         <ProtectScenarios>False</ProtectScenarios>
        </WorksheetOptions>
       </Worksheet>
       <Worksheet ss:Name="Sheet2">
        <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
         <ProtectObjects>False</ProtectObjects>
         <ProtectScenarios>False</ProtectScenarios>
        </WorksheetOptions>
       </Worksheet>
       <Worksheet ss:Name="Sheet3">
        <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
         <ProtectObjects>False</ProtectObjects>
         <ProtectScenarios>False</ProtectScenarios>
        </WorksheetOptions>
       </Worksheet>
      </Workbook>
      

      【讨论】:

      • Office Open XML 实际上是一个包含多个 XML 文件的 zip 文件。这并不像你暗示的那么简单。
      • Matthew,Office 2003 中引入了 SpreadsheetML。这不是一个 zip 文件,实际上并不难使用,我最近为它编写了一个解析器。
      • 我正在参考 Microsoft excel,在其中创建工作表时,您可以转到“另存为”菜单并选择将其保存为 xml 文件。打开 xml 文件复制硬编码的部分并用真实数据替换测试数据很容易我已经用 MS Office 2000 完成了数百万次,2003 和 2007 我没有测试过但它必须是相似的如果你觉得给我excel文件很难,我可以写一些测试程序。
      • @lordanTanev:我已经为你正在谈论的 XML 添加了规范的链接。
      • @lan 我从 xml 添加了一个示例,我正在谈论它来自 Excel 2003
      【解决方案10】:

      根据您要在其中执行此操作的环境,可以使用 Excel 互操作。然而,处理 COM 并确保您清理资源,否则 Excel 实例会留在您的计算机上,这很麻烦。

      如果您想了解更多信息,请查看此MSDN Example

      根据您的格式,您可以自己生成 CSV 或 SpreadsheetML,这并不难。其他替代方法是使用 3rd 方库来执行此操作。显然它们是要花钱的。

      【讨论】:

        【解决方案11】:
        List<"classname"> getreport = cs.getcompletionreport(); 
        
        var getreported = getreport.Select(c => new { demographic = c.rName);   
        

        其中cs.getcompletionreport() 参考类文件是应用程序的业务层
        我希望这会有所帮助。

        【讨论】:

        • 因为它看起来像这样 i.imgur.com/5ngtPac.png 我希望你同意这样更容易阅读并在视觉上将代码与答案的其余部分分开
        【解决方案12】:

        我知道,我参加这个聚会迟到了,但我认为这可能对其他人有所帮助。

        已经发布的答案是针对 csv 的,另一个是由 Interop dll 提供的,您需要在服务器上安装 excel,每种方法都有其优缺点。 这是一个可以为您提供的选项

        1. 完美的 excel 输出 [不是 csv]
        2. 完美的 Excel 和您的数据类型匹配
        3. 无需安装 excel
        4. 通过列表并获取 Excel 输出 :)

        您可以通过使用NPOI DLL 来实现这一点,它适用于 .net 和 .net 核心

        步骤:

        1. 导入 NPOI DLL
        2. 添加下面提供的第 1 节和第 2 节代码
        3. 很好

        第 1 节

        此代码执行以下任务:

        1. 创建新的 Excel 对象 - _workbook = new XSSFWorkbook();
        2. 创建新的 Excel 工作表对象 - _sheet =_workbook.CreateSheet(_sheetName);
        3. 调用WriteData() - 稍后解释最后,创建和
        4. 返回 MemoryStream 对象

        ================================================ ===============================

        using NPOI.SS.UserModel;
        using NPOI.XSSF.UserModel;
        using System;
        using System.Collections.Generic;
        using System.IO;
        using System.Net;
        using System.Net.Http;
        using System.Net.Http.Headers;
        
        namespace GenericExcelExport.ExcelExport
        {
            public interface IAbstractDataExport
            {
                HttpResponseMessage Export(List exportData, string fileName, string sheetName);
            }
        
            public abstract class AbstractDataExport : IAbstractDataExport
            {
                protected string _sheetName;
                protected string _fileName;
                protected List _headers;
                protected List _type;
                protected IWorkbook _workbook;
                protected ISheet _sheet;
                private const string DefaultSheetName = "Sheet1";
        
                public HttpResponseMessage Export
                      (List exportData, string fileName, string sheetName = DefaultSheetName)
                {
                    _fileName = fileName;
                    _sheetName = sheetName;
        
                    _workbook = new XSSFWorkbook(); //Creating New Excel object
                    _sheet = _workbook.CreateSheet(_sheetName); //Creating New Excel Sheet object
        
                    var headerStyle = _workbook.CreateCellStyle(); //Formatting
                    var headerFont = _workbook.CreateFont();
                    headerFont.IsBold = true;
                    headerStyle.SetFont(headerFont);
        
                    WriteData(exportData); //your list object to NPOI excel conversion happens here
        
                    //Header
                    var header = _sheet.CreateRow(0);
                    for (var i = 0; i < _headers.Count; i++)
                    {
                        var cell = header.CreateCell(i);
                        cell.SetCellValue(_headers[i]);
                        cell.CellStyle = headerStyle;
                    }
        
                    for (var i = 0; i < _headers.Count; i++)
                    {
                        _sheet.AutoSizeColumn(i);
                    }
        
                    using (var memoryStream = new MemoryStream()) //creating memoryStream
                    {
                        _workbook.Write(memoryStream);
                        var response = new HttpResponseMessage(HttpStatusCode.OK)
                        {
                            Content = new ByteArrayContent(memoryStream.ToArray())
                        };
        
                        response.Content.Headers.ContentType = new MediaTypeHeaderValue
                               ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                        response.Content.Headers.ContentDisposition = 
                               new ContentDispositionHeaderValue("attachment")
                        {
                            FileName = $"{_fileName}_{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx"
                        };
        
                        return response;
                    }
                }
        
                //Generic Definition to handle all types of List
                public abstract void WriteData(List exportData);
            }
        }
        

        ================================================ ===============================

        第 2 节

        在第 2 部分,我们将执行以下步骤:

        1. 将列表转换为 DataTable 反射以读取属性名称,您的
        2. 列标题将来自这里
        3. 循环遍历 DataTable 以创建 Excel 行

        ================================================ ===============================

        using NPOI.SS.UserModel;
        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Data;
        using System.Text.RegularExpressions;
        
        namespace GenericExcelExport.ExcelExport
        {
            public class AbstractDataExportBridge : AbstractDataExport
            {
                public AbstractDataExportBridge()
                {
                    _headers = new List<string>();
                    _type = new List<string>();
                }
        
                public override void WriteData<T>(List<T> exportData)
                {
                    PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
        
                    DataTable table = new DataTable();
        
                    foreach (PropertyDescriptor prop in properties)
                    {
                        var type = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
                        _type.Add(type.Name);
                        table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? 
                                          prop.PropertyType);
                        string name = Regex.Replace(prop.Name, "([A-Z])", " $1").Trim(); //space separated 
                                                                                   //name by caps for header
                        _headers.Add(name);
                    }
        
                    foreach (T item in exportData)
                    {
                        DataRow row = table.NewRow();
                        foreach (PropertyDescriptor prop in properties)
                            row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                        table.Rows.Add(row);
                    }
        
                    IRow sheetRow = null;
        
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        sheetRow = _sheet.CreateRow(i + 1);
                        for (int j = 0; j < table.Columns.Count; j++)
                        {
                            ICell Row1 = sheetRow.CreateCell(j);
        
                            string type = _type[j].ToLower();
                            var currentCellValue = table.Rows[i][j];
        
                            if (currentCellValue != null && 
                                !string.IsNullOrEmpty(Convert.ToString(currentCellValue)))
                            {
                                if (type == "string")
                                {
                                    Row1.SetCellValue(Convert.ToString(currentCellValue));
                                }
                                else if (type == "int32")
                                {
                                    Row1.SetCellValue(Convert.ToInt32(currentCellValue));
                                }
                                else if (type == "double")
                                {
                                    Row1.SetCellValue(Convert.ToDouble(currentCellValue));
                                }
                            }
                            else
                            {
                                Row1.SetCellValue(string.Empty);
                            }
                        }
                    }
                }
            }
        }
        

        ================================================ ===============================

        现在你只需要打电话 WriteData() 函数通过传递您的列表,它将为您提供您的 excel。

        我已经在 WEB API 和 WEB API Core 中对其进行了测试,效果很好。

        【讨论】:

        • 这样的表现如何?只是浏览代码我猜它很慢?
        • 我们在 3 个应用程序中使用它,性能相当令人满意,但是它确实有一些改进的地方
        【解决方案13】:

        将列表传递给“Write”方法,将列表转换为缓冲区并返回缓冲区,将下载一个文件

        byte[] buffer = Write(ListData, true, "AttendenceSummary"); return File(buffer, "application/excel", reportTitle + ".xlsx");
        
              public static byte[] Write<T>(IEnumerable<T> list, bool xlsxExtension, string sheetName = "ExportData")
                {
                    if (list == null)
                    {
                        throw new ArgumentNullException("list");
                    }
        
                    XSSFWorkbook hssfworkbook = new XSSFWorkbook();
                    int Rowspersheet = 15000;
                    int TotalRows = list.Count();
                    int TotalSheets = TotalRows / Rowspersheet;
        
                    for (int i = 0; i <= TotalSheets; i++)
                    {
                        ISheet sheet1 = hssfworkbook.CreateSheet(sheetName + "_" + i);
                        IRow row = sheet1.CreateRow(0);
                        int index = 0;
                        foreach (PropertyInfo property in typeof(T).GetProperties())
                        {
                            ICellStyle cellStyle = hssfworkbook.CreateCellStyle();
                            IFont cellFont = hssfworkbook.CreateFont();
        
                            cellFont.Boldweight = (short)NPOI.SS.UserModel.FontBoldWeight.Bold;
                            cellStyle.SetFont(cellFont);
        
                            ICell cell = row.CreateCell(index++);
                            cell.CellStyle = cellStyle;
                            cell.SetCellValue(property.Name);
                        }
        
                        int rowIndex = 1;
                        // int rowIndex2 = 1;
        
                        foreach (T obj in list.Skip(Rowspersheet * i).Take(Rowspersheet))
                        {
        
                            row = sheet1.CreateRow(rowIndex++);
                            index = 0;
        
                            foreach (PropertyInfo property in typeof(T).GetProperties())
                            {
                                ICell cell = row.CreateCell(index++);
                                cell.SetCellValue(Convert.ToString(property.GetValue(obj)));
                            }
        
                        }
                    }
        
                    MemoryStream file = new MemoryStream();
                    hssfworkbook.Write(file);
                    return file.ToArray();
        
                }
        

        【讨论】:

          【解决方案14】:

          三个有用的相关代码sn-ps:

          1. 带有 EPPlus 的 ListToExcel 的 C# 扩展方法:

          public static void ListToExcel<T>(this IList<T> list, string filename, bool isRtl)
              {
                  var response = HttpContext.Current.Response;
          
                  var path = HttpUtility.UrlEncode(filename + ".xlsx", System.Text.Encoding.UTF8);
          
                  using (ExcelPackage pck = new ExcelPackage())
                  {
                      ExcelWorksheet worksheet = pck.Workbook.Worksheets.Add(filename);
                      worksheet.Cells["A1"].LoadFromCollection<T>(list, true);
          
                      //--------------------------------------------Create Rtl
          
                      if (isRtl)
                          worksheet.View.RightToLeft = true;
          
                      //--------------------------------------------Create AutoFilter
          
                      worksheet.Cells[worksheet.Dimension.Address].AutoFilter = true;
          
                      //--------------------------------------------Create Format as table
                      //create a range for the table
                      ExcelRange range = worksheet.Cells[1, 1, worksheet.Dimension.End.Row, worksheet.Dimension.End.Column];
                      //add a table to the range
                      var tab = worksheet.Tables.Add(range, "Table1");
                      //format the table
                      tab.TableStyle = TableStyles.Medium2;
          
                      //--------------------------------------------
          
                      var ms = new System.IO.MemoryStream();
                      pck.SaveAs(ms);
                      ms.WriteTo(response.OutputStream);
          
                      response.Clear();
                      //response.ContentType = "application/vnd.ms-excel"; //xls
                      response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; //xlsx                
                      response.AddHeader("content-disposition", String.Format("attachment;filename={0}", Path.GetFileName(path)));
                      response.BinaryWrite(pck.GetAsByteArray());
                      response.Flush();
                      response.End();
                  }
          

          注意:使用“Install-Package EPPlus”安装 EPPlus

          1. 使用 Microsoft.Office.Interop.Excel 的 ListToExcel 的 C# 扩展方法:

           public static void ListToExcel<T>(this IList<T> list, string filename)
              {
                  Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
                  Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
                  Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
          
                  app.Visible = false;
                  worksheet = workbook.Sheets["Sheet1"];
          
                  worksheet = workbook.ActiveSheet;
                  worksheet.Name = filename;
          
          
                  PropertyInfo[] properties = typeof(T).GetProperties();
          
                  for (int i = 0; i < properties.Length; i++)
                  {
                      worksheet.Cells[1, i + 1] = properties[i].Name;
                      worksheet.Cells[1, i + 1].AutoFilter();
                  }
          
                  for (int i = 0; i < list.Count; i++)
                  {
                      var item = list[i];
          
                      for (int j = 0; j < properties.Length; j++)
                      {
                          var prop = properties[j];
                          worksheet.Cells[i + 2, j + 1] = prop.GetValue(item);
                      }
                  }
          
                  var path = System.Web.HttpContext.Current.Server.MapPath("~/" + filename + ".xlsx");
          
                  if (File.Exists(path))
                      File.Delete(path);
          
                  worksheet.SaveAs(path);
                  workbook.Close();
                  app.Quit();
          
                  var response = HttpContext.Current.Response;
          
                  using (FileStream fs = File.OpenRead(path))
                  {
                      int length = (int)fs.Length;
                      byte[] buffer;
          
                      using (BinaryReader br = new BinaryReader(fs))
                      {
                          buffer = br.ReadBytes(length);
                      }
          
                      response.Clear();
                      response.Buffer = true;
                      //response.ContentType = "application/vnd.ms-excel"; //xls
                      response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; //xlsx                
                      response.AddHeader("content-disposition", String.Format("attachment;filename={0}", Path.GetFileName(path)));
                      response.BinaryWrite(buffer);
                      response.Flush();
                      response.End();
                  }
              }
          

          注意:如果您使用 Microsoft.Office.Interop.Excel,必须在客户端安装 Office。

          1. ListToCsv 的 C# 扩展方法:

          public static void ListToCsv<T>(this IList<T> list, string filename)
              {
                  string StringHeaders = "";
                  string StringRows = "";
          
                  PropertyInfo[] properties = typeof(T).GetProperties();
                  for (int i = 0; i < properties.Length - 1; i++)
                  {
                      StringHeaders += properties[i].Name + ",";
                  }
                  var lastPropName = properties[properties.Length - 1].Name;
                  StringHeaders += lastPropName + Environment.NewLine;
          
          
                  foreach (var item in list)
                  {
                      for (int i = 0; i < properties.Length - 1; i++)
                      {
                          var prop = properties[i];
                          StringRows += prop.GetValue(item) + ",";
                      }
                      var lastPropInfo = properties[properties.Length - 1];
                      StringRows += lastPropInfo.GetValue(item) + Environment.NewLine;
                  }
          
                  var response = HttpContext.Current.Response;
                  response.Clear();
                  response.AddHeader("content-disposition", "attachment; filename=" + filename + ".csv");
                  response.AddHeader("content-type", "text/csv");
          
                  using (StreamWriter writer = new StreamWriter(response.OutputStream, Encoding.UTF8))
                  {
                      writer.WriteLine(StringHeaders + StringRows);
                  }
          
                  response.Flush();
                  response.End();
              }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-07-30
            • 1970-01-01
            • 1970-01-01
            • 2018-10-15
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多