网页转word


  1. 先写一个word模板,将变量用${} 样式填充在将要显示的值处
  2. List类型的数据,需要指定序号进行循环
  3. word模板另存为xml文件   注意:有时候会在${}之间多出来样式,需删掉
  4. controller的方法中new一个map,将要填充在word中的变量都放到map里,记得指定输出流的编码(utf-8),否则可能报内容有错误,不能读取word文档。
    	// 如果数据库中的数据为null,转换为0
    	public String toString (Object obj){
    		return (obj == null) ? "0" : obj.toString();
    	}
    	@RequestMapping(value = "/download")
    	public void export(HttpServletResponse response, Integer id) {
    		if (null != id) {
    			PaTrainWeekly paTrainWeekly = this.paTrainWeeklyService.findById(id);
    			List<PaTrainNewReceiveItems> receiveItems = this.paTrainNewReceiveItemsService.findByWeeklyId(id);
    			
    			Map<String, String> data = new HashMap<>();
    			data.put("year", toString(paTrainWeekly.getYear()));
    			data.put("month", toString(paTrainWeekly.getMonth()));
    			if (ObjectUtil.isNotBlank(receiveItems)) {
    				// 循环集合里的数据
    				for(int i = 0; i < receiveItems.size(); i++){
    					PaTrainNewReceiveItems tr = receiveItems.get(i);
    					if(tr != null){
    						String trainNum = toString(tr.getTrainNum());
    						String company = tr.getCompany();
    						String receiveNum = new SimpleDateFormat("yyyy-MM-dd").format(tr.getReceiveNum());
    						
    						// 第一列增加一个序号
    						data.put("num_"+(i+1), toString(i+1));
    						data.put("receiveItems_"+ (i+1) +"_trainNum", trainNum);
    						data.put("receiveItems_"+ (i+1) +"_company", company);
    					}
    				}
    				// word里暂定十条数据,因为不确定集合里有多少条数据,少于10条的用null代替。
    				if(10>receiveItems.size()){
    					for(int i = receiveItems.size(); 10> i; i++){
    						data.put("num_"+(i+1), "");
    						data.put("receiveItems_"+ (i+1) +"_trainNum", "");
    						data.put("receiveItems_"+ (i+1) +"_company","");
    					}
    				}
    			}
    			
    			String wordData = FileUtils.wordData(data, "trainWeekly.xml");
    			response.setCharacterEncoding("utf-8"); 
    			response.setContentType("application/msword");  
    			// 设置浏览器以下载的方式处理该文件名  
    			String fileName =  DateUtils.formatDate(new Date())+".doc";  
    			response.setHeader("Content-Disposition", "attachment;filename=".concat(fileName)); 
    			try {
    				OutputStream out = response.getOutputStream();
    				out.write(wordData.getBytes("UTF-8"));
    				out.flush();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
public static String wordData(Map<String, String> data, String template) {
	String tmp = readTemplate(template);
	logger.debug("template {}", tmp);
	Set<Entry<String, String>> set = data.entrySet();
	for (Entry<String, String> entry : set) {
		String k = entry.getKey();
		String v = entry.getValue();
		if (v == null) {
			v = "";
		}
		k = "${" + k + "}";
		tmp = tmp.replace(k, v);
	}

	return tmp;
}
public static String readTemplate(String name) {
	StringBuffer buffer = new StringBuffer();
	try {
		URL url = FileUtils.class.getResource("/files/" + name);

		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(url.getPath()), "UTF-8"));
		String line = null;
		while ((line = br.readLine()) != null) {
			buffer.append(line);
		}
		br.close();

	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return buffer.toString();
}
/**
 * 将Date格式化成符合默认日期格式的字符串
 * 
 * @param date
 * @return 返回样例:2012-03-29
 */
public static String formatDate(Date date) {
	SimpleDateFormat formatTool = new SimpleDateFormat();
	formatTool.applyPattern(DAY_PATTERN);
	return formatTool.format(date);
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
从多个网页的完整或选定内容创建Microsoft Word文档。 此扩展程序将完整的网页网页的选定部分换为Microsoft Word文档(docx)。您还可以选择将来自多个网页的内容或仅将其中的选定部分换为单个Word文档。换后的文档可以下载到本地磁盘。该扩展读取当前页面或所选部分HTML结构,并将其换为相应的docx结构。限制:-1.安装/更新-安装或更新后,此扩展名不适用于在安装/更新之前打开的标签页,除非重新加载了这些标签页或重新启动了chrome 2.我可以换什么? -此扩展程序可以将除以下页面之外的任何网页换为Microsoft Word文档-所有Chrome网上应用店页面(包括此页面) Urls以chrome://开头 chrome:// extensions以chrome-extension://开头的URL。3.某些换后的页面看起来不一样-所有内容都被换为Word文档,其中大部分格式保持不变。布局可能会有所不同 4.为什么某些网页保存的文件显示方形框? -对于非英语的网页,尤其会发生这种情况。目前,应用程序不支持所有语言所需的字体换 5.为什么本地HTML文件不起作用? -出于安全原因,Chrome浏览器不允许扩展名访问File Urls,除非得到用户的明确许可。如果您希望扩展程序适用于本地HTML文件,那么您要做的就是-1.访问扩展程序管理页面(chrome:// extensions) 2.到“将网页另存为Word文档”扩展名 3.选中“允许访问文件URL”复选框 4.重新加载本地HTML文件。 支持语言:English
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值