介绍
高效读取大型 excel 文件可能具有挑战性,尤其是在处理需要高性能和可扩展性的应用程序时。microsoft 的 openxml sdk 提供了一套强大的工具来处理 office 文档(包括 excel 文件),而无需在服务器上安装 excel。本文将指导您使用 c# 和 openxml 高效读取大型 excel 文件。
为何使用 openxml
openxml 是办公文档(word、excel、powerpoint)的开放标准,允许以编程方式操作这些文档。使用 openxml 的一些好处包括:
性能:它直接对文件流进行操作,而无需将整个文档加载到内存中。
无依赖性:无需安装 microsoft office。
可扩展性:非常适合服务器端应用程序和批处理。
先决条件
在深入研究代码之前,请确保您已具备以下内容。
visual studio 或任何 c# ide
.net framework 或 .net core sdk
openxml sdk:您可以使用命令通过 nuget 安装它。
install-package documentformat.openxml
使用 openxml 读取大型 excel 文件
以下是使用 c# 和 openxml 读取大型 excel 文件的分步指南。
1. 设置项目
创建一个新的 c# 控制台应用程序。
打开 visual studio 并创建一个新的控制台应用程序(.net core 或 .net framework)。
通过 nuget 安装 openxml sdk。
2.打开 excel 文件
首先,您需要打开 excel 文件并访问要读取的工作表。使用以下代码打开 excel 文件。
using documentformat.openxml.packaging;
using documentformat.openxml.spreadsheet;
using system;
using system.collections.generic;
namespace readlargeexcelfile
{
class program
{
static void main(string[] args)
{
string filepath = "path/to/your/large/excelfile.xlsx";
using (spreadsheetdocument doc = spreadsheetdocument.open(filepath, false))
{
workbookpart workbookpart = doc.workbookpart;
sheet sheet = workbookpart.workbook.sheets.getfirstchild<sheet>();
worksheetpart worksheetpart = (worksheetpart)workbookpart.getpartbyid(sheet.id);
ienumerable<row> rows = worksheetpart.worksheet.getfirstchild<sheetdata>().elements<row>();
foreach (row row in rows)
{
foreach (cell cell in row.elements<cell>())
{
string cellvalue = getcellvalue(doc, cell);
console.write(cellvalue + " ");
}
console.writeline();
}
}
}
private static string getcellvalue(spreadsheetdocument doc, cell cell)
{
sharedstringtablepart stringtablepart = doc.workbookpart.sharedstringtablepart;
string value = cell.cellvalue.innerxml;
if (cell.datatype != null && cell.datatype.value == cellvalues.sharedstring)
{
return stringtablepart.sharedstringtable.childelements[int32.parse(value)].innertext;
}
else
{
return value;
}
}
}
}
3. 高效处理大文件
上述代码将整个工作表读入内存,这对于非常大的文件来说可能效率不高。为了更有效地处理大文件,请考虑分块处理文件或使用流式传输技术。
4. 优化性能
为了优化性能,您可以
流式传输文件:使用流式传输技术分部分处理文件,而不是将整个文件加载到内存中。
并行处理:如果您的应用程序允许,您可以并行处理文件的不同部分。
高效的数据结构:使用高效的数据结构来存储和处理数据。
下面是一个使用流式传输的示例。
using documentformat.openxml.packaging;
using documentformat.openxml.spreadsheet;
using system;
using system.collections.generic;
namespace readlargeexcelfile
{
class program
{
static void main(string[] args)
{
string filepath = "path/to/your/large/excelfile.xlsx";
using (spreadsheetdocument doc = spreadsheetdocument.open(filepath, false))
{
workbookpart workbookpart = doc.workbookpart;
sheet sheet = workbookpart.workbook.sheets.getfirstchild<sheet>();
worksheetpart worksheetpart = (worksheetpart)workbookpart.getpartbyid(sheet.id);
openxmlreader reader = openxmlreader.create(worksheetpart);
while (reader.read())
{
if (reader.elementtype == typeof(row))
{
row row = (row)reader.loadcurrentelement();
foreach (cell cell in row.elements<cell>())
{
string cellvalue = getcellvalue(doc, cell);
console.write(cellvalue + " ");
}
console.writeline();
}
}
}
}
private static string getcellvalue(spreadsheetdocument doc, cell cell)
{
sharedstringtablepart stringtablepart = doc.workbookpart.sharedstringtablepart;
string value = cell.cellvalue.innerxml;
if (cell.datatype != null && cell.datatype.value == cellvalues.sharedstring)
{
return stringtablepart.sharedstringtable.childelements[int32.parse(value)].innertext;
}
else
{
return value;
}
}
}
}
结论
使用 c# 和 openxml 读取大型 excel 文件为需要高性能和可扩展性的应用程序提供了强大的解决方案。通过遵循本文概述的做法,您可以高效地处理存储在 excel 文件中的大型数据集,从而使您的应用程序更高效、响应更快。openxml 无需安装 office 即可操作 office 文档,这使其成为任何开发人员工具包中的重要工具。
到此这篇关于使用c#和openxml读取大型excel文件的文章就介绍到这了,更多相关c# openxml读取excel内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论