gpt4 book ai didi

excel - 如何使用 OpenXML Format SDK 从电子表格读取数据?

转载 作者:行者123 更新时间:2023-12-01 17:29:42 25 4
gpt4 key购买 nike

我需要使用 Open XML SDK 2.0 从 Excel 2007 工作簿中的单个工作表读取数据。我花了很多时间寻找执行此操作的基本指南,但我只找到了创建电子表格的帮助。

如何使用此 SDK 迭代工作表中的行,然后迭代每行中的单元格?

最佳答案

另一个答案似乎更像是元答案。我一直在努力解决这个问题,因为使用 LINQ 确实可以处理分离的文档部分。以下代码包含一个包装函数,用于从 Cell 中获取值,从而解析任何可能的字符串查找。

public void ExcelDocTest()
{
Debug.WriteLine("Running through sheet.");
int rowsComplete = 0;

using (SpreadsheetDocument spreadsheetDocument =
SpreadsheetDocument.Open(@"path\to\Spreadsheet.xlsx", false))
{
WorkbookPart workBookPart = spreadsheetDocument.WorkbookPart;

foreach (Sheet s in workBookPart.Workbook.Descendants<Sheet>())
{
WorksheetPart wsPart = workBookPart.GetPartById(s.Id) as WorksheetPart;
Debug.WriteLine("Worksheet {1}:{2} - id({0}) {3}", s.Id, s.SheetId, s.Name,
wsPart == null ? "NOT FOUND!" : "found.");

if (wsPart == null)
{
continue;
}

Row[] rows = wsPart.Worksheet.Descendants<Row>().ToArray();

//assumes the first row contains column names
foreach (Row row in wsPart.Worksheet.Descendants<Row>())
{
rowsComplete++;

bool emptyRow = true;
List<object> rowData = new List<object>();
string value;

foreach (Cell c in row.Elements<Cell>())
{
value = GetCellValue(c);
emptyRow = emptyRow && string.IsNullOrWhiteSpace(value);
rowData.Add(value);
}

Debug.WriteLine("Row {0}: {1}", row,
emptyRow ? "EMPTY!" : string.Join(", ", rowData));
}
}

}
Debug.WriteLine("Done, processed {0} rows.", rowsComplete);
}

public static string GetCellValue(Cell cell)
{
if (cell == null)
return null;
if (cell.DataType == null)
return cell.InnerText;

string value = cell.InnerText;
switch (cell.DataType.Value)
{
case CellValues.SharedString:
// For shared strings, look up the value in the shared strings table.
// Get worksheet from cell
OpenXmlElement parent = cell.Parent;
while (parent.Parent != null && parent.Parent != parent
&& string.Compare(parent.LocalName, "worksheet", true) != 0)
{
parent = parent.Parent;
}
if (string.Compare(parent.LocalName, "worksheet", true) != 0)
{
throw new Exception("Unable to find parent worksheet.");
}

Worksheet ws = parent as Worksheet;
SpreadsheetDocument ssDoc = ws.WorksheetPart.OpenXmlPackage as SpreadsheetDocument;
SharedStringTablePart sstPart = ssDoc.WorkbookPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault();

// lookup value in shared string table
if (sstPart != null && sstPart.SharedStringTable != null)
{
value = sstPart.SharedStringTable.ElementAt(int.Parse(value)).InnerText;
}
break;

//this case within a case is copied from msdn.
case CellValues.Boolean:
switch (value)
{
case "0":
value = "FALSE";
break;
default:
value = "TRUE";
break;
}
break;
}
return value;
}

编辑:谢谢@Nitin-Jadhav用于更正 GetCellValue()。

关于excel - 如何使用 OpenXML Format SDK 从电子表格读取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2624333/

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