apache poi中的shiftRows无法正常工作

时间:2011-03-02 19:03:24

标签: java apache apache-poi

InputStream inp = new FileInputStream("workbook.xls");
HSSFWorkbook wb = new HSSFWorkbook(inp);

String text="";
Sheet sheet1 = wb.getSheetAt(0);

for (Row row : sheet1) {
    for (Cell cell : row) {
        // Do something here
        ExcelExtractor extractor = new ExcelExtractor(wb);
        text=extractor.getText();
        System.out.print("text="+text);
    }
}
sheet1.shiftRows(0,sheet1.getLastRowNum(),1);

调用shiftrows后,我的excel表格中没有得到结果。数据保持不变。我在哪里做错了?

1 个答案:

答案 0 :(得分:1)

这是因为您的工作簿只能打开阅读,因为它来自输入流

InputStream inp = new FileInputStream("workbook.xls");

要获得结果,您必须将新工作簿对象写入新的xsl电子表格。有点像...

File outWB = new File("workbook2.xls");
OutputStream out = new FileOutputStream(outWB);
wb.write(out);
out.flush();
out.close();

您可以对同一个文件执行此操作,但首先需要inp.close()