使用POI操作Excel修改模板(批量替换excel中的数据并判断excel版本)

package com.sycamore.controller;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.sycamore.util.LogUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PushbackInputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;


/**
 * Created by sunming on 2017/12/13.
 */
public class ExcelUtils {


    /**
     * 替换Excel2003版模板文件内容
     * @param item 文档数据
     * @param sourceFilePath Excel模板文件路径
     * @param targetFilePath Excel生成文件路径
     */
    public static boolean replaceModel2003(Map item, String sourceFilePath, String targetFilePath) {
        boolean bool = true;
        try {
        InputStream is = new FileInputStream(sourceFilePath);
            HSSFWorkbook wb = new HSSFWorkbook(is);
            HSSFSheet sheet = wb.getSheetAt(0);
            Iterator rows = sheet.rowIterator();
            while(rows.hasNext()){
            XSSFRow row = (XSSFRow) rows.next();
                if(row!=null) {
                    int num = row.getLastCellNum();
                    for(int i=0;i<num;i++) {
                    XSSFCell cell=  row.getCell(i);
                        if(cell!=null) {
                            cell.setCellType(XSSFCell.CELL_TYPE_STRING);
                        }
                        if(cell==null || cell.getStringCellValue()==null) {
                            continue;
                        }
                        String value= cell.getStringCellValue();
                        if(!"".equals(value)) {
                            Set<String> keySet = item.keySet();
                            Iterator<String> it = keySet.iterator();
                            while (it.hasNext()) {
                                String text = it.next();
                                if(value.equalsIgnoreCase(text)) {
                                    cell.setCellValue((String)item.get(text));
                                    break;
                                }
                            }
                        } else {
                            cell.setCellValue("");
                        }
                    }
                }
            }
            // 输出文件
            FileOutputStream fileOut = new FileOutputStream(targetFilePath);
            wb.write(fileOut);
            fileOut.close();
        } catch (Exception e) {
            bool = false;
            e.printStackTrace();
        }
        return bool;
    }
    
    /**
     * 替换Excel2007版模板文件内容
     * @param item 文档数据
     * @param sourceFilePath Excel模板文件路径
     * @param targetFilePath Excel生成文件路径
     */
    public static boolean replaceModel2007(Map item, String sourceFilePath, String targetFilePath) {
        boolean bool = true;
        try {
        InputStream is = new FileInputStream(sourceFilePath);
            XSSFWorkbook wb = new XSSFWorkbook(is);
            XSSFSheet sheet = wb.getSheetAt(0);
            Iterator rows = sheet.rowIterator();
            while(rows.hasNext()){
                XSSFRow row = (XSSFRow) rows.next();
                if(row!=null) {
                    int num = row.getLastCellNum();
                    for(int i=0;i<num;i++) {
                        XSSFCell cell = row.getCell(i);
                        if(cell!=null) {
                            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                        }
                        if(cell==null || cell.getStringCellValue()==null) {
                            continue;
                        }
                        String value= cell.getStringCellValue();
                        if(!"".equals(value)) {
                            Set<String> keySet = item.keySet();
                            Iterator<String> it = keySet.iterator();
                            while (it.hasNext()) {
                                String text = it.next();
                                if(value.equalsIgnoreCase(text)) {
                                    cell.setCellValue((String)item.get(text));
                                    break;
                                }
                            }
                        } else {
                            cell.setCellValue("");
                        }
                    }
                }
            }
            // 输出文件
            FileOutputStream fileOut = new FileOutputStream(targetFilePath);
            wb.write(fileOut);
            fileOut.close();


        } catch (Exception e) {
            bool = false;
            e.printStackTrace();
        }
        return bool;
    }
    
    
    


    /**
    * 判断Excel版本
    * */
   public static void replaceModel(String filePath,Map<String,Object> dataSource,String targetFilePath) throws Exception{
   
    /**
    *判断excel版本是2003还是2007版 
    * */
    InputStream inp = new FileInputStream(new File(filePath));
if(!inp.markSupported()) {
inp = new PushbackInputStream(inp, 8);
}
if(POIFSFileSystem.hasPOIFSHeader(inp)) {
LogUtil.info("进入2003版excel....");
 replaceModel2003(dataSource, filePath, targetFilePath);
}
if(POIXMLDocument.hasOOXMLHeader(inp)) {
LogUtil.info("进入2007版excel....");
replaceModel2007(dataSource, filePath, targetFilePath);
}
   
   }
    
    
    
    // 测试
    public static void main(String[] args) throws Exception {
        Map item = new HashMap();
        item.put("L-00001","L-00012");
        item.put("L-00002","L-00013");
        item.put("L-00003","L-00014");

        String path =  "C:\\Users\\sunming\\Desktop\\22\\22.xls";
        String path2 = "C:\\Users\\sunming\\Desktop\\22\\33.xls";
        replaceModel(path, item, path2);
    }

}





链接:http://blog.csdn.net/Code_KK/article/details/78799306

猜你喜欢

转载自blog.csdn.net/qq_37272886/article/details/79030707
今日推荐