Java 如何将数据从arrayList写入多张excel工作表
我在ArrayList中存储了数据,我想将这些数据以多张工作表的形式写入excel工作簿。 我可以在excel工作簿中写入数据,但它是在同一张表中写入的。 到目前为止,我能够以以下方式写入数据,如图中所示: 但是我想把数据分成多张表, 数据应与页眉分开,如序号、第1栏……第6栏,一旦出现,数据应写入新的工作表。 我正在使用此代码将数据写入excelJava 如何将数据从arrayList写入多张excel工作表,java,apache-poi,Java,Apache Poi,我在ArrayList中存储了数据,我想将这些数据以多张工作表的形式写入excel工作簿。 我可以在excel工作簿中写入数据,但它是在同一张表中写入的。 到目前为止,我能够以以下方式写入数据,如图中所示: 但是我想把数据分成多张表, 数据应与页眉分开,如序号、第1栏……第6栏,一旦出现,数据应写入新的工作表。 我正在使用此代码将数据写入excel for (int i = 0; i < listOfResults.size(); i++) { row = she
for (int i = 0; i < listOfResults.size(); i++) {
row = sheet.createRow(i);
for (int j = 0; j < 7; j++) {
cell = row.createCell(j);
if (j + add < listOfResults.size()) {
cell.setCellValue(listOfResults.get(j + add));
}
}
add += 7;
}
for(int i=0;i
请帮助..您可以检查当前元素是否与序号类似。
如果是,您必须打开新工作表并继续书写。这与其说是POI问题,不如说是中断逻辑问题 您的代码
for (int i = 0; i < listOfResults.size(); i++) {
row = sheet.createRow(i);
for (int j = 0; j < 7; j++) {
cell = row.createCell(j);
if (j + add < listOfResults.size()) {
cell.setCellValue(listOfResults.get(j + add));
}
}
add += 7;
}
int rowIndex = 0;
int firstCellInRow = 0;
int cellCount = listOfResults.size();
int cellsPerRow = 7;
int rowCount = cellCount / cellsPerRow;
for (int i = 0; i < rowCount; i++) {
// detect first header cell
firstCellInRow = i * cellsPerRow;
if (listOfResults.get(firstCellInRow).equals("S.NO")) {
// create a new sheet
sheet = wb.createSheet();
// reset the row index to 0
rowIndex = 0;
}
// create the row and increment the row index
row = sheet.createRow(rowIndex++);
// now add the data to the cells
for (int j = 0; j < cellsPerRow; j++) {
cell = row.createCell(j);
if (firstCellInRow + j < listOfResults.size()) {
cell.setCellValue(listOfResults.get(firstCellInRow + j));
}
}
}
for(int i=0;i
这里没有检测标题行的内容,因此您没有创建新的图纸。您需要检测中断,然后创建一个新的工作表,并将每个标题的行索引重置为零
修改代码
for (int i = 0; i < listOfResults.size(); i++) {
row = sheet.createRow(i);
for (int j = 0; j < 7; j++) {
cell = row.createCell(j);
if (j + add < listOfResults.size()) {
cell.setCellValue(listOfResults.get(j + add));
}
}
add += 7;
}
int rowIndex = 0;
int firstCellInRow = 0;
int cellCount = listOfResults.size();
int cellsPerRow = 7;
int rowCount = cellCount / cellsPerRow;
for (int i = 0; i < rowCount; i++) {
// detect first header cell
firstCellInRow = i * cellsPerRow;
if (listOfResults.get(firstCellInRow).equals("S.NO")) {
// create a new sheet
sheet = wb.createSheet();
// reset the row index to 0
rowIndex = 0;
}
// create the row and increment the row index
row = sheet.createRow(rowIndex++);
// now add the data to the cells
for (int j = 0; j < cellsPerRow; j++) {
cell = row.createCell(j);
if (firstCellInRow + j < listOfResults.size()) {
cell.setCellValue(listOfResults.get(firstCellInRow + j));
}
}
}
int行索引=0;
int firstCellInRow=0;
int cellCount=listOfResults.size();
int cellsPerRow=7;
int rowCount=cellCount/cellsPerRow;
对于(int i=0;i
另一个问题是您在主循环中运行的次数太多。您正在为每个单元格循环一次,但您确实希望每行循环一次。这就是在第一个循环中将列表的长度(单元格数)除以7(每行单元格数)的原因。如何在代码中创建
工作表
?我怀疑您从internet上获取了一些代码,并试图使其适应您的需要。最好是仔细阅读该代码,理解每个调用,并使用ApachePOI文档检查该调用的确切功能,以及API提供的其他可能性。这将为您提供创建满足您个人需要的程序所需的知识。@user7291698:-我以以下方式创建工作表:Before first for loop sheet sheet=wb.createSheet(“ExpectedResults”);Real怀疑论者-是的,我已经检查了Apache POI文档,但是仍然没有任何东西可以帮助我。@xarines-我已经尝试过这个方法,使用这个条件我能够将数据分解为多张表,但是第一张表,但是我得到了这种类型的输出:表1:仅页眉,表2:表1数据加页眉2,表3:table data plus header3,就像我在检查以下条件后创建新的表一样:if(listOfResults.get(j+add).contains(“S.NO”){Sheet=wb.createSheet(“Sheet”+i);}cell.setCellValue(listOfResults.get(j+add))代码>@Betlista-我首先检查“S.NO”,然后创建新的工作表,然后写入数据<代码>if(listOfResults.get(j+add).contains(“S.NO”){sheet=wb.createSheet(“sheet”+i);}cell.setCellValue(listOfResults.get(j+add))
@mayankbisht创建新工作表时,您还需要重新创建您的行
和单元格
,因为您所拥有的都属于旧工作表…Real怀疑论者:-感谢这项工作做得很好,我能够根据需要将数据写入多个工作表。第一张表是正确的,第二张表是从空白行开始的,之后数据就在那里了。这些空行等于第一张图纸数据的大小。同样,第三张图纸以空行开始(图纸1+图纸2)