gpt4 book ai didi

c# - OpenXML、SAX 和简单地读取 Xlsx 文件

转载 作者:太空宇宙 更新时间:2023-11-03 13:54:10 26 4
gpt4 key购买 nike

我一直在努力寻找有关如何使用 OpenXml 读取大型 xlsx 文件的解决方案。我试过 Microsoft 示例,但运气不佳。我只需要将 excel 文件读入 c# 中的 DataTable。我不关心数据表中的值类型,一切都可以存储为字符串值。

目前我发现的示例不保留电子表格的结构,只返回单元格的值。

有什么想法吗?

最佳答案

open xml SDK 可能有点难以理解。但是,我发现使用 http://simpleooxml.codeplex.com/ 很有用这个代码丛项目。它在 sdk 上添加了一个薄层,以便更轻松地解析 excel 文件和使用样式。

然后你可以在他们的工作表阅读器上使用类似下面的东西来递归并获取你想要的值

System.IO.MemoryStream ms = Utility.StreamToMemory(xslxTemplate);
using (SpreadsheetDocument document = SpreadsheetDocument.Open(ms, true))
{
IEnumerable<Sheet> sheets = document.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>();
if (sheets.Count() == 0)
{
// The specified worksheet does not exist.
return null;
}
string relationshipId = sheets.First().Id.Value;
WorksheetPart worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(relationshipId);
string myval =WorksheetReader.GetCell("A", 0, worksheetPart).CellValue.InnerText;
// Put in a loop to go through contents of document
}

关于c# - OpenXML、SAX 和简单地读取 Xlsx 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12848230/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com