C#将.xls转换为.csv而不使用Excel

需要将.xls或.xlsx转换为.csv,而无需在C#/ ASP.net Web应用程序中使用Excel。 该应用程序目前正在使用NPOI.dll来实现某些function,但我没有看到关于该特定function的NPOI的codeplex wiki上的任何信息。 有没有人有什么建议?

谢谢

有图书馆( Excel Data Reader ,例如)可以让你阅读excel。 一旦你能够读取数据,写入csv应该很简单。

ADODB.NET可用于将Excel文件视为数据源。

//string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0;"; string ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0;"; ConnectionString = string.Format(ConnectionString, @"FullPathToExcelFile"); OleDbConnection conn = new OleDbConnection(ConnectionString); conn.Open(); OleDbCommand cmdSelect = new OleDbCommand("SELECT * FROM [Sheet1$]", conn); OleDbDataAdapter oleDBAdapter = new OleDbDataAdapter(); oleDBAdapter.SelectCommand = cmdSelect; DataSet myDataset = new DataSet(); oleDBAdapter.Fill(myDataset); conn.Close(); // Do whatever with data in myDataset including export to csv... 

看看FileHelpers库。 它会完全按照你的意愿行事。

使用FileHelpers,您可以从Excel文件中读取并写入csv或平面文本文件。 它是面向对象的! 您所要做的就是使用某些属性注释类,以便它们与源excel文件匹配。

考虑这个例子:

 [DelimitedRecord("|")] public class CustomersVerticalBar { public string CustomerID; public string CompanyName; ... } 

阅读使用此:

 ExcelStorage provider = new ExcelStorage(typeof(CustomersVerticalBar)); provider.StartRow = 3; provider.StartColumn = 2; provider.FileName = "Customers.xls"; CustomerVerticalBar[] res = (CustomerVerticalBar[]) provider.ExtractRecords(); 

从这里取的示例: http : //filehelpers.sourceforge.net/example_exceldatalink.html

.xls是一种专有的二进制格式,无法以纯文本格式读取,因此您需要办公室或Libre Office或其他东西来读取… .xlsx是基于xml的格式,并且应该可以通过解析DOM来实现…但您仍然会手动迭代每个值并手动分隔等。您是否考虑过使用xslt?