Java 无法将值插入现有工作簿[apache poi]
我想在现有的工作手册中插入值。下面是封装此逻辑的类:Java 无法将值插入现有工作簿[apache poi],java,apache-poi,Java,Apache Poi,我想在现有的工作手册中插入值。下面是封装此逻辑的类: public class FormulaExtractor { private String pathToExcelFile; private XSSFWorkbook workbook; private Map<Double, Formula> idFormular = new HashMap<>(); public FormulaExtractor(String pathToExcelFile) { th
public class FormulaExtractor {
private String pathToExcelFile;
private XSSFWorkbook workbook;
private Map<Double, Formula> idFormular = new HashMap<>();
public FormulaExtractor(String pathToExcelFile) {
this.pathToExcelFile = pathToExcelFile;
try (FileInputStream fileInputStream = new FileInputStream(new File(pathToExcelFile))) {
this.workbook = new XSSFWorkbook(fileInputStream);
} catch (FileNotFoundException e) {
System.out.println("Cannot locate a xls file. Exception: " + e.toString());
} catch (IOException e) {
System.out.println("IO exception " + e.toString());
}
}
public void insertHumanReadableFormulas() throws IOException {
XSSFSheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row currentRow = rowIterator.next();
Cell cell = currentRow.createCell(CellReference.convertColStringToIndex("K"));
cell.setCellValue("Some dummy variable to test. ");
}
FileOutputStream fileOut = new FileOutputStream(new File(this.pathToExcelFile));
workbook.write(fileOut);
fileOut.close();
}
我没有得到任何异常或错误。当我打开excel文件时,我根本无法在列“K”中找到值。我做错了什么?当你说你做不到的时候——为什么不呢?你有错误吗?我没有任何异常或错误。当我打开excel文件时,我根本无法在“K”列中找到值。你有什么问题?您到底遇到了什么错误?我使用
poi-ooxml-3.10.1.jar尝试了您的代码,它的工作方式与您预期的一样。你查对文件了吗?您是否尝试过使用新创建的*.xlsx
文件,该文件不包含任何其他数据。当您说您不能这样做时,为什么不呢?你有错误吗?我没有任何异常或错误。当我打开excel文件时,我根本无法在“K”列中找到值。你有什么问题?您到底遇到了什么错误?我使用poi-ooxml-3.10.1.jar尝试了您的代码,它的工作方式与您预期的一样。你查对文件了吗?您是否尝试过使用新创建的*.xlsx
文件,该文件不包含任何其他数据。
public class App {
public static void main(String[] args) throws IOException {
final String pathToExcel = "some/path/some_excel.xlsx";
FormulaExtractor formulaExtractor = new FormulaExtractor(pathToExcel);
formulaExtractor.insertHumanReadableFormulas();
}
}