java使用 freemarker 模板生成一个excel文件多个sheet

    /**
     * 导出Excel
     * @param request
     * @param
     * @return
     */
    @RequestMapping(value = "/jsp/map/导出.do", method = RequestMethod.POST)
    @ResponseBody
    public String  sxdcjgghbToExcel(HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam("zzdwdm") String zzdwdm,
            @RequestParam("outType") String outType) {
        try {
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8"); 
                //开始传入数据
//               Map<String,Object> map = new HashMap<String,Object>();
                 //生成excel的简单的类
                 XLSTransfor
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用freemarker提供的API,通过Java代码生成Word文件。首先,需要引入相关依赖,比如Apache POI和freemarker。然后,根据模板文件和数据,使用freemarker渲染模板,最后生成Word文件。 以下是代码示例: ```java import java.io.*; import java.util.*; import freemarker.template.*; import org.apache.poi.xwpf.usermodel.*; public class WordGenerator { public static void generate(Map<String, Object> data, String templateFilePath, String outputFilePath) { try { // Load template file Configuration configuration = new Configuration(Configuration.VERSION_2_3_31); configuration.setDirectoryForTemplateLoading(new File(templateFilePath)); Template template = configuration.getTemplate("template.ftl"); // Render template with data StringWriter writer = new StringWriter(); template.process(data, writer); // Save rendered content as Word file XWPFDocument document = new XWPFDocument(); XWPFParagraph paragraph = document.createParagraph(); XWPFRun run = paragraph.createRun(); run.setText(writer.toString()); FileOutputStream outputStream = new FileOutputStream(new File(outputFilePath)); document.write(outputStream); outputStream.close(); document.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { // Example data Map<String, Object> data = new HashMap<>(); data.put("title", "Example Word Document"); data.put("subtitle", "Generated by Freemarker and Apache POI"); data.put("content", "Lorem ipsum dolor sit amet, consectetur adipiscing elit."); String templateFilePath = "/path/to/template/dir"; String outputFilePath = "/path/to/output/docx"; generate(data, templateFilePath, outputFilePath); } } ``` 其中`template.ftl`是freemarker模板文件,通过在模板文件使用`${key}`的方式引用数据。 关于freemarker的具体使用方式和语法,可以参考官方文档:https://freemarker.apache.org/docs/。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值