poi 匯出 例項

張天轅發表於2020-11-05
  PackagePlanEntity packagePlanEntity = packagePlanDao.selectById(id);
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFFont createFont = workbook.createFont();
        createFont.setFontHeightInPoints((short) 12);// 設定字型

        XSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setAlignment(HorizontalAlignment.LEFT); // 內容居左
        cellStyle.setFont(createFont);

        XSSFCellStyle cellStyle1 = workbook.createCellStyle();
        cellStyle1.setAlignment(HorizontalAlignment.CENTER);// 水平居中
        cellStyle1.setFont(createFont);
        cellStyle1.setVerticalAlignment(VerticalAlignment.CENTER);	//上下居中
        XSSFSheet sheet = workbook.createSheet("組裝計劃列表");
        //獲取行
        XSSFRow row0 = sheet.createRow(0);
        String[] title = {"組裝計劃名稱","組裝計劃說明","組裝計劃建立時間","傳送狀態","組裝計劃建立人","組裝計劃辦理人","組裝計劃抄送人"};
        for (int i = 0; i < title.length; i++) {
            XSSFCell title0 = row0.createCell(i);
            title0.setCellValue(title[i]);
            if(i==7){
                sheet.setColumnWidth(i, 8000);//物料名稱一列
            }else{
                sheet.setColumnWidth(i, 4500);
            }
            title0.setCellStyle(cellStyle1);
        }
        XSSFRow row1 = sheet.createRow(1);

        XSSFCell cell0 = row1.createCell(0);
        cell0.setCellValue(packagePlanEntity.getPackagePlanName());
        cell0.setCellStyle(cellStyle1);

        XSSFCell cell1 = row1.createCell(1);
        cell1.setCellValue(packagePlanEntity.getPackagePlanExplain());
        cell1.setCellStyle(cellStyle1);

        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createAt = packagePlanEntity.getCreateAt();
        String format1 = format.format(createAt);
        XSSFCell cell2 = row1.createCell(2);
        cell2.setCellValue(format1);
        cell2.setCellStyle(cellStyle1);

		//下載
        ServletOutputStream fileOut = null;
        try {
            fileOut = response.getOutputStream();
            String fileName = new String("組裝計劃列表".getBytes("UTF-8"), "ISO8859-1");
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
            fileOut = response.getOutputStream();
            workbook.write(fileOut);
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            if (fileOut != null) {
                try {
                    fileOut.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }



    }

相關文章