c#-4.0 – 在C#中将xls和xlsx转换为Datatable
发布时间:2020-12-15 22:07:58 所属栏目:百科 来源:网络整理
导读:我正在努力将xls和xlsx转换为c#中的datatable.我用过这段代码. public DataTable ReadDataExcel(string filepath) { FileStream stream = File.Open(filepath,FileMode.Open,FileAccess.Read); IExcelDataReader excelReader = ExcelReaderFactory.CreateBin
我正在努力将xls和xlsx转换为c#中的datatable.我用过这段代码.
public DataTable ReadDataExcel(string filepath) { FileStream stream = File.Open(filepath,FileMode.Open,FileAccess.Read); IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream); excelReader.IsFirstRowAsColumnNames = true; DataSet result = excelReader.AsDataSet(); DataTable dt = new DataTable(); dt = result.Tables[0]; return dt; } 在xls的情况下,它的工作正常.每当我使用xlsx它不工作.它给出了“对象引用”错误. 是否有任何其他方法将xls和xlsx转换为datatable.我对使用不感兴趣 解决方法
这就是答案.
要导入xlsx, IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); 要导入xls, IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |