java操作excel 替换指定字符串

根据excel模板里设置指定的num0,num1,num2,num3.然后替换成想到的内容,生成新的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;

/**
 * Writer: fuanyu
 * Date  : 14-12-26
 * Time  : 上午9:43
 */

@SuppressWarnings("unchecked")
public class TestExcel {

     /**
     * 替换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("num0","000");
        item.put("num1","1");
        item.put("num2","2");
        item.put("num3","3");
        item.put("num4","4");
        item.put("num5","5");
        item.put("num6","6");

        //d:\\template.xls为Excel模板文件,d:\\test.xls为程序根据Excel模板文件生成的新文件
        replaceModel(item, "d:\\template.xls", "d:\\test.xls");

}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值