Spring Boot 使用JXLS導出Excel

  • 目錄結構
├─src
│  ├─main
│  │  ├─java
│  │  │  └─com
│  │  │      └─ranhan
│  │  │          │  ExportApplication.java
│  │  │          ├─controller
│  │  │          │      ExportController.java
│  │  │          ├─dao
│  │  │          │      OrderMapper.java
│  │  │          ├─model
│  │  │          │      Order.java
│  │  │          ├─service
│  │  │          │  │  MailService.java
│  │  │          │  │
│  │  │          │  └─impl
│  │  │          │          OrderExportExcelServiceImpl.java
│  │  │          └─utils
│  │  │                  JxlsUtil.java
│  │  └─resources
│  │      │  application.properties
│  │      ├─mapper
│  │      │      OrderMapper.xml
│  │      └─templates
│  │          ├─excel
│  │          │      order.xlsx

/**
 * Excel導出
 * @param templateName
 * @param data
 */
public String export(String templateName, Map<String, Object> data) {
    XLSTransformer transformer = new XLSTransformer();
    OutputStream out = null;
    String filename = DateUtil.getNewDate() + ".xlsx";
    InputStream stream = null;
    try {
        String templatePath = "src/main/resources/templates/excel/" + templateName;
        File template = ResourceUtils.getFile(templatePath);
        FileInputStream in = new FileInputStream(template);
        Workbook workbook = transformer.transformXLS(in, data);
        out = new FileOutputStream(new File(filename));
        workbook.write(out);
        out.flush();
        } catch (FileNotFoundException e) {
            logger.error("jxls:", e);
        } catch (InvalidFormatException e) {
            logger.error("jxls:", e);
        } catch (IOException e) {
            logger.error("jxls:", e);
        } finally {
        IOUtils.closeQuietly(stream);
        if (null != out) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
           }
    return filename;
}
  • 本地使用正常,打jar測試錯誤
    1. spring boot class path resource [templates/] cannot be resolved to absolute file path because it does not reside in the file system
    • 推測是文件路徑錯誤,對獲取文件路徑進行修改
    ClassLoader classLoader = this.getClass().getClassLoader();
    String templatePath = url.getPath() + "templates"+ File.separator + "excel" + File.separator + templateName;
    FileInputStream in = new FileInputStream(new File(templatePath));
    
    • 進行修改后打包還是導出失敗,進行調試
    • templatePath路徑為/E:/WorkSpase/export/target/classes/templates\excel\shop.xlsx
    • 我打卡這個模板,結果說損壞
    • 解決方案
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <configuration>
        <encoding>UTF-8</encoding>
        <nonFilteredFileExtensions>
           <nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
           <nonFilteredFileExtension>xls</nonFilteredFileExtension>
        </nonFilteredFileExtensions>
        </configuration>
    </plugin>
    
    1. java.io.FileNotFoundException: file:/app.jar!/BOOT-INF/classes!/templates/excel/shop.xlsx (No such file or directory)
    • 網上搜到的解決方案是使用流方式讀取文件,在最開始也改成流的形式,同樣是導出失敗,原因是excel文件格式進行了轉碼處理。現在又改成流的讀取方式可以正常導出。
    • 加入pom
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.6</version>
    </dependency>
    
    • 修改導出方法
    String templatePath = "templates"+ File.separator + "excel" + File.separator + templateName;
    stream = getClass().getClassLoader().getResourceAsStream(templatePath);
    File targetFile = new File(templateName);
    FileUtils.copyInputStreamToFile(stream, targetFile);
    FileInputStream in = new FileInputStream(targetFile);
    
    
    // 最后關閉流
    IOUtils.closeQuietly(stream);
    
  • freemarker導出
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容