我正在創建XLSX文件並向單元格添加一些值,它寫道一切正常,但是當我讀取文件或只是打開文件時。我沒有看到所有的數據。寫入excel文件時會丟失一些數據。 Java
File myfile = new File("hello.xlsx");
Workbook workBook = new XSSFWorkbook();
Sheet sheet = workBook.createSheet("sheet");
String[] tokens= {"A1", "B5", "C7", "J1", "K15", "Z20"};
for (String tok: tokens) {
CellReference ref = new CellReference(tok);
Row row = sheet.createRow(ref.getRow());
Cell cell = row.createCell((short) ref.getCol());
cell.setCellValue("blabla");
System.out.println(ref);
}
workBook.write(new FileOutputStream(myfile));
workBook.close();
System.out.println();
FileInputStream fileInputStream = new FileInputStream(myfile);
Workbook workBook2 = new XSSFWorkbook(fileInputStream);
Sheet worksheet = workBook2.getSheet("sheet");
for(String tok: tokens) {
CellReference ref= new CellReference(tok);
Row row = worksheet.getRow(ref.getRow());
Cell cell = row.getCell(ref.getCol());
if(cell != null){
System.out.println(ref);
}
}
但是,當我讀回他們,我只得到他們幾個。爲什麼?
org.apache.poi.ss.util.CellReference [A1]
org.apache.poi.ss.util.CellReference [B5]
org.apache.poi.ss.util.CellReference [C7]
org.apache.poi.ss.util.CellReference [J1]
org.apache.poi.ss.util.CellReference [K15]
org.apache.poi.ss.util.CellReference [Z20]
org.apache.poi.ss.util.CellReference [B5]
org.apache.poi.ss.util.CellReference [C7]
org.apache.poi.ss.util.CellReference [J1]
org.apache.poi.ss.util.CellReference [K15]
org.apache.poi.ss.util.CellReference [Z20]
謝謝!它對我很好。 – viderSelf
不客氣。請記住,也許對細胞也一樣。如果不存在,那麼總是會創建沒有樣式的**新**單元格,而您可能只想更改該值。 –