Java 如何使用ApachePOI将excel拆分为多个excel?
我在excel电子表格中有以下表格Java 如何使用ApachePOI将excel拆分为多个excel?,java,excel,apache-poi,Java,Excel,Apache Poi,我在excel电子表格中有以下表格 manager salary puk 2 puk 3 puk 4 puk 5 ser 3 ser 4 ser 5 sos 23 sos 24 sos 25 sos 26 sos 27 我需要使用FileOutputStream将此电子表格拆
manager salary
puk 2
puk 3
puk 4
puk 5
ser 3
ser 4
ser 5
sos 23
sos 24
sos 25
sos 26
sos 27
我需要使用FileOutputStream
将此电子表格拆分为三个不同的SHPReadsheet。第一个应包含managerpuk
,第二个ser
我想不出一个好的逻辑来分割它。我是否应该创建原始电子表格的临时副本,然后删除所有多余的行并保存它?然后如何删除这些行
(使用CSVReader可以很容易地做到这一点,但我需要保留格式)这是关于如何实现您的要求的整个程序,我已经测试过了,它工作得非常完美:) 告诉我你的想法:
package additives;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import java.util.*;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import java.io.*;
public class StackOverflow {
public static Workbook readWorkbook(){
Workbook wb=null;
try {
wb = WorkbookFactory.create(new File("stackOverflow.xlsx"));
} catch (InvalidFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return wb;
}
public static void writeToSheet(List<String> name, List<Double> salary, int sheetCounter, List<Sheet> outputSheets){
for(int i=0;i<salary.size();i++){
Row row=outputSheets.get(sheetCounter).createRow(i);
Cell nameCell=row.createCell(0);
nameCell.setCellValue(name.get(i));
Cell salaryCell=row.createCell(1);
salaryCell.setCellValue(salary.get(i));
}
}
public static void writeToWorkbook(Workbook wb){
try{
FileOutputStream out=new FileOutputStream("new StackOverflow.xlsx");
wb.write(out);
out.close();
} catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
Workbook inputWb=readWorkbook();
Sheet inputWs=inputWb.getSheet("sheet1");
List<String> name=new ArrayList<>();
List<Double> salary=new ArrayList<>();
Workbook outputWb=new XSSFWorkbook();
List<Sheet> outputSheets=new ArrayList<>();
int rowIndex=inputWs.getLastRowNum()+1;
int sheetCounter=0;
for(int i=1; i<rowIndex-1; i++){
Row outerRow=inputWs.getRow(i);
Row innerRow=null;
Cell outerCell=outerRow.getCell(0);
Cell innerCell=null;
int j=0;
for(j=i+1;j<rowIndex;j++){
innerRow=inputWs.getRow(j);
innerCell=innerRow.getCell(0);
if(outerCell.getStringCellValue().equals(innerCell.getStringCellValue())){
name.add(innerRow.getCell(0).getStringCellValue());
salary.add(innerRow.getCell(1).getNumericCellValue());
}
if(!outerCell.getStringCellValue().equals(innerCell.getStringCellValue())){
break;
}
}
name.add(outerRow.getCell(0).getStringCellValue());
salary.add(outerRow.getCell(1).getNumericCellValue());
i=j;
outputSheets.add(outputWb.createSheet("sheet"+sheetCounter));
writeToSheet(name,salary,sheetCounter, outputSheets);
sheetCounter++;
name.clear();
salary.clear();
Row tempRow=inputWs.getRow(i);
try{
name.add(tempRow.getCell(0).getStringCellValue());
salary.add(tempRow.getCell(1).getNumericCellValue());
}catch(Exception e){
continue;
}
}
writeToWorkbook(outputWb);
}
}
包装添加剂;
导入org.apache.poi.ss.usermodel.*;
导入org.apache.poi.xssf.usermodel.*;
导入java.util.*;
导入org.apache.poi.openxml4j.exceptions.InvalidFormatException;
导入java.io.*;
公共类堆栈溢出{
公共静态工作簿readWorkbook(){
工作簿wb=null;
试一试{
wb=WorkbookFactory.create(新文件(“stackOverflow.xlsx”);
}捕获(无效格式){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回wb;
}
公共静态无效写表(列表名称、列表薪资、int sheetCounter、列表输出表){
对于(int i=0;i而不是删除…移动单元格将是更好的选择。尝试此操作,您可能希望分割模板和数据电子表格。模板仅具有格式。从电子表格读取数据并将其放入临时列表。当数据输出时,复制模板电子表格。谢谢您,这非常完美