C# 读取大型XLSX文件
我有一个应用程序,必须读取excel并将其转换为数组。到现在为止,一直都还不错。在我尝试转换一个更大的文件之前,所有文件都可以正常工作。我尝试了OpenXML和SAX方法:C# 读取大型XLSX文件,c#,asp.net,.net,excel,web-services,C#,Asp.net,.net,Excel,Web Services,我有一个应用程序,必须读取excel并将其转换为数组。到现在为止,一直都还不错。在我尝试转换一个更大的文件之前,所有文件都可以正常工作。我尝试了OpenXML和SAX方法: using (SpreadsheetDocument xlsx = SpreadsheetDocument.Open(filePath, false)) { WorkbookPart workbookPart = xlsx.WorkbookPart; List<List<string>>
using (SpreadsheetDocument xlsx = SpreadsheetDocument.Open(filePath, false))
{
WorkbookPart workbookPart = xlsx.WorkbookPart;
List<List<string>> parsedContent = new List<List<string>>();
foreach (WorksheetPart worksheet in workbookPart.WorksheetParts)
{
OpenXmlReader xlsxReader = OpenXmlReader.Create(worksheet);
while (xlsxReader.Read())
{
}
}
}
IExcelDataReader reader = null;
string FilePath = "PathToExcelFile";
//Load file into a stream
FileStream stream = File.Open(FilePath, FileMode.Open, FileAccess.Read);
//Must check file extension to adjust the reader to the excel file type
if (Path.GetExtension(FilePath).Equals(".xls"))
reader = ExcelReaderFactory.CreateBinaryReader(stream);
else if (Path.GetExtension(FilePath).Equals(".xlsx"))
reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
if (reader != null)
{
//Fill DataSet
DataSet content = reader.AsDataSet();
//Read....
}
使用(SpreadsheetDocument xlsx=SpreadsheetDocument.Open(filePath,false))
{
WorkbookPart WorkbookPart=xlsx.WorkbookPart;
List parsedContent=新列表();
foreach(workbookPart.WorksheetParts中的工作表部件工作表)
{
OpenXmlReader xlsxReader=OpenXmlReader.Create(工作表);
while(xlsxReader.Read())
{
}
}
}
这对于1-10MB范围内的文件很有效。我的问题是当我尝试加载10+MB文件时。结果是OutOfMemoryException。如何正确读取那大块数据?如何提高内存利用率
IExcelDataReader reader = null;
string FilePath = "PathToExcelFile";
//Load file into a stream
FileStream stream = File.Open(FilePath, FileMode.Open, FileAccess.Read);
//Must check file extension to adjust the reader to the excel file type
if (Path.GetExtension(FilePath).Equals(".xls"))
reader = ExcelReaderFactory.CreateBinaryReader(stream);
else if (Path.GetExtension(FilePath).Equals(".xlsx"))
reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
if (reader != null)
{
//Fill DataSet
DataSet content = reader.AsDataSet();
//Read....
}
另外,我尝试使用ClosedXML、EPPlus等库
IExcelDataReader reader = null;
string FilePath = "PathToExcelFile";
//Load file into a stream
FileStream stream = File.Open(FilePath, FileMode.Open, FileAccess.Read);
//Must check file extension to adjust the reader to the excel file type
if (Path.GetExtension(FilePath).Equals(".xls"))
reader = ExcelReaderFactory.CreateBinaryReader(stream);
else if (Path.GetExtension(FilePath).Equals(".xlsx"))
reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
if (reader != null)
{
//Fill DataSet
DataSet content = reader.AsDataSet();
//Read....
}
每一个解决方案都将受到赞赏。提前感谢如果您计划只读取excel文件内容,我建议您使用ExcelDataReader库,它将工作表数据提取到数据集对象中
IExcelDataReader reader = null;
string FilePath = "PathToExcelFile";
//Load file into a stream
FileStream stream = File.Open(FilePath, FileMode.Open, FileAccess.Read);
//Must check file extension to adjust the reader to the excel file type
if (Path.GetExtension(FilePath).Equals(".xls"))
reader = ExcelReaderFactory.CreateBinaryReader(stream);
else if (Path.GetExtension(FilePath).Equals(".xlsx"))
reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
if (reader != null)
{
//Fill DataSet
DataSet content = reader.AsDataSet();
//Read....
}
使用。通过Nuget很容易安装,只需要几行代码:
IExcelDataReader reader = null;
string FilePath = "PathToExcelFile";
//Load file into a stream
FileStream stream = File.Open(FilePath, FileMode.Open, FileAccess.Read);
//Must check file extension to adjust the reader to the excel file type
if (Path.GetExtension(FilePath).Equals(".xls"))
reader = ExcelReaderFactory.CreateBinaryReader(stream);
else if (Path.GetExtension(FilePath).Equals(".xlsx"))
reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
if (reader != null)
{
//Fill DataSet
DataSet content = reader.AsDataSet();
//Read....
}
努吉:
IExcelDataReader reader = null;
string FilePath = "PathToExcelFile";
//Load file into a stream
FileStream stream = File.Open(FilePath, FileMode.Open, FileAccess.Read);
//Must check file extension to adjust the reader to the excel file type
if (Path.GetExtension(FilePath).Equals(".xls"))
reader = ExcelReaderFactory.CreateBinaryReader(stream);
else if (Path.GetExtension(FilePath).Equals(".xlsx"))
reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
if (reader != null)
{
//Fill DataSet
DataSet content = reader.AsDataSet();
//Read....
}
Install-Package ExcelDataReader
用法:
IExcelDataReader reader = null;
string FilePath = "PathToExcelFile";
//Load file into a stream
FileStream stream = File.Open(FilePath, FileMode.Open, FileAccess.Read);
//Must check file extension to adjust the reader to the excel file type
if (Path.GetExtension(FilePath).Equals(".xls"))
reader = ExcelReaderFactory.CreateBinaryReader(stream);
else if (Path.GetExtension(FilePath).Equals(".xlsx"))
reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
if (reader != null)
{
//Fill DataSet
DataSet content = reader.AsDataSet();
//Read....
}
using (FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read))
{
using (IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream))
{
DataSet result = excelReader.AsDataSet();
foreach (DataRow dr in result[0])
{
//Do stuff
}
}
}
可能重复检查此:@VDWWD不是一个好的重复-几乎所有答案都是bad@Vasil为什么要使用1000万个元素的列表?这是导致问题的原因,而不是Excel。当你把所有东西都保存在内存中时,使用阅读器是没有意义的。只有当您立即处理数据时,读卡器才有意义。您是如何处理这些行的,为什么要将它们存储在
列表中?你不应该使用数组吗?一个代码示例将大大提高这个答案的质量,它就像魔术一样。这应该被接受为答案