任何人都知道如何使用开放 XML SDK 2.0 获取 Excel 单元格值,知道行和列的位置,例如(A2),通过 Linq?
因此,Excel 直接存储单元格值,或者如果它是字符串,则存储在称为 SharedString 表的通用数据结构中。
在下面的示例中,我们首先使用 linq 获取特定地址处的单元格,然后将其传递给另一个函数以获取单元格的值。
另请注意,您必须在每个阶段添加空检查以确定行/列是否存在
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
namespace TestOpenXmlSDK
{
class Program
{
static void Main(string[] args)
{
string pathSource = @"D:\sample.xlsx";
using (FileStream fsSource = new FileStream(pathSource, FileMode.Open, FileAccess.Read))
{
byte[] bytes = new byte[fsSource.Length];
fsSource.Read(bytes, 0, (int)fsSource.Length);
using (MemoryStream mem = new MemoryStream())
{
mem.Write(bytes, 0, (int)bytes.Length);
using (SpreadsheetDocument excelDocument = SpreadsheetDocument.Open(mem, true))
{
var wbPart = excelDocument.WorkbookPart;
var wsPart = wbPart.WorksheetParts.First();
var sheetData = wsPart.Worksheet.GetFirstChild<SheetData>();
var cellValue = GetCellValue(GetCell(sheetData, "B2"), wbPart);
}
}
}
}
public static Cell GetCell(SheetData sheetData, string cellAddress)
{
uint rowIndex = uint.Parse(Regex.Match(cellAddress, @"[0-9]+").Value);
return sheetData.Descendants<Row>().FirstOrDefault(p => p.RowIndex == rowIndex).Descendants<Cell>().FirstOrDefault(p => p.CellReference == cellAddress);
}
public static string GetCellValue(Cell cell, WorkbookPart wbPart)
{
string value = cell.InnerText;
if (cell.DataType != null)
{
switch (cell.DataType.Value)
{
case CellValues.SharedString:
var stringTable = wbPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault();
if (stringTable != null)
{
value = stringTable.SharedStringTable.ElementAt(int.Parse(value)).InnerText;
}
break;
case CellValues.Boolean:
switch (value)
{
case "0":
value = "FALSE";
break;
default:
value = "TRUE";
break;
}
break;
}
}
return value;
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)