需要在C#/ASP.net Web应用程序中将.xls或.xlsx文件转换为.csv文件,而不使用Excel。该应用程序目前正在使用NPOI.dll实现某些功能,但我在codeplex wiki上没有找到有关该特定功能的任何信息。请问是否有任何建议?
谢谢。
谢谢。
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...
[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是一种专有的二进制格式,无法以纯文本格式读取,因此您需要使用Office或Libre Office或其他软件来读取它... .xlsx是基于XML的格式,应该可以通过解析DOM来实现...但您仍然需要手动迭代每个值并手动分隔等。您考虑过使用XSLT吗?