如何使用java创build可追加的excelsheet
我想创build一个可追加的excel表。 就像我有四个列stream1 stream2 stream3 stream4
我第一次插入数据只有第一列(stream1)之后,我想充分填写其他列逐一。
这是我正在使用的代码:
public void createFile(Jqueue stream1, Jqueue stream2, Jqueue stream3, Jqueue stream4) { try { String filename = "path"; boolean alreadyExists = (new File(filename)).exists(); HSSFRow rowhead = sheet1.createRow((short) 0); rowhead.createCell((short) 0).setCellValue("Stream 1"); rowhead.createCell((short) 1).setCellValue("Stream 2"); rowhead.createCell((short) 2).setCellValue("Stream 3"); rowhead.createCell((short) 3).setCellValue("Stream 4"); int i = 1; while (!stream1.isEmpty()) { String urlstream1 = ""; String urlstream2 = ""; String urlstream3 = ""; String urlstream4 = ""; HSSFRow row = sheet1.createRow((short) i); try { if (stream1.size() > 0) { urlstream1 = stream1.dequeue().toString(); } } catch (Exception ex) { } try { if (stream2.size() > 0) { urlstream2 = stream2.dequeue().toString(); } } catch (Exception ex) { } try { if (stream3.size() > 0) { urlstream3 = stream3.dequeue().toString(); } } catch (Exception ex) { } try { if (stream4.size() > 0) { urlstream4 = stream4.dequeue().toString(); } } catch (Exception ex) { } row.createCell((short) 0).setCellValue(urlstream1); row.createCell((short) 1).setCellValue(urlstream2); row.createCell((short) 2).setCellValue(urlstream3); row.createCell((short) 3).setCellValue(urlstream4); i++; } FileOutputStream fileOut = new FileOutputStream(filename); hwb.write(fileOut); fileOut.close();
但是这不是可追加的代码。 它逐行插入数据。
thx先进。
我想你的代码应该稍微改变一下。
尝试更换
int i = 1;
具体如下:
int i = sheet1.getLastRowNum()+ 1;
你还需要改变你的实现来读写文件。
- 如何把一个图像在Excel的单元格的Java?
- 在Java中使用模板创buildExcel表格,新版本的Excel
- 如何在Excel中使用HSSFSheet Apache POI创build具有多种样式的单元格?
- POI:单元格的行数错误forms方法getPhysicalNumberOfCells
- 使用NPOI的.xlsx文件有问题的损坏 – Excel无法打开文件“file.xlsx”,因为文件格式或文件扩展名无效
- 在Scala或Java中使用Excel语法格式化数字
- Excel工作表的单元格types显示为实际上是文本types的数字
- 如何检索与Excel中显示的结果相同的单元格值?
- 编写Excel文件时出现Apache POI OutOfMemoryError