[英]How to extract formula from excel cell in java to identify cmd functions?
Here is the brief description of the problem.以下是问题的简要说明。
I am working on identifying excel files which have CMD functions such as "=cmd|'/C calc'!A0" for security filtering.我正在识别具有 CMD 功能的 excel 文件,例如用于安全过滤的“=cmd|'/C calc'!A0”。 We have to currently use Java to parse these files.
我们目前必须使用 Java 来解析这些文件。
I used the following two approaches:我使用了以下两种方法:
Does anyone have some suggestions how i can go about this ?有没有人对我如何解决这个问题有一些建议? It would be really helpful.
这真的很有帮助。
Thank you.谢谢你。
I did find an elaborate soln which i used using the above linked stackoverflow.. It handles for both XSSF and HSSF.我确实找到了一个精心设计的解决方案,我使用上面链接的 stackoverflow .. 它处理 XSSF 和 HSSF。
if (workbook instanceof XSSFWorkbook) {
XSSFWorkbook xssfWorkbook = (XSSFWorkbook) workbook;
List<ExternalLinksTable> externalLinks = xssfWorkbook.getExternalLinksTable();
for (ExternalLinksTable linksTable : externalLinks) {
if (linksTable.getCTExternalLink().isSetDdeLink()) {
return false;
}
}
} else {
HSSFWorkbook hssfWorkbook = (HSSFWorkbook) workbook;
Set<String> references = getWorkbookReferences(hssfWorkbook);
if (containsStartsWithSubString(references, "cmd")) {
return false;
}
}
private Set<String> getWorkbookReferences (HSSFWorkbook wb)
{
Set<String> references = new HashSet<>();
InternalWorkbook internalWorkbook = wb.getInternalWorkbook();
int extSheetIdx = 0;
while (internalWorkbook.getExternalSheet(extSheetIdx) != null) {
EvaluationWorkbook.ExternalSheet extSheet =
internalWorkbook.getExternalSheet(extSheetIdx++);
references.add(extSheet.getWorkbookName());
// fail safe.
if (extSheetIdx > maxExterLinks) {
return references;
}
}
return references;
}
Any suggestions are welcome!欢迎任何建议!
I am still working on how to identify for Word documents, unfortunately :)不幸的是,我仍在研究如何识别 Word 文档:)
There is no option to get the complete string of the cell if there is a formula in the cell as below如果单元格中有如下公式,则无法获取单元格的完整字符串
SUM(1+1)*cmd|' /C calc'!A0
When I use myCell.getCellFormula()
the result is SUM(1+1)*A1
which is not an expected one.当我使用
myCell.getCellFormula()
,结果是SUM(1+1)*A1
这不是预期的结果。
I wanted to block the =cmd|我想阻止 =cmd| or cmd|
或 cmd| if found in a particular cell in the sheet.
如果在工作表的特定单元格中找到。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.