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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Code_KK/article/details/78799306

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

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 java.io.FileInputStream;
import java.io.FileOutputStream;
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 {

    /**
     * 替换Excel模板文件内容
     * @param item 文档数据
     * @param sourceFilePath Excel模板文件路径
     * @param targetFilePath Excel生成文件路径
     */
    public static boolean replaceModel(Map item, String sourceFilePath, String targetFilePath) {
        boolean bool = true;
        try {

            POIFSFileSystem fs  =new POIFSFileSystem(new FileInputStream(sourceFilePath));
            HSSFWorkbook wb = new HSSFWorkbook(fs);
            HSSFSheet sheet = wb.getSheetAt(0);
            Iterator rows = sheet.rowIterator();
            while(rows.hasNext()){
                HSSFRow row = (HSSFRow) rows.next();
                if(row!=null) {
                    int num = row.getLastCellNum();
                    for(int i=0;i<num;i++) {
                        HSSFCell 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;
    }

    // 测试
    public static void main(String[] args) {

        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(item, path, path2);

    }

}

猜你喜欢

转载自blog.csdn.net/Code_KK/article/details/78799306