POI

Posted hunter-007

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POI相关的知识,希望对你有一定的参考价值。

操作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的主要内容,如果未能解决你的问题,请参考以下文章

org.apache.poi.ss.usermodel 包可以从多个模块访问:poi、poi.ooxm

项目中 poi 如何多个版本并存

jasperreports 与poi的版本问题

poi是啥意思

java poi 操作ppt,该怎么解决

java poi的使用问题