无法写入Excel文件(Apache POI) public静态字符串已存在(ArrayList reg\u id、ArrayList doc\u id、ArrayList status)引发rowseceededexception、WriteException、IOException{ 可写工作簿myFirstWbook=null; FileOutputStream out=新的FileOutputStream(新文件(“C://Users//Desktop//OP_demo.xlsx”); XSSFWorkbook工作簿=新XSSFWorkbook(); XSSFSheet sheet=workbook.createSheet(“Java书籍”); int rowCount=0; int columnCount=0; 对于(int i=0;i
,您需要将工作簿移动到循环之外。 workbook.write(out);将工作簿的内容写入文件流,处理完所有记录后需要写入文件,否则每次执行此操作时文件内容都会被重写无法写入Excel文件(Apache POI) public静态字符串已存在(ArrayList reg\u id、ArrayList doc\u id、ArrayList status)引发rowseceededexception、WriteException、IOException{ 可写工作簿myFirstWbook=null; FileOutputStream out=新的FileOutputStream(新文件(“C://Users//Desktop//OP_demo.xlsx”); XSSFWorkbook工作簿=新XSSFWorkbook(); XSSFSheet sheet=workbook.createSheet(“Java书籍”); int rowCount=0; int columnCount=0; 对于(int i=0;i,java,excel,apache,apache-poi,Java,Excel,Apache,Apache Poi,,您需要将工作簿移动到循环之外。 workbook.write(out);将工作簿的内容写入文件流,处理完所有记录后需要写入文件,否则每次执行此操作时文件内容都会被重写 public static String already_exists(ArrayList<String> reg_id, ArrayList<String> doc_id, ArrayList<String> status) throws RowsExceededException, Wri
public static String already_exists(ArrayList<String> reg_id, ArrayList<String> doc_id, ArrayList<String> status) throws RowsExceededException, WriteException, IOException{
WritableWorkbook myFirstWbook = null;
FileOutputStream out = new FileOutputStream(new File("C://Users//Desktop//OP_demo.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Java Books");
int rowCount = 0;
int columnCount = 0;
for(int i=0;i<reg_id.size();i++)
{
Object[][] bookData={{reg_id.get(i),doc_id.get(i),status.get(i)}};
Row row = sheet.createRow(rowCount++);
//System.out.println(reg_id.get(i)+doc_id.get(i)+status.get(i));
for (Object[] aBook : bookData)
{
for (Object field : aBook)
{
org.apache.poi.ss.usermodel.Cell cell = row.createCell(columnCount++);
{
cell.setCellValue(field.toString());
}
}
workbook.write(out);
}
}
out.close();
return "";
}
public静态字符串已存在(ArrayList reg\u id、ArrayList doc\u id、ArrayList status)引发rowseceededexception、WriteException、IOException{
可写工作簿myFirstWbook=null;
FileOutputStream out=新的FileOutputStream(新文件(“C://Users//Desktop//OP_demo.xlsx”);
XSSFWorkbook工作簿=新XSSFWorkbook();
XSSFSheet sheet=workbook.createSheet(“Java书籍”);
int rowCount=0;
int columnCount=0;
对于(int i=0;我在调用.close()之前,尝试将“workbook.write(out);”从循环中移出;嗨,Ankur,它成功了!我正在尝试了解它有什么不同,因为我是新手。你能解释一下吗?
public static String already_exists(ArrayList<String> reg_id, ArrayList<String> doc_id, ArrayList<String> status) throws RowsExceededException, WriteException, IOException{
WritableWorkbook myFirstWbook = null;
FileOutputStream out = new FileOutputStream(new File("C://Users//Desktop//OP_demo.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Java Books");
int rowCount = 0;
int columnCount = 0;
for(int i=0;i<reg_id.size();i++)
{
Object[][] bookData={{reg_id.get(i),doc_id.get(i),status.get(i)}};
Row row = sheet.createRow(rowCount++);
//System.out.println(reg_id.get(i)+doc_id.get(i)+status.get(i));
for (Object[] aBook : bookData)
{
for (Object field : aBook)
{
org.apache.poi.ss.usermodel.Cell cell = row.createCell(columnCount++);
{
cell.setCellValue(field.toString());
}
}
}
}
workbook.write(out); // writing the workbook to file-stream outside loop
out.close();
return "";
}