POI导出列表

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_39556804/article/details/84883682
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFCellStyle style = workbook.createCellStyle();//设置列样式
        style.setAlignment(HorizontalAlignment.CENTER);// 居中

        XSSFSheet sheet = workbook.createSheet();
        XSSFRow row;

        //设置宽度
        int[] column = { 3000, 4100, 5100, 4000, 6100, 4000, 4200, 9000, 4000 };
        for (int i = 0; i < column.length; i++) {
            sheet.setColumnWidth(i, column[i]);
        }

        //表头
        row = sheet.createRow(0);
        String[] title = { resource.getMessage("global.buttons.numbering"), resource.getMessage("activity.winning.list.imei"),
                resource.getMessage("activity.winning.list.time"), resource.getMessage("activity.winning.list.prize"),
                resource.getMessage("activity.winning.list.prizeCode"), resource.getMessage("activity.winning.list.userName"),
                resource.getMessage("activity.winning.list.userPhone"), resource.getMessage("activity.winning.list.userAddress"),
                resource.getMessage("activity.winning.list.activityName") };
        for (int j = 0; j < title.length; j++) {
            row.createCell(j).setCellValue(title[j]);
        }

        //列表
        int num = 1;
        for (ActivityWinningListDO winning : list) {
            row = sheet.createRow(num);
            row.createCell(0).setCellValue(num);
            row.createCell(1).setCellValue(winning.getImei());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            row.createCell(2).setCellValue(sdf.format(winning.getGmtCreate()));
            row.createCell(3).setCellValue(winning.getPrize());
            row.createCell(4).setCellValue(convert(winning.getPrizeCode()));
            row.createCell(5).setCellValue(convert(winning.getUserName()));
            row.createCell(6).setCellValue(convert(winning.getUserPhone()));
            row.createCell(7).setCellValue(convert(winning.getUserAddress()));
            row.createCell(8).setCellValue(winning.getActivityName());
        }

        //处理中文名乱码
        String agent = request.getHeader("USER-AGENT").toLowerCase();
        String fileName = resource.getMessage("activity.winning.name.list");
        if (agent.contains("firefox")) {
            response.setCharacterEncoding("utf-8");
            fileName = new String(fileName.getBytes(), "ISO8859-1");
        } else {
            fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
        }

        try {
            response.setHeader("Content-Disposition", "attchment;filename=" + fileName + ".xlsx");
            response.setHeader("Pargam", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            workbook.write(response.getOutputStream());
            response.getOutputStream().flush();
            response.getOutputStream().close();
        } catch (Exception e) {
            LOGGER.error("export feedback error" + e);
        }

猜你喜欢

转载自blog.csdn.net/weixin_39556804/article/details/84883682