2025-02-10

@author YHC

工作薄 border邊框樣式:

[java]
//創建Excel  
            Workbook wb = new HSSFWorkbook(); 
            //創建工作薄  
            Sheet sheet = wb.createSheet("new sheet"); 
 
            //創建一個行對象,添加一些單元格到裡面,Row的下標從0開始  
            Row row = sheet.createRow(1); 
 
            //創建一個單元格,並添加值到裡面  
            Cell cell = row.createCell(1); 
            cell.setCellValue(4); 
 
            //這個樣式是設置工作薄四周的邊框的樣式  
            CellStyle style = wb.createCellStyle(); 
            //下邊框   
            style.setBorderBottom(CellStyle.SOLID_FOREGROUND); 
            //下邊框顏色  
            style.setBottomBorderColor(IndexedColors.BLACK.getIndex());//黑色  
            //左邊框  
            style.setBorderLeft(CellStyle.SOLID_FOREGROUND); 
            //左邊框顏色  
            style.setLeftBorderColor(IndexedColors.BLUE.getIndex());//藍色  
            //右邊框  
            style.setBorderRight(CellStyle.SOLID_FOREGROUND); 
            //右邊框顏色  
            style.setRightBorderColor(IndexedColors.YELLOW.getIndex());//黃色  
            //上邊框  
            style.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED);//虛線  
            //上邊框顏色  
            style.setTopBorderColor(IndexedColors.RED.getIndex());//紅色  
            //設置單元格樣式  
            cell.setCellStyle(style); 
 
            // 寫入文件  
            FileOutputStream fileOut = new FileOutputStream("workbook.xls"); 
            wb.write(fileOut); 
            fileOut.close(); 

//創建Excel
      Workbook wb = new HSSFWorkbook();
      //創建工作薄
      Sheet sheet = wb.createSheet("new sheet");

      //創建一個行對象,添加一些單元格到裡面,Row的下標從0開始
      Row row = sheet.createRow(1);

      //創建一個單元格,並添加值到裡面
      Cell cell = row.createCell(1);
      cell.setCellValue(4);

      //這個樣式是設置工作薄四周的邊框的樣式
      CellStyle style = wb.createCellStyle();
      //下邊框
      style.setBorderBottom(CellStyle.SOLID_FOREGROUND);
      //下邊框顏色
      style.setBottomBorderColor(IndexedColors.BLACK.getIndex());//黑色
      //左邊框
      style.setBorderLeft(CellStyle.SOLID_FOREGROUND);
      //左邊框顏色
      style.setLeftBorderColor(IndexedColors.BLUE.getIndex());//藍色
      //右邊框
      style.setBorderRight(CellStyle.SOLID_FOREGROUND);
      //右邊框顏色
      style.setRightBorderColor(IndexedColors.YELLOW.getIndex());//黃色
      //上邊框
      style.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED);//虛線
      //上邊框顏色
      style.setTopBorderColor(IndexedColors.RED.getIndex());//紅色
      //設置單元格樣式
      cell.setCellStyle(style);

      // 寫入文件
      FileOutputStream fileOut = new FileOutputStream("workbook.xls");
      wb.write(fileOut);
      fileOut.close();運行後效果:

 

 

遍歷所有的行和單元格:

有時候,你僅僅隻想遍歷工作薄中所有的行,或是行中所有的單元格,這個是允許的,隻需要一個簡單的循環便能完成此功能,

太幸運瞭,這個是如此簡單,行對象(Row)中定義瞭一個內部類,CellIterator 來處理迭代所有的單元格(得到其中的任

意一行,去調用row.cellIterator()),和工作薄提供瞭rowIterator()方法,提供一個迭代器遍歷所有的行.另外

Sheet(工作薄) and Row(行對象) 都實現瞭 java.lang.Iterable類,所以,使用Java1.5提供的

內置的"foreach"的支持,簡單的利用foreach進行遍歷,代碼如下:

[java]
     //根據Workbook對象得到工作薄  
 Sheet sheet = wb.getsheetat(0); 
//得到行迭代器,遍歷所有行  
for (iterator<row> rit = sheet.rowiterator(); rit.hasnext(); ) { 
  //得到行對象  
  Row row = rit.next(); 
  //根據行對象得到所有的單元格,遍歷所有單元格  
  for (iterator<cell> cit = row.celliterator(); cit.hasnext(); ) { 
    //得到該單元格  
    Cell cell = cit.next(); 
    // 你需要實現功能的代碼…  
  } 

             //根據Workbook對象得到工作薄
      Sheet sheet = wb.getsheetat(0);
     //得到行迭代器,遍歷所有行
     for (iterator<row> rit = sheet.rowiterator(); rit.hasnext(); ) {
       //得到行對象
       Row row = rit.next();
       //根據行對象得到所有的單元格,遍歷所有單元格
       for (iterator<cell> cit = row.celliterator(); cit.hasnext(); ) {
      //得到該單元格
         Cell cell = cit.next();
         // 你需要實現功能的代碼…
       }
     }

 作者:yhc13429826359
 

 

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *