openxml sdk excel如何parsing和计算公式

Excel公式单元格中有公式= SUM(C2:C3)的Excel文件。

从云中的远程web服务器上托pipe的Web应用程序,没有安装Excel,我会传入C2和C3的值。

我也可以在Excel中确定确切的公式。 如何在C#中编程分析这个公式,以便我可以得到6的结果,如果C2和C3的input值分别是2和4?

如果公式非常复杂,那么parsing公式并在asp.net mvc应用程序的服务器端用C#计算它的最好方法是什么?

在这种情况下,代码示例真的会使我受益。

如果你提供了一个工具来打开excel文件并把它的内容转换成html,你必须处理计算。

如果该文件是“很好创build”,例如手动使用Excel,则可以确保不需要pipe理公式的计算,因为Excel会执行这个技巧,并将公式存储在CellFormula的子元素中,并生成CellValue的子元素请参阅方法GetValue_D11())。 所以基本上你只需要显示结果..它总是一个string。

不幸的是你必须处理样式和数据types,如果你想维护行为。

其实你必须build立一个复杂的基于Web的电子表格查看器/编辑器。

这里是一个示例“固定”(完全不是dynamic的),用于检索string值和公式值。 如果你想运行testing,一定要下载该文件( http://www.devnmore.com/share/Test.xlsx )否则它不能正常工作。

ShowValuesSample svs = new ShowValuesSample("yourPath\\Test.xlsx"); String[] test = svs.GetDescriptions_A2A10(); Double grandTotal = svs.GetValue_D11(); 

ShowValuesSample类:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using DocumentFormat.OpenXml.Packaging; using Ap = DocumentFormat.OpenXml.ExtendedProperties; using Vt = DocumentFormat.OpenXml.VariantTypes; using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Spreadsheet; using A = DocumentFormat.OpenXml.Drawing; using System.Globalization; namespace TesterApp { public class ShowValuesSample { public String FileName { get; private set; } private SpreadsheetDocument _ExcelDocument = null; public SpreadsheetDocument ExcelDocument { get { if (_ExcelDocument == null) { _ExcelDocument = SpreadsheetDocument.Open(FileName, true); } return _ExcelDocument; } } private SheetData _SheetDataOfTheFirstSheet = null; public SheetData SheetDataOfTheFirstSheet { get { if (_SheetDataOfTheFirstSheet == null) { WorksheetPart shPart = ExcelDocument.WorkbookPart.WorksheetParts.ElementAt(0); Worksheet wsh = shPart.Worksheet; _SheetDataOfTheFirstSheet = wsh.Elements<SheetData>().ElementAt(0); } return _SheetDataOfTheFirstSheet; } } private SharedStringTable _SharedStrings = null; public SharedStringTable SharedStrings { get { if (_SharedStrings == null) { SharedStringTablePart shsPart = ExcelDocument.WorkbookPart.SharedStringTablePart; _SharedStrings = shsPart.SharedStringTable; } return _SharedStrings; } } public ShowValuesSample(String fileName) { FileName = fileName; } //In the file descriptions are stored as sharedString //so cellValue it's the zeroBased index of the sharedStringTable //in my example i saved 9 different values //sharedstring it's a trick to reduce size of a file obiouvsly writing //repetitive string just once public String[] GetDescriptions_A2A10() { String[] retVal = new String[9]; for (int i = 0; i < retVal.Length; i++) { Row r = SheetDataOfTheFirstSheet.Elements<Row>().ElementAt(i + 1); Cell c = r.Elements<Cell>().ElementAt(0); Int32 shsIndex = Convert.ToInt32(c.CellValue.Text); SharedStringItem shsItem = SharedStrings.Elements<SharedStringItem>().ElementAt(shsIndex); retVal[i] = shsItem.Text.Text; } return retVal; } //The value it's stored beacause excel does //To be sure it's correct you should perform all calculations //In this case i'm sure Excel didn't stored the wrong value so.. public Double GetValue_D11() { Double retVal = 0.0d; Int32 cellIndex = 0; //cellIndex it's 0 and not 3, cause A11, B11, C11 are empty cells //Another issue to deal with ;-) Cell c = SheetDataOfTheFirstSheet.Elements<Row>().ElementAt(10).Elements<Cell>().ElementAt(cellIndex); //as example take a look at the value of storedFormula String storedFormula = c.CellFormula.Text; String storedValue = c.CellValue.Text; NumberFormatInfo provider = new NumberFormatInfo(); provider.NumberDecimalSeparator = "."; provider.NumberGroupSeparator = ","; provider.NumberGroupSizes = new Int32[] { 3 }; retVal = Convert.ToDouble(storedValue, provider); return retVal; } } } 
 spreadSheet.WorkbookPart.Workbook.CalculationProperties.ForceFullCalculation = true; spreadSheet.WorkbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = true; 

为我工作。

恐怕不可能。 在Open XML中,您可以读取或更改公式。 但是你处理公式并通过打开的XML获得结果。

更改公式的C2和C3的值,然后将其保存在打开的xml中,现在通过Excel App打开文档。 这些值将被计算和显示。

请参考这个SO Post,关于这个问题打开xml sdk excel公式重新计算caching问题

请参阅这篇文章http://openxmldeveloper.org/discussions/formats/f/14/p/1806/158153.aspx

希望这可以帮助!