码迷,mamicode.com
首页 > 其他好文 > 详细

POI

时间:2019-07-10 11:00:39      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:length   下载   servlet   ++   tput   class   splay   src   xss   

操作Excel

实现的图示:

技术图片

 


软件/工具:POI包

 代码:

技术图片
//自动调整宽高
    private static void setSizeColumn(XSSFSheet sheet, int size) {
        for (int columnNum = 0; columnNum < size; columnNum++) {
            int columnWidth = sheet.getColumnWidth(columnNum) / 256;
            for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
                XSSFRow currentRow;
                // 当前行未被使用过
                if (sheet.getRow(rowNum) == null) {
                    currentRow = sheet.createRow(rowNum);
                } else {
                    currentRow = sheet.getRow(rowNum);
                }
                if (currentRow.getCell(columnNum) != null) {
                    XSSFCell currentCell = currentRow.getCell(columnNum);
                    if (currentCell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
                        int length = currentCell.getStringCellValue().getBytes().length;
                        if (columnWidth < length)
                            columnWidth = length;
                    }
                }
            }
            columnWidth = columnWidth * 256;
            sheet.setColumnWidth(columnNum, columnWidth >= 65280 ? 6000 : columnWidth);
        }
    }

    public static void main(String[] args) {
        HttpServletResponse response;
        OutputStream fileOut = null;
        try {
            String[] alias = { "头像", "名称", "个人团体", "志愿者人才", "性别" };
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet();
            workbook.setSheetName(0, "人才、志愿者");
            XSSFRow row = sheet.createRow(0);
            sheet.setColumnWidth(0, 2048);
            XSSFCell cell;
            XSSFCellStyle cellStyle = workbook.createCellStyle();
            XSSFFont font = workbook.createFont();
            font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
            cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);// 居中
            cellStyle.setFont(font);// 加粗
            // 创建标题
            for (int i = 0; i < alias.length; i++) {
                cell = row.createCell(i);
                cell.setCellValue(alias[i]);
                cell.setCellStyle(cellStyle);
            }
            // 遍历
            for (int i = 1; i < alias.length; i++) {
                row = sheet.createRow(i);//操作第i列
                for (int j = 0; j <= 4; j++) {
                    //如果第三列要存图片
                    if (j == 2) {
                        BufferedImage bufferImg =  ImageIO.read(new File("C:/person.png"));
                        ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
                        ImageIO.write(bufferImg, "png", byteArrayOut);
                        byte[] data = byteArrayOut.toByteArray();
                        XSSFDrawing drawingPatriarch = sheet.createDrawingPatriarch();
                        //(short)2, i, (short)3  中的2 ,3 表示图片从第2列到第3列 = 第三列存储图片
                        XSSFClientAnchor anchor = new XSSFClientAnchor(480, 30, 700, 250, (short)2, i, (short)3, i + 1);
                        drawingPatriarch.createPicture(anchor, workbook.addPicture(data, XSSFWorkbook.PICTURE_TYPE_PNG));
                        sheet.setColumnWidth((short)1000, (short)1000);
                        row.setHeight((short)1000);
                    }else{
                        cell = row.createCell(j);
                        cell.setCellValue(alias[j]);
                    }
                }
            }
            //调整宽高
              for (int i = 1; i < alias.length; i++){ 
                  sheet.autoSizeColumn(i);
                  setSizeColumn(sheet, alias.length); 
              }
            //输出文件
            fileOut = new FileOutputStream("D:/output_Excel.xls");
            workbook.write(fileOut);
            System.out.println("生成成功!");
        } catch (IOException io) {
            io.printStackTrace();
        } finally {
            if (fileOut != null) {
                try {
                    fileOut.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
View Code

 美化:

  通过调整XSSFCellStyle后赋值给  行/列 即可


若是直接下载不用输出到本地的话。将XSSFWorkbook写入到response获得的输出流即可将文件下载。

response.setContentType("octets/stream");
String excelName = "企业信息";
response.addHeader("Content-Disposition", "attachment;filename="+new String(excelName.getBytes("gb2312"), "ISO8859-1" )+".xls");
OutputStream out = response.getOutputStream();
workbook.write(out);

  

POI

标签:length   下载   servlet   ++   tput   class   splay   src   xss   

原文地址:https://www.cnblogs.com/hunter-007/p/11162136.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!