POI 基本使用方法

来源:互联网 发布:网络直播原理 编辑:程序博客网 时间:2024/06/05 03:34

使用POI生成excel文件



public void buildExcelFile(List<Orde> list, OutputStream os) throws Exception{Workbook wb = new SXSSFWorkbook(); //创建一个文档Sheet sh = wb.createSheet();//创建一个片/* 表头 */{int cellIndex = 0 ;Row title = sh.createRow(0);//创建一行title.createCell(cellIndex++).setCellValue("供应商");  //创建单元格title.createCell(cellIndex++).setCellValue("ID");title.createCell(cellIndex++).setCellValue("预订日期");title.createCell(cellIndex++).setCellValue("酒店名");title.createCell(cellIndex++).setCellValue("入住日期");title.createCell(cellIndex++).setCellValue("退房日期");title.createCell(cellIndex++).setCellValue("客人姓名");title.createCell(cellIndex++).setCellValue("晚数");title.createCell(cellIndex++).setCellValue("房间数量");title.createCell(cellIndex++).setCellValue("间晚数");title.createCell(cellIndex++).setCellValue("员工姓名");title.createCell(cellIndex++).setCellValue("shouldPayIn");title.createCell(cellIndex++).setCellValue("payIn类型");title.createCell(cellIndex++).setCellValue("payIn");title.createCell(cellIndex++).setCellValue("payIn货币");title.createCell(cellIndex++).setCellValue("payIn银行");title.createCell(cellIndex++).setCellValue("shouldPayOut");title.createCell(cellIndex++).setCellValue("payOut类型");title.createCell(cellIndex++).setCellValue("payOut");title.createCell(cellIndex++).setCellValue("payOut货币");title.createCell(cellIndex++).setCellValue("信用卡号");title.createCell(cellIndex++).setCellValue("下单同业");    title.createCell(cellIndex++).setCellValue("利润");title.createCell(cellIndex++).setCellValue("查房");title.createCell(cellIndex++).setCellValue("查房描述");CellStyle style = wb.createCellStyle();//单元格样式style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);// 设置背景颜色模式style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);  // 设置背景颜色style.setAlignment(HSSFCellStyle.ALIGN_CENTER); Font font = wb.createFont();font.setFontName("黑体");font.setFontHeightInPoints((short) 13);//设置字体大小style.setFont(font);//设置字体for (int i = 0; i < cellIndex ; i++) {title.getCell(i).setCellStyle(style);sh.setColumnWidth(i, 4000);//设置列宽}}for (int i = 0; i < list.size() ; i++) {Orde orde = list.get(i);Row row = sh.createRow(i + 1);int cellIndex = 0 ;row.createCell(cellIndex++).setCellValue(obtainCellValue("orde.getSupplier.getName",orde)); row.createCell(cellIndex++).setCellValue(obtainCellValue("orde.getPId",orde));row.createCell(cellIndex++).setCellValue(obtainCellValue("orde.getBookingDate",orde));row.createCell(cellIndex++).setCellValue(obtainCellValue("orde.getHotelName",orde));{//入住日期(如果入住日期在今天之后, 显示为红色)Cell checkIn = row.createCell(cellIndex++);checkIn.setCellValue(obtainCellValue("orde.getInDate",orde));if(orde.getInDate().compareTo(new Date()) < 0 ){CellStyle style = wb.createCellStyle();style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);style.setFillForegroundColor(HSSFColor.TAN.index);checkIn.setCellStyle(style);}}row.createCell(cellIndex++).setCellValue(obtainCellValue("orde.getOutDate",orde));row.createCell(cellIndex++).setCellValue(obtainCellValue("orde.getGustName",orde));row.createCell(cellIndex++).setCellValue(obtainCellValue("orde.getNights",orde));row.createCell(cellIndex++).setCellValue(obtainCellValue("orde.getRooms",orde));row.createCell(cellIndex++).setCellValue(obtainCellValue("orde.getNrs",orde));row.createCell(cellIndex++).setCellValue(obtainCellValue("orde.getEmp.getName",orde));row.createCell(cellIndex++).setCellValue(obtainCellValue("orde.getShouldPayIn",orde));row.createCell(cellIndex++).setCellValue(obtainCellValue("orde.getPayInType.getName",orde));row.createCell(cellIndex++).setCellValue(obtainCellValue("orde.getPayIn",orde));row.createCell(cellIndex++).setCellValue(obtainCellValue("orde.getPayInCurrency",orde));row.createCell(cellIndex++).setCellValue(obtainCellValue("orde.getBank.getName",orde));row.createCell(cellIndex++).setCellValue(obtainCellValue("orde.getShouldPayOut",orde));row.createCell(cellIndex++).setCellValue(obtainCellValue("orde.getPayType.getName",orde));row.createCell(cellIndex++).setCellValue(obtainCellValue("orde.getPayOut",orde));row.createCell(cellIndex++).setCellValue(obtainCellValue("orde.getPayOutCurrency",orde));row.createCell(cellIndex++).setCellValue(obtainCellValue("orde.getCard",orde));row.createCell(cellIndex++).setCellValue(obtainCellValue("orde.getDisinfo.getName",orde));row.createCell(cellIndex++).setCellValue(obtainCellValue("orde.getProfit",orde));row.createCell(cellIndex++).setCellValue(obtainCellValue("orde.getCheckRoom",orde));row.createCell(cellIndex++).setCellValue(obtainCellValue("orde.getCheckDes",orde));}wb.write(os);  //输出到输出流}  


原创粉丝点击