如何将 Excel 方程导出为其他类型的表达式

有时候你可能需要在代码中将Excel公式导出为其他格式以满足工作需要,Aspose.Cells库可以满足你的需求。以下内容介绍一些导出Excel公式为其他格式的方法,希望这些方法对你有所帮助。

这里提供了示例代码,帮助您使用Aspose.Cells实现目标,同时也提供了必要的示例文件。

示例文件:Sample.xlsx

将公式导出为LaTeX表达式

如果你想将Excel中的公式导出为LaTeX表达式,可以使用ToLaTeX()方法。

下方示例代码展示了如何使用ToLaTeX()方法,并将生成的结果插入到HTML中:

C#-转LaTeX

string dirPath = @"";
Workbook workbook = new Workbook(dirPath + "Sample.xlsx");
StringBuilder sb = new StringBuilder();
sb.Append("<!DOCTYPE html>\r\n<html lang=\"en\">\r\n<head>\r\n <meta charset=\"UTF-8\">\r\n <title>Title</title>\r\n <script type=\"text/javascript\" async src=\"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML\"></script>\r\n <script type=\"text/x-mathjax-config\">\r\n MathJax.Hub.Config({\r\n\t tex2jax: {\r\n\t inlineMath: [['$','$'], ['\\\\(','\\\\)']],\r\n\t processEscapes: true\r\n\t }\r\n\t});\r\n </script>\r\n</head>\r\n<body>");
ShapeCollection shapes = workbook.Worksheets[0].Shapes;
TextBox textBox = (TextBox)shapes[0];
EquationNode mathNode = textBox.GetEquationParagraph().GetChild(0);
string s = mathNode.ToLaTeX();
sb.AppendLine("<p>$" + s + "$</p>");
sb.Append("</body>\r\n</html>");
File.WriteAllText(@"result.html", sb.ToString());

将公式导出为MathML表达式

如果你想将Excel中的公式导出为MathML表达式,可以使用ToMathML()方法。

下方示例代码展示了如何使用ToMathML()方法,并将生成的结果插入到HTML中:

C#-转MathML

string dirPath = @"";
Workbook workbook = new Workbook(dirPath + "Sample.xlsx");
StringBuilder sb = new StringBuilder();
sb.Append("<!DOCTYPE html>\r\n<html lang=\"en\">\r\n<head>\r\n <meta charset=\"UTF-8\">\r\n <title>Title</title>\r\n <script type=\"text/javascript\" async src=\"https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML\"></script>\r\n</head>\r\n<body>");
ShapeCollection shapes = workbook.Worksheets[0].Shapes;
TextBox textBox = (TextBox)shapes[0];
EquationNode mathNode = textBox.GetEquationParagraph().GetChild(0);
sb.AppendLine(mathNode.ToMathML());
sb.Append("</body>\r\n</html>");
File.WriteAllText(@"result.html", sb.ToString());