gpt4 book ai didi

java - 无法使用 apache POI 在工作簿中创建新的 Excel 工作表

转载 作者:行者123 更新时间:2023-12-01 22:17:45 29 4
gpt4 key购买 nike

我正在尝试将多个文件复制到一个 Excel 文件中。该 Excel 文件中的每个工作表都将包含一个文件的内容。我需要复制大约 6 个文件。因此生成的文件应包含 6 张。但是当我运行代码时,单个文件只生成一张表。我尝试调试它但无法找出原因。

这是我的代码。

 public static void main(String[] args) throws IOException {

// TODO Auto-generated method stub
CreateSingleExcelFile cef = new CreateSingleExcelFile();
cef.fileIterator();
}

public void fileIterator() throws IOException{

File dir = new File("path for files to copy");
File[] dir_listing = dir.listFiles();
HSSFWorkbook my_wb = new HSSFWorkbook();
//creating an output stream to copy all files in combined.xls
BufferedOutputStream bos= new BufferedOutputStream(new FileOutputStream("path of resultant excel sheet"));
//traversing through a list of files
for(File file: dir_listing){
//file: file to be copied.
add_in_excel(my_wb,bos,file);
System.out.println("In file :" + file.getName());
}
bos.close();
System.out.println("Files are copied");
}


private void add_in_excel(HSSFWorkbook copy_wb, BufferedOutputStream bos,File file) throws IOException {
// TODO Auto-generated method stub
//creating a new sheet in the copy workbook
HSSFSheet mySheet = copy_wb.createSheet(file.getName());

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
HSSFWorkbook workbook = new HSSFWorkbook(bis);
CellStyle cs = copy_wb.createCellStyle();
cs.setWrapText(true);

HSSFSheet sheet = null;
HSSFRow row = null;
HSSFCell cell = null;
HSSFRow myRow = null;
HSSFCell myCell = null;
int sheets = workbook.getNumberOfSheets();
int fRow = 3;
int lRow = 0;
int count_row=0;

//traversing through sheets in the 'file'
for (int iSheet = 0; iSheet < sheets; iSheet++) {
sheet = workbook.getSheetAt(iSheet);
if (sheet != null) {
lRow = sheet.getLastRowNum();
for (int iRow = fRow; iRow <= lRow; iRow++) {
row = sheet.getRow(iRow);
//creating row in the new sheet
myRow = mySheet.createRow(count_row++);
if (row != null) {
for (int iCell = 0; iCell < 4; iCell++) {
//creating a column in the new sheet
cell = row.getCell(iCell);
myCell = myRow.createCell(iCell);
myCell.setCellStyle(cs);
//setting cell type and adding data in each cell
if (cell != null ) {
myCell.setCellType(cell.getCellType());
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_BLANK:
myCell.setCellValue("");
break;

case HSSFCell.CELL_TYPE_BOOLEAN:
myCell.setCellValue(cell.getBooleanCellValue());
break;

case HSSFCell.CELL_TYPE_ERROR:
myCell.setCellErrorValue(cell.getErrorCellValue());
break;

case HSSFCell.CELL_TYPE_FORMULA:
myCell.setCellFormula(cell.getCellFormula());
break;

case HSSFCell.CELL_TYPE_NUMERIC:
if(HSSFDateUtil.isCellDateFormatted(cell))
myCell.setCellValue(cell.getDateCellValue());
else
myCell.setCellValue(cell.getNumericCellValue());
break;

case HSSFCell.CELL_TYPE_STRING:
myCell.setCellValue(cell.getStringCellValue());
break;
default:
myCell.setCellFormula(cell.getCellFormula());
}
}
}
}
}
}
}
bis.close();
copy_wb.write(bos);
}

最佳答案

由于您没有提到任何异常或堆栈跟踪,因此我将进行疯狂猜测 - 对于目录中的每个新 excel 文件,您正在执行

HSSFWorkbook 工作簿 = new HSSFWorkbook(bis);

在阅读每张工作表(所有行和单元格)结束时,您将继续阅读下一张工作表,依此类推,直到所有工作表都已创建并存储在内存中。然后,您通过

将工作簿本身写到输出流

copy_wb.write(bos);

[我知道这些你都知道,但万一将来有人来,这将使他们更容易了解正在发生的事情,而无需花费时间]

我在想,当你第一次说workbook.write(outputstream)时,内容就被写入了。但你还没有关闭流,并且你已经写完了一整本工作簿。下次您想要将另一个工作簿写入同一流时,我真的不知道会发生什么。不应该是向当前工作簿添加工作表(而不是将多个工作簿写入同一输出流)吗?

我建议创建目标工作簿(如果不存在)并编写源工作簿的工作表(而不是工作簿本身)。这可能是一种解决方法,但除非我可以将多个工作簿调试到同一输出流,否则我无法真正建议解决当前问题。

关于java - 无法使用 apache POI 在工作簿中创建新的 Excel 工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30647746/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com