dot*_*ner 1 c# excel openxml-sdk asp.net-mvc-4
我有单元格C4中具有公式= SUM(C2:C3)的excel文件.我在单元格C2和C3中分别有值15和17.以编程方式,我希望能够执行C4公式并返回值32(公式的结果).
有人建议我应该使用OpenXml SDK.因为此代码将由Windows Azure网站托管和使用.
这是我到目前为止的代码,单元格不执行C4中的公式.
string filename = Server.MapPath("/") + "ExcelData.xlsx";
using (SpreadsheetDocument document = SpreadsheetDocument.Open(filename, true))
{
document.WorkbookPart.Workbook.CalculationProperties.ForceFullCalculation = true;
document.WorkbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = true;
Sheet sheet = document.WorkbookPart.Workbook.Descendants<Sheet>().SingleOrDefault(s => s.Name == "myRange1");
if (sheet == null)
{
throw new ArgumentException(
String.Format("No sheet named {0} found in spreadsheet {1}", "myRange1", filename), "sheetName");
}
WorksheetPart worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(sheet.Id);
int rowIndex = int.Parse("C4".Substring(1));
Row row = worksheetPart.Worksheet.GetFirstChild<SheetData>().
Elements<Row>().FirstOrDefault(r => r.RowIndex == rowIndex);
Cell cell = row.Elements<Cell>().FirstOrDefault(c => "C4".Equals(c.CellReference.Value));
}
Run Code Online (Sandbox Code Playgroud)
document.WorkbookPart.Workbook.CalculationProperties.ForceFullCalculation = true;
document.WorkbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = true;
Run Code Online (Sandbox Code Playgroud)
根据MSDN:
使用CellFormula(<f>)元素定义公式文本.公式可以包含包含各种预定义函数的数学表达式.所述CellValue(<V>)元件,存储基于计算式的最后时间缓存式值.
| 归档时间: |
|
| 查看次数: |
3594 次 |
| 最近记录: |