jsp导出excel

来源:互联网 发布:js 面向对象 编辑:程序博客网 时间:2024/05/06 03:20

前台


window.location.href = '${pageContext.request.contextPath }/stock/exportExcel?data='+str+'&tableName='+tableName;


后台


@RequestMapping(value = "/exportExcel",method = RequestMethod.GET)
    public void exportExcel( String data, String[] tableName,HttpServletResponse response) {
        response.reset();
        response.setContentType("application/msexcel;charset=UTF-8");
        try {
            String filename = new String(("库存查询亲戚其次").getBytes("gb2312"), "ISO-8859-1");
            response.setHeader("Content-disposition", "inline;filename="+ filename + ".xls");// 定义文件名
            HSSFWorkbook newbook = stockManageService.exportExcel(data, tableName);
            newbook.write(response.getOutputStream());
            response.getOutputStream().flush();
            response.getOutputStream().close();
        } catch (IOException e) {
            e.printStackTrace();
            logger.info("========/exportExcel===exportExcel(3)-----exception--", e);
        }

    }


//poi excel 代码

@Override
    public HSSFWorkbook exportExcel(String data, String[] tableName) {
        HSSFWorkbook newbook = new HSSFWorkbook();
        HSSFSheet sheet = newbook.createSheet("sheet1");
        HSSFRow row = sheet.createRow(0);
        row.setHeight((short)350);
        HSSFCell cell = null;
        if (tableName != null && tableName.length > 0) {
            for (int i = 0; i < tableName.length; i++) {
                cell = row.createCell(i);
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);// 定
                cell.setCellValue(tableName[i]);// 在
                sheet.autoSizeColumn(i);
            }
        }
        HSSFCell cell0 = null;
        HSSFCell cell1 = null;
        HSSFCell cell2 = null;
        HSSFCell cell3 = null;
        HSSFCell cell4 = null;
        HSSFCell cell5 = null;
        HSSFCell cell6 = null;
        HSSFCell cell7 = null;
        JSONArray array = JSONArray.fromObject(data);
        for (int i = 0; i < array.size(); i++) {
            StockListInfo stockListInfo = (StockListInfo) JSONObject.toBean((JSONObject) array.get(i), StockListInfo.class);
            row = sheet.createRow(i + 1);
            row.setHeight((short)350);
            cell0 = row.createCell(0);
            cell1 = row.createCell(1);
            cell2 = row.createCell(2);
            cell3 = row.createCell(3);
            cell4 = row.createCell(4);
            cell5 = row.createCell(5);
            cell6 = row.createCell(6);
            cell7 = row.createCell(7);
            cell0.setCellType(HSSFCell.CELL_TYPE_STRING);// 定
            cell0.setCellValue(stockListInfo.getSkuId());// 在
            cell1.setCellType(HSSFCell.CELL_TYPE_STRING);// 定
            cell1.setCellValue(stockListInfo.getSkuName());// 在
            cell2.setCellType(HSSFCell.CELL_TYPE_STRING);// 定
            cell2.setCellValue(stockListInfo.getWarehouseName());// 在
            cell3.setCellType(HSSFCell.CELL_TYPE_STRING);// 定
            cell3.setCellValue(stockListInfo.isHasClearance());// 在
            cell4.setCellType(HSSFCell.CELL_TYPE_STRING);// 定
            cell4.setCellValue(stockListInfo.getGoodQuantity());// 在
            cell5.setCellType(HSSFCell.CELL_TYPE_STRING);// 定
            cell5.setCellValue(stockListInfo.getBadQuantity());// 在
            cell6.setCellType(HSSFCell.CELL_TYPE_STRING);// 定
            cell6.setCellValue(stockListInfo.getLockQuantity());// 在
            cell7.setCellType(HSSFCell.CELL_TYPE_STRING);// 定
            cell7.setCellValue(stockListInfo.getCanSaleQuantity());// 在
        }
        sheet.setColumnWidth(0, (short)3000);
        sheet.setColumnWidth(1, (short)6000);
        sheet.setColumnWidth(2, (short)6000);
        return newbook;
    }

0 0