栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springboot 输出Excel word列表

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Springboot 输出Excel word列表

springboot 导出excel word
  • pom.xml
    • excel
    • word

pom.xml

        org.freemarker
        freemarker
        2.3.30
    
    
    
        org.apache.poi
        poi
        3.14
    
excel
https://blog.csdn.net/weixin_44824381/article/details/103409787
File file = new File("XXX\te.xls");
      OutputStream output = new FileOutputStream(file);
      workbook.write(output);
      workbook.close();
word
		 
	//  @SuppressWarnings("unchecked")
	  public static void createWord(Map dataMap, String templateName, String filePath,
	      String fileName) {
	    try {
	      //创建配置实例
	      Configuration configuration = new Configuration();
	
	      //设置编码
	      configuration.setDefaultEncoding("UTF-8");
	      //获取根目录
	      File path = new File(ResourceUtils.getURL("classpath:").getPath());
	      if (!path.exists()) {
	        path = new File("");
	      }
	      System.out.println("path:" + path.getAbsolutePath());
	      //若是上传目录为/templates/,则能够以下获取:
	      File upload = new File(path.getAbsolutePath(), "templates/");
	      if (!upload.exists()) {
	        upload.mkdirs();
	      }
	      System.out.println("upload22222222 url:" + upload.getAbsolutePath());
	      //指定路径的第一种方式,
	      //configuration.setClassForTemplateLoading();
	      //指定路径的第二种方式,具体路径
	      upload = new File("XXXXXXX\");
	      System.out.println("upload22222222 url:" + upload.getAbsolutePath());
	      configuration.setDirectoryForTemplateLoading(upload);
	
	//      //ftl模板文件
	//      configuration.setClassForTemplateLoading(WordUtils.class,"/");
	//
	      //获取模板
	      Template template = configuration.getTemplate(templateName);
	
	      //输出文件
	      File outFile = new File(filePath + File.separator + fileName);
	
	      //如果输出目标文件夹不存在,则创建
	      if (!outFile.getParentFile().exists()) {
	        outFile.getParentFile().mkdirs();
	      }
	
	      //将模板和数据模型合并生成文件
	      Writer out = new BufferedWriter(
	          new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));
	
	      //生成文件
	      template.process(dataMap, out);
	
	      //关闭流
	      out.flush();
	      out.close();
	    } catch (Exception e) {
	      e.printStackTrace();
	    }
	  }
	2、Main方法
	  dataMap.put("product", dataMap);
		dataMap.put("listInfo", list);
	//文件唯一名称
 	String fileonlyName = "生成Word文档.doc";
	
	WordUtils.createWord(dataMap, "test.ftl", filePath, fileOnlyName);

3、freemarker标签
	<#list listInfo as list> 

##效果
1、word:

2、excel

1、http://www.javashuo.com/article/p-dinlgnjl-nq.html
2、https://www.cnblogs.com/h-java/p/10026850.html

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/282139.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号