求大神解答下JAVA怎么导出这种格式的EXL

图片说明

下面是JSON数据
{
"CARRIER": "妮妮",
"CAR_NUMBER": "5555",
"CREATE_DATE": "2017-09-28",
"CUSTOMER_NAME": "冬冬",
"ID": "ACF4B1F71C2405948CF1D6AC75DD5074",
"ORDER_TYPE": "0",
"ROW_ID": 1,
"STOCKCODE": "CK10035721197",
"STORE_NAME": "蛋仓",
"SUPPLIER_NAME": "鸭蛋供应商",
"skuInfoList": [
{
"GIFT_NUM": 4,
"GOODS_PRICE": 54.21,
"ID": "ACF4B1F71C2405948CF1D6AC75DD5074",
"METER_NAME": "根",
"OUT_NUM": 4,
"PACKAGE_SPEC": "2",
"PRODUCT_DENO": "b10302c7b7d840548a4f751c7e03d61e",
"PRODUCT_NAME": "华禽 鸡蛋 2",
"TOTALWEIGHT": 160
}
],
"trCount": 1
},

            下面是我的代码,商品没加上,求个思路

            OutputStream os = null;
    try {
        String mimetype = "application/x-msdownload";
        response.setContentType(mimetype);
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd-HH-mm");
        String downFileName = "出库明细" + sdf.format(new Date()) + ".xlsx";
        String inlineType = "attachment"; // 是否内联附件
        response.setHeader("Content-Disposition",
                inlineType + ";filename=\"" + new String(downFileName.getBytes(), "ISO8859-1") + "\"");
        // 
        PageData pd = this.getPageData();
        String listPageStr = service.listPageHistoryStockOut(pd.toJsonString());
        JSONObject listObject = JSON.parseObject(listPageStr);
        List<Object> list = listObject.getJSONArray("returnData");

        // 导出Excel
        os = response.getOutputStream();
        XSSFWorkbook wb = new XSSFWorkbook();
        Sheet sheet = wb.createSheet("sheet1");
        XSSFCellStyle s = wb.createCellStyle();// 样式对象 
        s.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
        s.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
        String[] title = { "出库编号", "出库类型","出库时间","供应商","仓库","客户/申请人","承运方","车牌号/快递单号" };
        String[] propertyName = { "STOCKCODE", "ORDER_TYPE","CREATE_DATE","SUPPLIER_NAME","STORE_NAME","CUSTOMER_NAME","CARRIER","CAR_NUMBER"};
        sheet.createRow(0);
        Row xssfR = sheet.createRow(0);
        for (int j = 0; j < title.length; j++) {
            Cell xssfCell = xssfR.createCell(j);
            xssfCell.setCellValue(title[j]);
            xssfCell.setCellStyle(s);
        }

        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> m = (Map<String, Object>) list.get(i);
            // sheet.addMergedRegion(new CellRangeAddress(1, 3, 0, 0)); 
            // 创建Excel的sheet的一行
            int r = i + 1;
            Row xssfRow = sheet.createRow(r);
            for (int j = 0; j < propertyName.length; j++) {
                    Object cell;
                    cell = m.get(propertyName[j]);
                    // 创建一个Excel的单元格
                    Cell xssfCell = xssfRow.createCell(j);
                    if (cell != null) {
                        xssfCell.setCellValue(cell.toString());
                    } else {
                        xssfCell.setCellValue("");
                    }
                }
            }
        //}
        // 自动列宽
        sheet.autoSizeColumn((short) 0);
        wb.write(os);
    } catch (UnsupportedEncodingException e) {
    } catch (IOException e) {
    } finally {
        try {
            os.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

1个回答

创建一个excel模板,在给模板的单元格赋值。

 @RequestMapping("/webapp/ApplicationVersionController/exportPbomCompare")
    public void exportPbomCompare(String viewId, String versionId, HttpServletRequest request,
            HttpServletResponse response) throws Throwable {
        //定义数据导出对象
        ExcelExport excelExport;
                //模板,模板名称,起始行
        excelExport = new ExcelExport("ReportTemplate_pbom_compare.xlsx", "匹配清单", 2);
        //设置sheet1
        excelExport.getSheet(0);
                //单元格赋值
        excelExport.copyRows(queryDataList, COLUMN);
        //数据导出
        excelExport.exportToNewFile(response);
    }
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐