apache poi拆分excel表格
前几天遇到一个需求,需要把一个有几万行有规则数据的excel文件拆分成N个excel文件,例如以下excel表格中包括以张三,李四,王五的各科考试成绩,每个学生的学号是唯一的,现在要求把每个人的成绩数据分开来生成独立的表格数据,并以学号作为文件名。思路: 使用HashMap,用每个工作表作为值,用学号作为键。遍历原始工作表中的每一行,以学号为键去HashMap对象中取工作表,如果不存在,那么主新建一个工作表,写入此行数据并保存工作表在HashMap对象中。如果以当前行的学号作为键可以取到工作表,则取出工作表,并将当前行写入工作表,并重新保存工作表到HashMap对象中。最后遍历HashMap对象,重新生成Excel文件。
以下为实现代码
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFDataFormat;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelSplit {
public static void main(String[] args){
try {
System.out.println("开始拆分.....");
Map<String, XSSFWorkbook> map=getSplitMap("D:/xxxx.xlsx");//得到拆分后的子文件存储对象
createSplitXSSFWorkbook(map, "D:/splitdone/201404", "2014.04");//遍历对象生成的拆分文件
System.out.println("拆分结束,文件被拆分为"+map.size()+"个文件.");
} catch (Exception e) {
e.printStackTrace();
}
}
//将第一列的值作为键值,将一个文件拆分为多个文件
public static Map<String,XSSFWorkbook> getSplitMap(String fileName) throws Exception{
Map<String,XSSFWorkbook> map=new HashMap<String,XSSFWorkbook>();
InputStream is = new FileInputStream(new File(fileName));
//根据输入流创建Workbook对象
Workbook wb = WorkbookFactory.create(is);
//get到Sheet对象
Sheet sheet = wb.getSheetAt(0);
Row titleRow=null;
//这个必须用接口
int i=0;
for(Row row : sheet){//遍历每一行
if(i==0){
titleRow=row;//得到标题行
}else{
Cell keyCell=row.getCell(0);
String key=keyCell.getRichStringCellValue().toString();
XSSFWorkbook tempWorkbook=map.get(key);
if(tempWorkbook==null){//如果以当前行第一列值作为键值取不到工作表
tempWorkbook= new XSSFWorkbook();
Sheet tempSheet=tempWorkbook.createSheet();
Row firstRow=tempSheet.createRow(0);
for(short k=0;k<titleRow.getLastCellNum();k++){//为每个子文件创建标题
Cell c=titleRow.getCell(k);
Cell newcell=firstRow.createCell(k);
newcell.setCellValue(c.getStringCellValue());
}
map.put(key,tempWorkbook);
}
Sheet secSheet=tempWorkbook.getSheetAt(0);
Row secRow=secSheet.createRow(secSheet.getLastRowNum()+1);
for(short m=0;m<row.getLastCellNum();m++){
Cell newcell=secRow.createCell(m);
setCellValue(newcell,row.getCell(m),tempWorkbook);
}
map.put(key,tempWorkbook);
}
i=i+1;//行数加一
}
return map;
}
//创建文件
public static void createSplitXSSFWorkbook(Map<String, XSSFWorkbook> map,String savePath,String month)
throws IOException {
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
String key = (String) entry.getKey();
XSSFWorkbook val = (XSSFWorkbook) entry.getValue();
File filePath=new File(savePath);
if(!filePath.exists()){
System.out.println("存放目录不存在,自动为您创建存放目录.");
filePath.mkdir();
}
if(!filePath.isDirectory()){
System.out.println("无效文件目录");
return ;
}
File file=new File(savePath+"/"+key+"_"+month+".xlsx");
FileOutputStream fOut;// 新建输出文件流
try {
fOut = new FileOutputStream(file);
val.write(fOut); // 把相应的Excel工作薄存盘
fOut.flush();
fOut.close(); // 操作结束,关闭文件
} catch (FileNotFoundException e) {
System.out.println("找不到文件");
}
}
}
//将一个单元格的值赋给另一个单元格
public static void setCellValue(Cell newCell,Cell cell,XSSFWorkbook wb){
if(cell==null){
return;
}
switch(cell.getCellType()){
case Cell.CELL_TYPE_BOOLEAN:
newCell.setCellValue(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
if(DateUtil.isCellDateFormatted(cell)){
XSSFCellStyle cellStyle =wb.createCellStyle();
XSSFDataFormat format= wb.createDataFormat();
cellStyle.setDataFormat(format.getFormat("yyyy/m/d"));
newCell.setCellStyle(cellStyle);
newCell.setCellValue(cell.getDateCellValue());
}else{
//读取数字
newCell.setCellValue(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_FORMULA:
newCell.setCellValue(cell.getCellFormula());
break;
case Cell.CELL_TYPE_STRING:
newCell.setCellValue(cell.getStringCellValue());
break;
}
}
}
有任何问题请联系qq 359709421
如果您觉得我的文章给了您帮助,请为我买一杯饮料吧!以下是我的支付宝,意思一下我将非常感激!
页:
[1]