[英]open xml sdk 2.0 read excel cell value with formula
I am reading excel file using openXML sdk. 我正在使用openXML sdk阅读excel文件。 Some excel cells contains formula, I want to read the value of the cell but when I use the below code it is fetching me the formula.
一些Excel单元格包含公式,我想读取单元格的值,但是当我使用下面的代码时,它将获取公式。 How to get the value.
如何获得价值。
WorksheetPart wsPart = (WorksheetPart)(wbPart.GetPartById(theSheet.Id));
using(SpreadsheetDocument document = SpreadsheetDocument.Open(fileName, false))
{
WorkbookPart wbPart = document.WorkbookPart;
Sheet theSheet = wbPart.Workbook.Descendants<Sheet>().Where(s => s.Name == sheetName).FirstOrDefault();
WorksheetPart wsPart = (WorksheetPart)(wbPart.GetPartById(theSheet.Id));
Cell theCell = wsPart.Worksheet.Descendants<Cell>().Where(c => c.CellReference == "D16").FirstOrDefault();
value = theCell.InnerText;
}
Here I am reading particular cell "D16" which contains formula. 在这里,我正在阅读包含公式的特定单元格“ D16”。
Need one more help, not able to read Date for the cell. 需要更多帮助,无法读取该单元格的日期。 I am getting some number.
我有一些电话。 For Date cell Datatype is Null so below code not working.
对于日期单元格,数据类型为Null,因此以下代码不起作用。 HOw do I get date vale from the cell
我如何从牢房中获取日期谷
if (theCell != null)
{
value = theCell.CellValue.InnerText;
if (theCell.DataType != null)
{
switch (theCell.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;
}
}
}
try CellValue proerty of the cell. 尝试单元格的CellValue属性。
WorksheetPart wsPart = (WorksheetPart)(wbPart.GetPartById(theSheet.Id));
using(SpreadsheetDocument document = SpreadsheetDocument.Open(fileName, false))
{
WorkbookPart wbPart = document.WorkbookPart;
Sheet theSheet = wbPart.Workbook.Descendants<Sheet>().Where(s => s.Name == sheetName).FirstOrDefault();
WorksheetPart wsPart = (WorksheetPart)(wbPart.GetPartById(theSheet.Id));
Cell theCell = wsPart.Worksheet.Descendants<Cell>().Where(c => c.CellReference == "D16").FirstOrDefault();
value = theCell.CellValue;
}
I am assuming you are hardcoding the cell reference for debugging purposes. 我假设您是出于调试目的对单元格引用进行硬编码。 otherwise i would strongly advise against that.
否则,我强烈建议不要这样做。
Try this: 尝试这个:
WorksheetPart wsPart = (WorksheetPart)(wbPart.GetPartById(theSheet.Id));
using(SpreadsheetDocument document = SpreadsheetDocument.Open(fileName, false))
{
WorkbookPart wbPart = document.WorkbookPart;
Sheet theSheet = wbPart.Workbook.Descendants<Sheet>().Where(s => s.Name == sheetName).FirstOrDefault();
WorksheetPart wsPart = (WorksheetPart)(wbPart.GetPartById(theSheet.Id));
Cell theCell = wsPart.Worksheet.Descendants<Cell>().Where(c => c.CellReference == "D16").FirstOrDefault();
var value = theCell.CellValue.InnerText;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.