使用freemarker导出EXCEL WPS可以打开EXCEL2016打不开问题解决

导出代码如下:
public void freeMarkerDownTest(HttpServletResponse response, HttpServletRequest request)
			throws UnsupportedEncodingException {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = null;
		for (int i = 1; i < 10; i++) {
			map = new HashMap<String, Object>();
			map.put("dx", i);
			map.put("class", i + "班");
			map.put("d1", i + "1%");
			map.put("d2", i + "2%");
			map.put("d3", i + "3%");
			map.put("d4", i + "4%");
			map.put("d5", i + "5%");
			map.put("d6", i + "6%");
			map.put("d7", i + "7%");
			map.put("d8", i + "8%");
			list.add(map);
		}
		Map<String, Object> testMap = new HashMap<String, Object>();
		testMap.put("datas", list);
		Configuration configuration = new Configuration();
		FreeMarkerUtil handler = new FreeMarkerUtil(configuration);
		response.setContentType("application/octet-stream;charset=utf-8");
		String fileName = FileControl.encodeFileName(request, "2017-10-23 -学校考勤报表.xls");
		response.setHeader("Content-Disposition", "attachment;" + fileName);
		try {
			String templatePath = "E:/zlsu/Document/marking/framktodoc/";
			handler.write(templatePath, "freemark测试1.xml", testMap, response.getWriter());
			System.out.println("导出成功!");
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
 

导出完用WPS 打开正常 用EXCEL2016 打开出现文件错误如下:

解决方法:

 用OpenOffice.org 3.4.1 打开EXCEL 文件 然后把它 用Oppenoffice 另存为XML, 然后插入对应的 freemarker 指令 把这个 xml文件作为 导出的模板, 导出的excel 就能用

EXCEL2016 打开了如下图:

  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您可以使用Freemarker模板引擎来导出Excel文件。以下是一些步骤和示例代码来实现这个过程: 1. 首先,确保您的项目中包含了Freemarker的依赖。 2. 创建一个模板文件,其中定义了要导出Excel的数据格式和布局。例如,您可以使用FreeMarker的标记语法来创建表格、行和单元格。 3. 在您的Java代码中,加载并解析该模板文件。您可以使用Freemarker提供的Configuration类来完成此操作。 示例代码: ```java // 导入 Freemarker 相关的类 import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; // 创建 Configuration 对象,并设置模板文件所在的目录 Configuration configuration = new Configuration(Configuration.VERSION_2_3_30); configuration.setClassForTemplateLoading(YourClassName.class, "/templates"); // 加载模板文件 Template template = configuration.getTemplate("excel_template.ftl"); // 创建一个数据模型对象,用于填充模板中的数据 Map<String, Object> dataModel = new HashMap<>(); dataModel.put("dataList", yourDataList); // yourDataList 是要导出的数据列表 // 创建一个 Writer 对象,用于将生成的 Excel 内容写入到文件或输出流中 Writer writer = new FileWriter(new File("output.xls")); // 使用模板和数据模型生成 Excel 文件 try { template.process(dataModel, writer); } catch (TemplateException e) { e.printStackTrace(); } // 关闭 Writer 对象 writer.close(); ``` 在上面的示例中,您需要提供一个模板文件(`excel_template.ftl`)来定义导出Excel文件的格式。您可以在模板文件中使用FreeMarker的标记语法来创建表格、行和单元格,并使用数据模型中的数据填充相应的位置。 请注意,您需要将模板文件放置在项目的 `resources/templates` 目录下(或者根据实际情况进行调整),并且确保您的数据模型(`yourDataList`)中包含要导出的数据。 希望这可以帮助到您!如果有任何进一步的问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值