网页转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
    评论
网页导出Word文件是指将网页内容换为可编辑的Word文档,使其具有更广泛的应用和传播方式。实现网页导出Word文件通常有两种方式。 第一种方式是使用在线换工具。有很多在线网页Word的工具可以免费使用,只需将网页链接或HTML代码输入到工具中,然后点击换按钮,工具将自动生成一个Word文档,其中包含了网页的内容、格式和图片等。这种方式适合于临时需要导出少量网页的用户,操作简单方便。 第二种方式是使用专业的网页Word软件。这些软件通常具有更强大的功能和更高的换质量,用户可以自定义换选项,如文件命名、样式格式、图片分辨率等。这种方式适合需要频繁导出大量网页的用户,可以提高换效率和质量。 无论使用哪种方式,网页导出Word文件都需要注意以下几点。首先,注意网页内容中的排版和格式,在换过程中可能会出现错位、字体不一致等问题,需要手动调整。其次,注意网页中的图片和链接,换后可能会失效,需要重新插入或修复。最后,导出的Word文件可能会比网页本身较大,需要注意存储空间和文件传输的方便性。 网页导出Word文件可以方便地保存网页内容、修改网页格式、与他人共享和打印等,具有广泛的应用场景。例如,学术研究人员可以将网页保存为Word文档进行论文写作和整理;企业可以将网页换为Word文件进行编辑和打印等。因此,网页导出Word文件是一个很实用的功能,可以提高人们处理网页内容的效率。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值