将List中的值导出为ex​​cel

sus*_*osh 17 c# excel ms-office office-interop excel-interop

嗨,我有一个列表容器,其中包含值列表.我希望将列表值直接导出到Excel.有没有办法直接做到这一点?

nae*_*n84 20

好的,如果您想使用COM,这是一个循序渐进的指南.

  1. 您必须安装Excel.
  2. 将项目引用添加到excel interop 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
}
Run Code Online (Sandbox Code Playgroud)


Mat*_*hen 14

使用CSV的想法,如果它只是一个字符串列表.假设l是你的清单:

using System.IO;

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


小智 12

使用ClosedXML库(无需安装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");
Run Code Online (Sandbox Code Playgroud)

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


小智 6

使用 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
Run Code Online (Sandbox Code Playgroud)

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


小智 6

快速方法 - ArrayToExcel (github)

byte[] excel = myList.ToExcel();
File.WriteAllBytes("result.xlsx", excel);
Run Code Online (Sandbox Code Playgroud)