参考: http://blog.csdn.net/wutbiao/article/details/8696446 项目中需求: 将12个excel文件合并为一个总excel文件。首先想的思路是读取每个excel文件的sheet然后再copy到总excel文件里,查阅了前辈写的POI工具类,最后生成成功。但逐次打开12个excle文件效率很低,耗时9s,决定换其他思路开发。现将此工具类代码暂放,以后再学习使用。
import java.util.Iterator;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class POIUtils {
public class XSSFDateUtil extends DateUtil {
}
public static void copyCellStyle(XSSFCellStyle fromStyle, XSSFCellStyle toStyle) {
toStyle.cloneStyleFrom(fromStyle);//此一行代码搞定
//下面统统不用
/*
//对齐方式
toStyle.setAlignment(fromStyle.getAlignment());
//边框和边框颜色
toStyle.setBorderBottom(fromStyle.getBorderBottom());
toStyle.setBorderLeft(fromStyle.getBorderLeft());
toStyle.setBorderRight(fromStyle.getBorderRight());
toStyle.setBorderTop(fromStyle.getBorderTop());
toStyle.setTopBorderColor(fromStyle.getTopBorderColor());
toStyle.setBottomBorderColor(fromStyle.getBottomBorderColor());
toStyle.setRightBorderColor(fromStyle.getRightBorderColor());
toStyle.setLeftBorderColor(fromStyle.getLeftBorderColor());
//背景和前景
//toStyle.setFillPattern(fromStyle.getFillPattern()); //填充图案,不起作用,转为黑色
toStyle.setFillBackgroundColor(fromStyle.getFillBackgroundColor()); //不起作用
toStyle.setFillForegroundColor(fromStyle.getFillForegroundColor());
toStyle.setDataFormat(fromStyle.getDataFormat()); //数据格式
//toStyle.setFont(fromStyle.getFont()); //不起作用
toStyle.setHidden(fromStyle.getHidden());
toStyle.setIndention(fromStyle.getIndention());//首行缩进
toStyle.setLocked(fromStyle.getLocked());
toStyle.setRotation(fromStyle.getRotation());//旋转
toStyle.setVerticalAlignment(fromStyle.getVerticalAlignment()); //垂直对齐
toStyle.setWrapText(fromStyle.getWrapText()); //文本换行
*/
}
public static void mergeSheetAllRegion(XSSFSheet fromSheet, XSSFSheet toSheet) {//合并单元格
int num = fromSheet.getNumMergedRegions();
CellRangeAddress cellR = null;
for (int i = 0; i < num; i++) {
cellR = fromSheet.getMergedRegion(i);
toSheet.addMergedRegion(cellR);
}
}
public static void copyCell(XSSFWorkbook wb,XSSFCell fromCell, XSSFCell toCell) {
XSSFCellStyle newstyle=wb.createCellStyle();
copyCellStyle(fromCell.getCellStyle(), newstyle);
//toCell.setEncoding(fromCell.getEncoding());
//样式
toCell.setCellStyle(newstyle);
if (fromCell.getCellComment() != null) {
toCell.setCellComment(fromCell.getCellComment());
}
// 不同数据类型处理
int fromCellType = fromCell.getCellType();
toCell.setCellType(fromCellType);
if (fromCellType == XSSFCell.CELL_TYPE_NUMERIC) {
if (XSSFDateUtil.isCellDateFormatted(fromCell)) {
toCell.setCellValue(fromCell.getDateCellValue());
} else {
toCell.setCellValue(fromCell.getNumericCellValue());
}
} else if (fromCellType == XSSFCell.CELL_TYPE_STRING) {
toCell.setCellValue(fromCell.getRichStringCellValue());
} else if (fromCellType == XSSFCell.CELL_TYPE_BLANK) {
// nothing21
} else if (fromCellType == XSSFCell.CELL_TYPE_BOOLEAN) {
toCell.setCellValue(fromCell.getBooleanCellValue());
} else if (fromCellType == XSSFCell.CELL_TYPE_ERROR) {
toCell.setCellErrorValue(fromCell.getErrorCellValue());
} else if (fromCellType == XSSFCell.CELL_TYPE_FORMULA) {
toCell.setCellFormula(fromCell.getCellFormula());
} else { // nothing29
}
}
public static void copyRow(XSSFWorkbook wb,XSSFRow oldRow,XSSFRow toRow){
toRow.setHeight(oldRow.getHeight());
for (Iterator cellIt = oldRow.cellIterator(); cellIt.hasNext();) {
XSSFCell tmpCell = (XSSFCell) cellIt.next();
XSSFCell newCell = toRow.createCell(tmpCell.getColumnIndex());
copyCell(wb,tmpCell, newCell);
}
}
public static void copySheet(XSSFWorkbook wb,XSSFSheet fromSheet, XSSFSheet toSheet) {
mergeSheetAllRegion(fromSheet, toSheet);
//设置列宽
for(int i=0;i 0) {
for (int i = 0; i < fileList.size(); i++) {
String ddType = fileList.get(i).getProfileType();
String tableId = fileList.get(i).getCognateTableId();
Map map=Maps.newHashMap();
map.put("ddType", ddType);//ddType:业务类型
map.put("tableId", tableId);//tableId:数据库表某条记录的主键id
mapList.add(i,map);
}
}
...
//2.遍历每条记录,根据不同的业务类型进行处理
if (mapList != null && mapList.size() > 0) {
//2-1.指定总excel的模板文件
String pathFrom = "/exceltemplates/allFiles.xlsx";
@SuppressWarnings("resource")
XSSFWorkbook fromWorkbook=new XSSFWorkbook(当前类.class.getResourceAsStream(pathFrom));
XSSFWorkbook toWorkbook=new XSSFWorkbook();
XSSFSheet fromSheet=null;
//2-2.遍历,开始生成excle文件的sheet页
Map map=new HashMap();
for (int i = 0; i < mapList.size(); i++) {
String ddType = mapList.get(i).get("ddType");
String tableId = mapList.get(i).get("tableId");
if (ddType.equals("A")) {
fromSheet=fromWorkbook.getSheetAt(0);
toWorkbook=xxxService.createFile(toWorkbook,fromSheet,tableId);
} else if (ddType.equals("B")) {
...
}
...//省略其他十几个业务类型
}
}
//3.输出总excle文件到tempurl下
String allDdFilesName = "总文件_" + format.format(new Date()) + ".xlsx";
FileOutputStream workbookOut = new FileOutputStream(tempUrl + allDdFilesName);
toWorkbook.write(workbookOut);
workbookOut.flush();
workbookOut.close();
...
其中某一个业务类的生成excle的方法实现:
public XSSFWorkbook createFile(XSSFWorkbook toWorkbook,XSSFSheet fromSheet,String tableId) throws IOException{
//1.对sheet进行命名
XSSFSheet toSheet=toWorkbook.createSheet();
int sheetAmount=toWorkbook.getNumberOfSheets();
int sheetMatchAmount=0;
for(int i=0;i |