如何在没有Microsoft.Office.Interop.Excel.dll的情况下导出列表到excel

本文关键字:情况下 列表 excel dll Excel Microsoft Interop Office | 更新日期: 2023-09-27 17:52:56

我有一个foo列表如下。如何在没有Microsoft.Office.Interop.Excel.dll的情况下导出到excel

public class Foo
{
    public DateTime Column1 { get; set; }
    public string Column2 { get; set; }
    public decimal Column3 { get; set; }
    public bool Column4 { get; set; }
}

如何在没有Microsoft.Office.Interop.Excel.dll的情况下导出列表到excel

您可以使用ClosedXML库(https://closedxml.codeplex.com/)

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

        var workbook = new XLWorkbook();
        workbook.AddWorksheet("sheetName");
        var ws = workbook.Worksheet("sheetName");
        int row = 1;
        foreach (Foo f in fooList)
        {
            string rowString = row.ToString();
            ws.Cell("A" + rowString).Value = f.Column1;
            ws.Cell("B" + rowString).Value = f.Column2;
            ws.Cell("C" + rowString).Value = f.Column3;
            ws.Cell("D" + rowString).Value = f.Column4;
            row++;
        }
        workbook.SaveAs("yourExcel.xlsx");

如果你喜欢,你可以创建一个System.Data.DataSet或System.Data.DataTable与所有的数据,然后添加它作为一个工作表与workbook.AddWorksheet(yourDataset)workbook.AddWorksheet(yourDataTable);

使用c#将列表导出到excel的超级简单方法

如何使用NuGet Packager Manager Console安装ClosedXML: PM> Get-Project [ProjectName] | install - package ClosedXML

        using (var conn = new DB.UpdatesEntities())
        {
            var stories = (from a in conn.Subscribers
                           orderby a.DT descending
                           select a).Take(100).ToList();
            var ShowHeader = true;
            PropertyInfo[] properties = stories.First().GetType().GetProperties();
            List<string> headerNames = properties.Select(prop => prop.Name).ToList();                
            var wb = new XLWorkbook();
            var ws = wb.Worksheets.Add("Subscribers");
            if (ShowHeader)
            {
                for (int i = 0; i < headerNames.Count; i++)                    
                    ws.Cell(1, i + 1).Value = headerNames[i];
                ws.Cell(2, 1).InsertData(stories);
            }
            else
            {
                ws.Cell(1, 1).InsertData(stories);
            }
            wb.SaveAs(@"C:'Testing'yourExcel.xlsx");
        }

桌面应用

    public static void WriteListToExcel<T>(List<T> list, string fulllPath)
    {
        try
        {
            List<string> result = new List<string>();
            result.Add(String.Join(String.Empty, typeof(T).GetProperties().Select(i => String.Format("{0}'t", i.Name)))); // Headers
            result.AddRange(list.Select(i => String.Join("'t", i.GetType().GetProperties().Select(t => t.GetValue(i, null))))); // Lines
            File.WriteAllLines(fulllPath, result);
        }
        catch (Exception e)
        {
            // Error do what you want....
        }
    }

Web应用

    public static void WriteListToExcel<T>(HttpResponseBase Response, List<T> list, string fileName)
    {
        try
        {
            Response.Clear();
            Response.AddHeader("content-disposition", String.Format("attachment;filename={0}.xls", fileName));
            Response.Charset = String.Empty;
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/vnd.ms-excel";
            List<string> result = new List<string>();
            result.Add(String.Format("{0}'n", String.Join(String.Empty, typeof(T).GetProperties().Select(i => String.Format("{0}'t", i.Name))))); // Headers
            result.AddRange(list.Select(i => String.Format("{0}'n",String.Join("'t", i.GetType().GetProperties().Select(t => t.GetValue(i, null)))))); // Lines 
            result.ForEach(i => Response.Write(i));
            Response.Flush();
            Response.End();
        }
        catch (Exception e)
        {
            // Error..
        }
    }
相关文章: