Java 導出 Excel 文件

目前,不少報表,都須要導出Excel文件apache

首先,mavenjson

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

標題對象maven

@Data
public class ExcelTitleName {

    String name;

    String value;

    public ExcelTitleName(String value, String name) {
        this.name = name;
        this.value = value;
    }
}

標題ui

 public static List<ExcelTitleName> getOrder() {
        List<ExcelTitleName> orderMap = new ArrayList<>();
        orderMap.add(new ExcelTitleName("orderId", "訂單號"));
        orderMap.add(new ExcelTitleName("orderType", "訂單類型"));
        orderMap.add(new ExcelTitleName("createTime", "下單時間"));
        orderMap.add(new ExcelTitleName("userName", "買家名稱"));
        orderMap.add(new ExcelTitleName("status", "訂單狀態"));
        orderMap.add(new ExcelTitleName("supplierName", "供應商名稱"));
        orderMap.add(new ExcelTitleName("paymentName", "付款類型"));
        orderMap.add(new ExcelTitleName("payStatus", "付款狀態"));
        orderMap.add(new ExcelTitleName("payTime", "付款時間"));
        orderMap.add(new ExcelTitleName("payType", "付款方式"));
        orderMap.add(new ExcelTitleName("shipType", "物流方式"));
        orderMap.add(new ExcelTitleName("goodsAmount", "商品總價"));
        orderMap.add(new ExcelTitleName("shipAmount", "物流費用"));
        orderMap.add(new ExcelTitleName("couponAmount", "折扣金額"));
        orderMap.add(new ExcelTitleName("orderAmount", "訂單金額"));
        return orderMap;
    }

解析過程this

 /**
     * data 轉化爲Excel
     */
    public void builderOrderExcel(Workbook book, List list,List<ExcelTitleName> map,String title) {
        if(CollectionUtils.isEmpty(list)){
            return;
        }
        JSONArray jsonArray = JSON.parseArray(JSON.toJSONString(list));
        jsonArray.add(0, new Object()); //標題佔位
        Sheet sheet1 = book.createSheet(title);
        Row titleRow = sheet1.createRow(0);
        for (int i = 0; i < map.size(); i++) {
            Cell cell = titleRow.createCell(i);
            cell.setCellValue(map.get(i).getName());
        }

        for (int rowi = 0; rowi < jsonArray.size(); rowi++) {
            Row row = sheet1.createRow(rowi);
            for (int contentj = 0; contentj < map.size(); contentj++) {
                Cell cell = row.createCell(contentj);
                if (rowi == 0) {
                    //設置標題
                    cell.setCellValue(map.get(contentj).getName());
                } else {
                    //設置內容
                    Object value = jsonArray.getJSONObject(rowi).get(map.get(contentj).getValue());
                    if (value != null) {
                        cell.setCellValue(String.valueOf(value));
                    } else {
                        cell.setCellValue("");
                    }
                }
            }
        }

    }

調用spa

  //建立Excel
        Workbook workbook = new HSSFWorkbook();
        //建立order
        deliveryGoodsResNberExcel.builderOrderExcel(workbook, data.getOrderList(),
                OrderExportUtil.getOrder(), "訂單");
相關文章
相關標籤/搜索