JXLS模板导出excel--项目版

JXLS模板导出EXLS

项目用到,记录下。

1、maven依赖

<dependency>  
    <groupId>net.sf.jxlsgroupId>  
    <artifactId>jxls-coreartifactId>  
    <version>1.0.5version>  
dependency> 

2、ExportController.java

package com.rivers.shopkeeper.controller.export;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Objects;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.common.collect.Maps;
import com.rivers.shopkeeper.dto.export.ExportOrderDTO;
import com.rivers.shopkeeper.service.export.ExportService;

import net.sf.jxls.transformer.XLSTransformer;

/**
 * 导出
 * 
 * @author xiyan
 *
 */
@Controller
@Scope("prototype")
@RequestMapping(value = "/s/export")
public class ExportController {

    @Autowired
    private ExportService service;

    /**
     * 导出订单
     */
    @GetMapping(value = "/exportExcelOrder")
    public void exportExcelOrder(HttpServletResponse response, String orderCode, String mobile) {
        ExportOrderDTO dto = service.findExportOrderDTOByOrderCode(orderCode, mobile);
        if (Objects.nonNull(dto)) {
            Map beans = Maps.newHashMap();
            beans.put("entity", dto);
            XLSTransformer transformer = new XLSTransformer();
            InputStream in = null;
            OutputStream out = null;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmss");
            String outputFileName = "OrderDetail_" + sdf.format(new Date()) + ".xlsx";
            response.setHeader("Content-Disposition", "attachment;filename=" + outputFileName);
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8");
            try {
                URL url = this.getClass().getResource("order_template.xlsx");
                File file = new File(url.getFile());
                in = new BufferedInputStream(new FileInputStream(file));
                Workbook workbook = transformer.transformXLS(in, beans);
                out = response.getOutputStream();
                workbook.write(out);
                out.flush();
            } catch (InvalidFormatException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

3、order_template.xlsx

JXLS模板导出excel--项目版_第1张图片

4、order_template.xlsx 存放位置

位置

——-end——-

你可能感兴趣的:(java,JXLS,java)