1. 程式人生 > >將頁面的列表資料匯出到excel檔案中

將頁面的列表資料匯出到excel檔案中

1、用的是springMVC的模式

2、在service介面層

//根據頁面傳過來的id在庫裡查詢

List<TbItem> selectByPrimaryKeydhh(long ids);

     在service介面的實現類中

    @Override
    public List<TbItem> selectByPrimaryKeydhh(long ids) {

//執行查詢的sql

        List<TbItem> selectByPrimaryKey = tbItemMapper.selectByPrimaryKeydhh(ids);
        return selectByPrimaryKey;
    }

3、在controller層

@Controller
public class Example {
    //定義檔案的格式
    private static final ReportFileTypeEnum X = ReportFileTypeEnum.XLS;
    @Autowired
    private ItemService itemService;
                        
    @RequestMapping(value="/partExportdhh",method=RequestMethod.POST)
    public  void maiddhhn(long ids) {
        List<Model> dataCode0 = new ArrayList<Model>();
        List<TbItem> list = itemService.selectByPrimaryKeydhh(ids);
        for (int i = 0; i < list.size(); i++) {
            
            Model model=new Model();
             TbItem tbItem = list.get(i);
             model.set("id",tbItem.getId());
             model.set("title",tbItem.getTitle());
             model.set("sell_point",tbItem.getSellPoint());
             model.set("price",tbItem.getPrice());
             model.set("num",tbItem.getNum());
             model.set("barcode",tbItem.getBarcode());
             model.set("image",tbItem.getImage());
             model.set("cid",tbItem.getCid());
             model.set("status",tbItem.getStatus());
             model.set("created",tbItem.getCreated());
             model.set("updated",tbItem.getUpdated());
            
            dataCode0.add(model);
        }

        LinkedHashMap<String, String> headersCode = new LinkedHashMap<String, String>();
        headersCode.put("id","商品ID");
        headersCode.put("title","商品標題");
        headersCode.put("sell_point","葉子類目");
        headersCode.put("price","價格");
        headersCode.put("num","庫存數量");
        headersCode.put("barcode","條形碼");
        headersCode.put("image","圖片");
        headersCode.put("cid","商品類目ID");
        headersCode.put("status","狀態");
        headersCode.put("created","建立時間");
        headersCode.put("updated","更新日期");
 

        //code2列表的模板
        String code2temlateUrl = "D:\\export\\code2" + X.getStatusDesc();
        //實際匯出來的資料
        String copytemplate2Url = "D:\\export\\copy" + X.getStatusDesc();
        //如果不存在這個資料夾就會建立這個資料夾
        if (!new File("D:\\export").exists()) {
            new File("D:\\export").mkdir();
        }
        HashMap<Integer, Integer> width = new HashMap<>();
        //設定這個excle會匯出到少個列
        width.put(0, 3);
        width.put(1, 3);
        width.put(2, 3);
        width.put(3, 3);
        width.put(4, 3);
        width.put(5, 3);
        width.put(7, 3);
        width.put(8, 3);
        width.put(9, 3);
        width.put(10, 3);
        // 根據模板匯出資料
        Utils.code2Tempalte(code2temlateUrl, dataCode0.size(), "商品列表", 12, X, width,
                headersCode.values().toArray());
        File file = new File(code2temlateUrl);
        if (!file.exists() || !file.isFile()) {
            System.out.println("檔案不存在");
            return;
        }
        Utils.copyTemplate(file, copytemplate2Url, X);

        try {
            Workbook wb = Utils.getWorkbookInstance(X, new FileInputStream(copytemplate2Url));
            int rowNo = 2;
            int rowSum = 0;
            Utils.file2FileWithData(wb, X, dataCode0, rowNo, 0, 12,headersCode.keySet().toArray());
            rowSum = 2 + dataCode0.size();
            FileOutputStream out = null;
            out = new FileOutputStream(copytemplate2Url);
            wb.write(out);
            System.out.println("匯出成功");
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

4、對於controller中用到的幾個工具類

//判斷excel檔案的字尾格式

public enum ReportFileTypeEnum {

    XLSX(0, ".xlsx"),
    XLS(1, ".xls");

    int status;
    String statusDesc;

    private ReportFileTypeEnum(int status,String statusDesc){
        this.status=status;
        this.statusDesc=statusDesc;
    }

    public int getStatus(){
        return status;
    }

    public String getStatusDesc(){
        return statusDesc;
    }

    public static ReportFileTypeEnum getType(int status){
        for (ReportFileTypeEnum excelFileTypeEnums : ReportFileTypeEnum.values()) {
            if (excelFileTypeEnums.getStatus() == status) {
                return excelFileTypeEnums;
            }
        }
        return null;
    }

    public static String getStatusDesc(int status){
        return getType(status).getStatusDesc();
    }
}

//用來封裝實體類

public class Model{

    private Map<String, Object> attrs = new HashMap<String, Object>();

    public void set(String attr, Object value) {
        attrs.put(attr, value);
    }

    public Object get(String attr) {
        return attrs.get(attr);
    }

    public Set entrySet() {
        return attrs.entrySet();
    }

4、需要引入poi的jar包的

          <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.8</version>
        </dependency>