我尝试使用上面提到的库将一个可在线访问的工作簿添加到excel文件中。 但是,我收到以下异常:“没有名称的外部工作簿......”
我不关心Apache POI是否可以找到外部工作簿,因为我知道它确实存在于该位置。如果我手动添加它,它就可以工作。
我的问题是,我可以删除此异常或禁用验证吗?我只是想在excel中添加URL作为公式。 简单的文字不是很糟糕。
感谢您的帮助!
这里是完整的Stacktrace(请不要建议在HTTP前删除G,因为我已经尝试了这个并且它无法正常工作):
Exception in thread "main" java.lang.RuntimeException: No external workbook with name 'Ghttp://my-test-homepage.de/download/test.xls'
at org.apache.poi.hssf.model.LinkTable.getExternalSheetIndex(LinkTable.java:445)
at org.apache.poi.hssf.model.InternalWorkbook.getExternalSheetIndex(InternalWorkbook.java:1594)
at org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook.getSheetExtIx(HSSFEvaluationWorkbook.java:291)
at org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook.get3DReferencePtg(HSSFEvaluationWorkbook.java:88)
at org.apache.poi.ss.formula.FormulaParser.createAreaRefParseNode(FormulaParser.java:975)
at org.apache.poi.ss.formula.FormulaParser.parseRangeable(FormulaParser.java:579)
at org.apache.poi.ss.formula.FormulaParser.parseRangeExpression(FormulaParser.java:322)
at org.apache.poi.ss.formula.FormulaParser.parseSimpleFactor(FormulaParser.java:1540)
at org.apache.poi.ss.formula.FormulaParser.percentFactor(FormulaParser.java:1498)
at org.apache.poi.ss.formula.FormulaParser.powerFactor(FormulaParser.java:1485)
at org.apache.poi.ss.formula.FormulaParser.Term(FormulaParser.java:1858)
at org.apache.poi.ss.formula.FormulaParser.additiveExpression(FormulaParser.java:1986)
at org.apache.poi.ss.formula.FormulaParser.concatExpression(FormulaParser.java:1970)
at org.apache.poi.ss.formula.FormulaParser.comparisonExpression(FormulaParser.java:1927)
at org.apache.poi.ss.formula.FormulaParser.intersectionExpression(FormulaParser.java:1900)
at org.apache.poi.ss.formula.FormulaParser.unionExpression(FormulaParser.java:1880)
at org.apache.poi.ss.formula.FormulaParser.parse(FormulaParser.java:2028)
at org.apache.poi.ss.formula.FormulaParser.parse(FormulaParser.java:170)
at org.apache.poi.ss.formula.FormulaParser.parse(FormulaParser.java:190)
at org.apache.poi.hssf.model.HSSFFormulaParser.parse(HSSFFormulaParser.java:79)
at org.apache.poi.hssf.usermodel.HSSFCell.setCellFormula(HSSFCell.java:621)
at test.app.main.ApplicationStarter.main(ApplicationStarter.java:53)
以下是完整的源代码:
private static final String PATH_TO_XLS_FILE = "C:/Users/User/Desktop/folder/value.xls";
public static void main(String[] args) {
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
Workbook workbook = null;
try {
fileInputStream = new FileInputStream(new File(PATH_TO_XLS_FILE));
workbook = new HSSFWorkbook(fileInputStream);
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
Sheet sheet = workbook.getSheet("Daten");
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row currectRow = rowIterator.next();
Iterator<Cell> cellIterator = currectRow.cellIterator();
while (cellIterator.hasNext()) {
Cell currectCell = cellIterator.next();
if (currectCell.getCellTypeEnum() == CellType.FORMULA
&& currectCell.getCellFormula().contains("key")) {
String link = "'[Ghttp://my-test-url.de/download/test.xls]value'!F4";
currectCell.setCellFormula(link);
}
}
}
fileOutputStream = new FileOutputStream(new File(PATH_TO_XLS_FILE));
workbook.write(fileOutputStream);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
IOUtils.closeQuietly(fileInputStream);
IOUtils.closeQuietly(fileOutputStream);
IOUtils.closeQuietly(workbook);
}
}
我试图自己解决问题,现在我可以将外部链接添加到Excel工作簿。不幸的是,我遇到了另一个问题。当我打开Excel文件时,无法解析外部链接,因为Apache POI插入了一个特殊符号。如果我删除此特殊符号,则可以解析外部链接。所以我的新问题是,我怎样才能避免Apache POI添加这个特殊标志?这可能是编码问题吗?谢谢你的帮助!
这是我的新源代码:
private static void modifyExcelFile(String pathToXLS) {
FileInputStream fileInputStreamLocal = null;
InputStream inputStreamRemote = null;
FileOutputStream fileOutputStream = null;
HSSFWorkbook workbookLocal = null;
HSSFWorkbook workbookRemote = null;
try {
LOGGER.debug(LOGGER_START);
fileInputStreamLocal = new FileInputStream(new File(pathToXLS));
inputStreamRemote = new URL(URL_TO_DATA_FILE + FILE_NAME).openStream();
workbookLocal = new HSSFWorkbook(fileInputStreamLocal);
workbookRemote = new HSSFWorkbook(inputStreamRemote);
workbookLocal.linkExternalWorkbook(URL_TO_DATA_FILE + FILE_NAME, workbookRemote);
Sheet sheet = workbookLocal.getSheet(SHEET_NAME);
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row currectRow = rowIterator.next();
Iterator<Cell> cellIterator = currectRow.cellIterator();
while (cellIterator.hasNext()) {
Cell currectCell = cellIterator.next();
if (currectCell.getCellTypeEnum() == CellType.FORMULA
&& currectCell.getCellFormula().contains(FILE_NAME)) {
String suffix = currectCell.getCellFormula();
int positionOfBracket = suffix.indexOf("]");
if (positionOfBracket < 0) {
continue;
}
suffix = suffix.substring(positionOfBracket + 1, suffix.length());
String[] suffixes = null;
suffixes = suffix.split("'!");
if (suffixes.length != 2) {
suffixes = suffix.split("!");
if (suffixes.length != 2) {
continue;
}
}
String link = "'[" + URL_TO_DATA_FILE + FILE_NAME + "]" + suffixes[0] + "'!" + suffixes[1];
currectCell.setCellFormula(link);
}
}
}
fileOutputStream = new FileOutputStream(new File(pathToXLS));
workbookLocal.write(fileOutputStream);
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
LOGGER.debug(LOGGER_ABORT);
} finally {
IOUtils.closeQuietly(fileInputStreamLocal);
IOUtils.closeQuietly(inputStreamRemote);
IOUtils.closeQuietly(fileOutputStream);
IOUtils.closeQuietly(workbookLocal);
IOUtils.closeQuietly(workbookRemote);
LOGGER.debug(LOGGER_END);
}
}
这里是Apache POI添加的特殊标志的屏幕截图: special sign is inside the red area