前幾天遇到一個需求,需要把一個有幾萬行有規則資料的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 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 getSplitMap(String fileName) throws Exception{
Map map=new HashMap();
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
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
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 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
如果您覺得我的文章給了您幫助,請為我買一杯飲料吧!以下是我的支付寶,意思一下我将非常感激!