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

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

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

  1. 你必须安装Excel。
  2. 将您的项目的引用添加到excel互操作dll。 要在.NET选项卡上执行此操作,请selectMicrosoft.Office.Interop.Excel。 这个名字可能有多个程序集。 select适合您的Visual Studio和Excel版本。
  3. 这里是一个代码示例来创build一个新的工作簿,并填写列表中的项目。

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 } 

使用CSV的想法,如果它只是一个string列表。 假设l是你的名单:

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

使用ClosedXML库(不需要安装MS Excel

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

  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"); 

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

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

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

根据你想要这样做的环境,可以使用Excel Interop。 然而,处理COM相当麻烦,并确保您清除资源,否则Excel实例将保持在您的计算机上。

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

根据你的格式,你可以自己生成CSV或SpreadsheetML,这不是太难。 其他的select是使用第三方库来做到这一点。 显然他们花钱。

一个简单的方法是打开Excel创build工作表,其中包含要导出的testing数据,然后说excel另存为xml打开xml请参阅Excel所期望的xml格式,并通过用导出数据replacetesting数据来生成它

SpreadsheetML标记规范

@lan这是xml的一个微软execel文件与一个列值我与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> 
 List<"classname"> getreport = cs.getcompletionreport(); var getreported = getreport.Select(c => new { demographic = c.rName); 

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

使用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维基。 https://github.com/closedxml/closedxml/wiki