使用ApachePois在excel中添加一些记录
我正在使用ApachePOI来处理.xlsx文件 我有两个.xlsx文件,使用ApachePois在excel中添加一些记录,apache,excel,apache-poi,Apache,Excel,Apache Poi,我正在使用ApachePOI来处理.xlsx文件 我有两个.xlsx文件, part.xlsx和full.xlsx 它们拥有相同的结构 每个记录(poi中的行对象)有三列:名称、年龄、位置 part.xlsx中几乎有5000行,full.xlsx中有40000行 现在我想从full.xlsx中添加与part.xlsx中的值相同的行 例如: part.xlsx: Name age location kk 23 USA bb 24 England ...... full.xlsx Na
part.xlsx和full.xlsx
它们拥有相同的结构
每个记录(poi中的行对象)有三列:名称、年龄、位置
part.xlsx中几乎有5000行,full.xlsx中有40000行
现在我想从full.xlsx中添加与part.xlsx中的值相同的行
例如:
part.xlsx:
Name age location
kk 23 USA
bb 24 England
......
full.xlsx
Name age location
kk 23 USA
bb 24 England
xx 25 USA
......
现在我想增加'kk'和'bb'行,并将它们保存到一个新文件中
这是代码:
List<User> usersInpart=new ArrayList<User>();
List<Row> rows_to_be_saved=new ArrayList<Row>();
//read the part.xlsx and save them.
FileInputStream fis_part=new FileInputStream("part.xlsx");
WorkBook wb_part=WorkbookFactory.create(fis_part);
Sheet st_part=wb_part.getSheetAt(0);
for(Row row : st_part){
if(row.getRowNum()==0) continue; //skip the first row(the title)
User u=new User();
u.setName(row.getCell(0).getRichStringValue().getString().trim());
u.setAge(row.getCell(1).getNumericCellValue());
u.setLocation(row.getCell(2).getRichStringValue().getString().trim());
usersInpart.add(u);
}
fis_part.close();
//read the full.xlsx
FileInputStream fis_full=new FileInputStream("full.xlsx");
WorkBook wb_full=WorkbookFactory.create(fis_full);
Sheet st_full=wb_full.getSheetAt(0);
for(Row row : st_full){
if(row.getRowNum()==0) continue; //skip the first row(the title)
String name=row.getCell(0).getRichStringValue().getString().trim();
double age=row.getCell(1).getNumericCellValue();
String location=row.getCell(2).getRichStringValue().getString().trim();
for(User u : usersInpart){
if(u.getName.equals(name) && u.getAge==age && u.getLocation().equals(location))
rows_to_be_saved.add(row);
}
}
fis_full.close();
//write the selected rows to file
WorkBook wb_res=WorkbookFactory.create(fis_full);
Sheet st_res=wb_res.createSheet(0);
int i=0;
for (Row row : rows_to_be_saved) {
Row rw=st_res.createRow(i);
int k=0;
for (Cell cell : row) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
rw.createCell(k).setCellValue(cell.getRichStringCellValue().getString());
break;
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
rw.createCell(k).setCellValue(cell.getDateCellValue());
} else {
rw.createCell(k).setCellValue(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
rw.createCell(k).setCellValue(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
rw.createCell(k).setCellValue(cell.getCellFormula());
break;
default:
}
k++;
}
i++;
}
//save the wb_res
wb_res.write(new FileOutputStrem("xx.xlsx"));
List usersInpart=new ArrayList();
列出要保存的行=新建ArrayList();
//阅读part.xlsx并保存它们。
FileInputStream fis_part=新的FileInputStream(“part.xlsx”);
工作簿wb\U部件=WorkbookFactory.create(fis\U部件);
板材st_零件=wb_零件。getSheetAt(0);
用于(行:st_零件){
如果(row.getRowNum()==0)继续;//跳过第一行(标题)
用户u=新用户();
u、 setName(row.getCell(0.getRichStringValue().getString().trim());
u、 设置(row.getCell(1.getNumericCellValue());
u、 setLocation(row.getCell(2.getRichStringValue().getString().trim());
usersInpart.add(u);
}
fis_零件关闭();
//阅读全文.xlsx
FileInputStream fis_full=新的FileInputStream(“full.xlsx”);
工作簿wb_full=WorkbookFactory.create(fis_full);
表st_full=wb_full.getSheetAt(0);
用于(行:st_full){
如果(row.getRowNum()==0)继续;//跳过第一行(标题)
字符串名称=行.getCell(0.getRichStringValue().getString().trim();
double age=row.getCell(1.getNumericCellValue();
字符串位置=行.getCell(2).getRichStringValue().getString().trim();
for(用户u:usersInpart){
if(u.getName.equals(name)和&u.getAge==age和&u.getLocation().equals(location))
要保存的行。添加(行);
}
}
fis_full.close();
//将所选行写入文件
工作簿wb_res=WorkbookFactory.create(fis_full);
图纸st_res=wb_res.createSheet(0);
int i=0;
for(行:要保存的行){
Row rw=标准资源创建Row(i);
int k=0;
用于(单元格:行){
开关(cell.getCellType()){
case Cell.Cell\u类型\u字符串:
createCell(k).setCellValue(cell.getRichStringCellValue().getString());
打破
case Cell.Cell\u类型\u数值:
if(DateUtil.isCellDateFormatted(单元格)){
createCell(k).setCellValue(cell.getDateCellValue());
}否则{
createCell(k).setCellValue(cell.getNumericCellValue());
}
打破
case Cell.Cell\u类型\u布尔值:
createCell(k).setCellValue(cell.getBooleanCellValue());
打破
case Cell.Cell_类型_公式:
createCell(k).setCellValue(cell.getCellFormula());
打破
违约:
}
k++;
}
i++;
}
//保存wb\u res
wb_res.write(新文件输出字符串(“xx.xlsx”);
现在我想知道保存文件有什么好办法吗
因为我已将所选行保存在“rows\u to\u be\u saved”中
我创建了新的工作表“st_res”,有没有办法直接将这些行保存到“st_res”中?从现在起,我已经根据“rows\u to\u be\u saved”中的行创建了每一行
因此将有两个行列表。我认为这是浪费记忆
有什么建议吗?如果内存使用是一个问题,您可以通过使用读取完整的.xlsx文件来节省更多的内存。您目前正在将一个40000行文件加载到内存中,而事件模型一次只能在内存中保留一行