从零开始—我的开源项目3:项目需求及设计

同学毕业后,当上了村官,致力于服务底层的社区人民。

聊天时为她每天的工作内容,回答说:这周的工作是把excel里面的几千条记录,整理成word文件。

钦佩这种风险精神的同时,不禁觉得这种重复性工作实在是有些枯燥乏味,于是拍着胸脯答应用程序搞定这一工作,期限是2小时。


需求:

从excel中读取记录,并填入含有固定格式的word文件的指定位置,生成word文件

分析:

手头有excel文件,及一个word的格式示例。

之前一直是用java做web相关,有poi操作读取excel的经验,但是没生成过word文件。

google了一下,确定了基本思路(附:参考资料http://genuinecx.blog.51cto.com/2890523/1331115

设计: 

1、在word模板中增加freemarker标记(格式${key})

2、另存为.xml格式,去掉多于字符。因为另存为.xml后,标记会被转义成下图格式,需删除掉${和idcard以及idcard和}之间的多于字符。

3、读取excel中的记录,组成Map格式的List数组(List<Map<String, Object>>)

4、生成word文件,并替换freemark标记。

具体实现代码如下:

package excel2word;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;

public class DocUtil {
	private Configuration configure = null;
	public DocUtil(){
        configure= new Configuration();
        configure.setDefaultEncoding("utf-8");
	}
	
    /**
     * 根据Doc模板生成word文件
     * @param dataMap Map 需要填入模板的数据
     * @param fileName 文件名称
     * @param savePath 保存路径
     */
    public void createDoc(Map<String, Object> dataMap, String downloadType, String savePath){
    	downloadType = "word_template";
    	//解决重名问题
    	int i = 1;
		savePath = "D:/temp/"+dataMap.get("name")+".doc";
		File file = new File(savePath);
		while (file.exists()) {
			savePath =  "D:/temp/"+dataMap.get("name")+"("+i+").doc";
			file = new File(savePath);
			i++;
		}
    	try{
                  //加载需要装填的模板
                  Template template  = null;
                  //加载模板文件
                  configure.setClassForTemplateLoading(this.getClass(),"/excel2word/template");
                  //设置对象包装器
                  configure.setObjectWrapper(new DefaultObjectWrapper());
                  //设置异常处理器
                  configure.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
                  //定义Template对象,注意模板类型名字与downloadType要一致
                  template= configure.getTemplate(downloadType + ".xml");
                  //输出文档
                  File outFile = new File(savePath);
                  Writer out = null;
                  out= new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"utf-8"));                                   
                  template.process(dataMap,out);
                  outFile.delete();
           }catch (Exception e) {
                  e.printStackTrace();
           }
    }
    
    public static void main(String[] args) throws Exception {
    	DocUtil dou = new DocUtil();
    	String path ="D:/temp/template.xls";
    	InputStream in = new FileInputStream(path);
    	Workbook book = new HSSFWorkbook(in);
        	Sheet sheet = book.getSheetAt(0);
        	for (int j=sheet.getFirstRowNum(); j < sheet.getLastRowNum(); j++) {
        		Row row = sheet.getRow(j);
        		if(null == row) {break;}
        		if(null == row.getCell(0)){break;};
        		Map<String, Object> dataMap = new HashMap<String, Object>();
        		dataMap.put("name", row.getCell(2));
        		dataMap.put("gender", row.getCell(3));
        		dataMap.put("nation", row.getCell(4));
        		String idcard = row.getCell(7).toString();
        		String y = null;
        		String m = null;
        		String birthday = null;
        		if (StringUtils.isNotEmpty(idcard)) {
        			if (idcard.length() == 15) {
        				y = "19"+idcard.substring(6,8);
        				m = idcard.substring(8,10);
        			} else {
        				y = idcard.substring(6,10);
        				m = idcard.substring(10,12);
        			}
        			birthday = y+"."+Integer.parseInt(m);
        		}
        		dataMap.put("birthday", birthday);
        		dataMap.put("education", row.getCell(5));
        		dataMap.put("idcard", row.getCell(7));
        		String data = row.getCell(6).toString();
        		String partdata = null;
        		if(StringUtils.isNotEmpty(data) && data.length() == 8) {
        			String year = data.substring(0, 4);
        			String mon = data.substring(4, 6);
        			String day = data.substring(6, 8);
        			partdata = year+"."+Integer.parseInt(mon)+"."+Integer.parseInt(day);
        		} else {
        			partdata = data;
        		}
        		dataMap.put("partdata", partdata);
        		dou.createDoc(dataMap, "", "");
        	}
    	}
}


遇到的问题:

忽略了excel中多sheet,导致读出莫名其妙的数据,困扰了一会;

生成的word文件数量和excel记录行数不一致,原因:name重复,文件被替换。没想到一个社区中这么多重名的资料。


此代码限制较多,xml是我事先做好的,放入了代码项目中。dataMap的key和excel的cell,以及路径都是在在代码中写死的,虽然完成了对小伙伴的承诺,顺利替换她的工作,并保证不会透露给她的领导,但是对代码和功能都不是很满意,希望能让不懂得编程的用户,也可以轻松完成这一工作,相信很多coder都和我有这一相同的毛病,以一个简单的目的开始,然后不断完善,完善,可能到最后连自己为什么开始的这一工作都忘记了。

后面的文章,我将记录我对代码的改造过程。





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值