POI操作EXCEL例子代碼

來源:互聯網
上載者:User

1、遍曆workbook

代碼
  1. // load源檔案   
  2. POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filePath));   
  3. HSSFWorkbook wb = new HSSFWorkbook(fs);   
  4. for (int i = 0; i < wb.getNumberOfSheets(); i++) {   
  5.     HSSFSheet sheet = wb.getSheetAt(i);   
  6.     for (int i = sheet.getFirstRowNum(); i < sheet.getLastRowNum(); i ++) {   
  7.     HSSFRow row = sheet.getRow(i);   
  8.             if (row != null) {   
  9.         。。。操作}   
  10.        }   
  11.      }   
  12. // 目標檔案   
  13. FileOutputStream fos = new FileOutputStream(objectPath);   
  14. //寫檔案   
  15. swb.write(fos);   
  16. fos.close();  

 

2、得到列和儲存格

代碼
  1. HSSFRow row = sheet.getRow(i);   
  2. HSSFCell cell = row.getCell((short) j);  

 

3、設定sheet名稱和儲存格內容為中文

代碼
  1. wb.setSheetName(n, "中文",HSSFCell.ENCODING_UTF_16);       
  2. cell.setEncoding((short) 1);   
  3. cell.setCellValue("中文");  

 

4、儲存格內容未公式或數值,可以這樣讀寫

代碼
  1. cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);   
  2. cell.getNumericCellValue()  

 


5、設定列寬、行高

代碼
  1. sheet.setColumnWidth((short)column,(short)width);   
  2. row.setHeight((short)height);  

 


6、添加地區,合併儲存格

代碼
  1. Region region = new Region((short)rowFrom,(short)columnFrom,(short)rowTo,(short)columnTo);   
  2. sheet.addMergedRegion(region);   
  3. //得到所有地區   
  4. sheet.getNumMergedRegions()  

 

7、常用方法
根據儲存格不同屬性返回字串數值

代碼
  1. public String getCellStringValue(HSSFCell cell) {   
  2.         String cellValue = "";   
  3.         switch (cell.getCellType()) {   
  4.         case HSSFCell.CELL_TYPE_STRING:   
  5.             cellValue = cell.getStringCellValue();   
  6.             if(cellValue.trim().equals("")||cellValue.trim().length()<=0)   
  7.                 cellValue=" ";   
  8.             break;   
  9.         case HSSFCell.CELL_TYPE_NUMERIC:   
  10.             cellValue = String.valueOf(cell.getNumericCellValue());   
  11.             break;   
  12.         case HSSFCell.CELL_TYPE_FORMULA:   
  13.             cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);   
  14.             cellValue = String.valueOf(cell.getNumericCellValue());   
  15.             break;   
  16.         case HSSFCell.CELL_TYPE_BLANK:   
  17.             cellValue=" ";   
  18.             break;   
  19.         case HSSFCell.CELL_TYPE_BOOLEAN:   
  20.             break;   
  21.         case HSSFCell.CELL_TYPE_ERROR:   
  22.             break;   
  23.         default:   
  24.             break;   
  25.         }   
  26.         return cellValue;   
  27.     }  



8、常用儲存格邊框格式

 

虛線HSSFCellStyle.BORDER_DOTTED
實線HSSFCellStyle.BORDER_THIN

代碼
  1. public static HSSFCellStyle getCellStyle(short type)   
  2.     {      
  3.        HSSFWorkbook wb = new HSSFWorkbook();   
  4.        HSSFCellStyle style = wb.createCellStyle();   
  5.        style.setBorderBottom(type);//下邊框    
  6.         style.setBorderLeft(type);//左邊框    
  7.         style.setBorderRight(type);//右邊框    
  8.         style.setBorderTop(type);//上邊框    
  9.        return style;   
  10.     }  



9、設定字型和內容位置
代碼

  1. HSSFFont f  = wb.createFont();   
  2. f.setFontHeightInPoints((short) 11);//字型大小   
  3. f.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);//加粗   
  4. style.setFont(f);   
  5. style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右置中   
  6. style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下置中   
  7. style.setRotation(short rotation);//儲存格內容的旋轉的角度   
  8. HSSFDataFormat df = wb.createDataFormat();   
  9. style1.setDataFormat(df.getFormat("0.00%"));//設定儲存格資料格式   
  10. cell.setCellFormula(string);//給儲存格設公式   
  11. style.setRotation(short rotation);//儲存格內容的旋轉的角度   
  12. cell.setCellStyle(style);   



10、插入圖片

論壇裡看到的 代碼

  1. //先把讀進來的圖片放到一個ByteArrayOutputStream中,以便產生ByteArray   
  2.       ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();   
  3.       BufferedImage bufferImg = ImageIO.read(new File("ok.jpg"));   
  4.       ImageIO.write(bufferImg,"jpg",byteArrayOut);   
  5. //讀進一個excel模版   
  6. FileInputStream fos = new FileInputStream(filePathName+"/stencil.xlt");    
  7. fs = new POIFSFileSystem(fos);   
  8. //建立一個工作薄   
  9. HSSFWorkbook wb = new HSSFWorkbook(fs);   
  10. HSSFSheet sheet = wb.getSheetAt(0);   
  11. HSSFPatriarch patriarch = sheet.createDrawingPatriarch();   
  12. HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,1023,255,(short) 0,0,(short)10,10);        
  13. patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));  

轉自:http://ltc603.javaeye.com/blog/30040

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.