POI 复制不同Sheet合成Excel文件,完美解决单元格样式和角标问题

项目背景:

1、需要从不同的Excel模板中读取Sheet,生成到汇总的Excel文件中

2、Excel格式是 Office2007版本,xlsx格式

3、需要将模板文件的内容和格式,拷贝到新的文件

4、解决POI 复制格式异常问题


工具代码:

package com.swing.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

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 PoiUtil2 {
	 public class XSSFDateUtil extends DateUtil {  

	    }  
	    public static void copyCellStyle(XSSFCellStyle fromStyle, XSSFCellStyle toStyle) {
	        toStyle.cloneStyleFrom(fromStyle);//此一行代码搞定
	    }  
	    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<=fromSheet.getRow(fromSheet.getFirstRowNum()).getLastCellNum();i++){ 
	            toSheet.setColumnWidth(i,fromSheet.getColumnWidth(i)); 
	        } 
	        for (Iterator rowIt = fromSheet.rowIterator(); rowIt.hasNext();) {  
	            XSSFRow oldRow = (XSSFRow) rowIt.next(); 
	            XSSFRow newRow = toSheet.createRow(oldRow.getRowNum()); 
	            copyRow(wb,oldRow,newRow);  
	        }  
	    }  
	    

	    public static void main(String[] args) {
	    	List pathList = new ArrayList();
	    	pathList.add("d:/swingPrint/printTemplate/" + "3容量法100_4" + ".xlsx");
//	    	pathList.add("d:/swingPrint/printTemplate/" + "48藻类计数检测原始记录_1" + ".xlsx");
	    	//将所有类型的尽调excel文件合并成一个excel文件
	    	XSSFWorkbook newExcelCreat = new XSSFWorkbook(); 
	    	try {
				
				for(int i = 0;i

效果:

多张Sheet页复制没问题

Sheet 页的上角标,下角标正常

POI 复制不同Sheet合成Excel文件,完美解决单元格样式和角标问题_第1张图片

 

POI 复制不同Sheet合成Excel文件,完美解决单元格样式和角标问题_第2张图片

如果文章对你有用,可以请我一罐八宝粥,^_^

你可能感兴趣的:(Java基础)