java apache poi 导出excel

不说废话,帖代码

引入架包:poi-3.12-20150511.jar   poi-ooxml-3.12-20150511.jar

管网可以下载

try{
         FLogicDataset<FDataInfoDeviceBrowserUnit> unitlist = _deviceBrowserConsole.select(logicContext);
         //创建新的Excel工作薄 
         HSSFWorkbook workbook = new HSSFWorkbook();
         HSSFSheet sheet = workbook.createSheet("sheet1");
         //在索引0的位置创建行(最顶端的行)  
         String[] headers = new String[]{"头信息", "设备信息"};
         HSSFRow headerRow = sheet.createRow(0);
         HSSFCell cell = headerRow.createCell(0);
         cell.setCellType(XSSFCell.CELL_TYPE_STRING);
         cell.setCellValue(headers[0]);
         HSSFCell cell0 = headerRow.createCell(1);
         cell0.setCellType(XSSFCell.CELL_TYPE_STRING);
         cell0.setCellValue(headers[1]);

         int index = 0;
         for(FDataInfoDeviceBrowserUnit unit : unitlist){
            index++;
            HSSFRow row = sheet.createRow(index);
            HSSFCell cell1 = row.createCell(0);
            //定义单元格为字符串类型  
            cell1.setCellType(XSSFCell.CELL_TYPE_STRING);
            //在单元格中输入一些内容  
            cell1.setCellValue(unit.agentCode());
            HSSFCell cell2 = row.createCell(1);
            //定义单元格为字符串类型  
            cell2.setCellType(XSSFCell.CELL_TYPE_STRING);
            //在单元格中输入一些内容  
            cell2.setCellValue(unit.content());
         }
         String filePath = "d:/deviceList_" + RDateTime.currentDateTime() + ".xls";
         //新建文件输出流  
         FileOutputStream fOut = new FileOutputStream(filePath);
         //将数据写入Excel  
         workbook.write(fOut);
         fOut.close();
         basePage.ajax(1, filePath);
         _logger.debug(this, "Expend", "Expend succeed. (path={1})", filePath);
      }catch(Exception e){
         e.printStackTrace();
         basePage.ajax(0, e.getMessage());
         _logger.debug(this, "Expend", "Expend fail. (message={1})", e);
      }


你可能感兴趣的:(java,apache,poi,导出Excel)