java apache poi 導出excel

不說廢話,帖代碼java

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

管網能夠下載
this

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);
      }
相關文章
相關標籤/搜索