java POI Excel 單元格樣式


正如Html需要CSS一樣,我們的POI生成的Excel同樣需要樣式才能更完美的表現我們的數據。下面還是從簡單的例子出發,學習和了解POI的樣式設計。

  一、我的位置。

1 package com.myjava.poi;
 2 
 3 import java.io.FileOutputStream;
 4 import java.util.Date;
 5 
 6 import org.apache.poi.hssf.usermodel.HSSFCell;
 7 import org.apache.poi.hssf.usermodel.HSSFCellStyle;
 8 import org.apache.poi.hssf.usermodel.HSSFRichTextString;
 9 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
10 import org.apache.poi.ss.usermodel.Cell;
11 import org.apache.poi.ss.usermodel.CellStyle;
12 import org.apache.poi.ss.usermodel.Row;
13 import org.apache.poi.ss.usermodel.Sheet;
14 import org.apache.poi.ss.usermodel.Workbook;
15 
16 public class ExcelStyle {
17 
18     public static void main(String[] args) throws Exception{
19         Workbook wb=new HSSFWorkbook(); // 定義一個新的工作簿
20         Sheet sheet=wb.createSheet("第一個Sheet頁");  // 創建第一個Sheet頁
21         Row row=sheet.createRow(2); // 創建一個行
22         row.setHeightInPoints(30);
23         
24         createCell(wb, row, (short)0, HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_BOTTOM);
25         createCell(wb, row, (short)1, HSSFCellStyle.ALIGN_FILL, HSSFCellStyle.VERTICAL_CENTER);
26         createCell(wb, row, (short)2, HSSFCellStyle.ALIGN_LEFT, HSSFCellStyle.VERTICAL_TOP);
27         createCell(wb, row, (short)3, HSSFCellStyle.ALIGN_RIGHT, HSSFCellStyle.VERTICAL_TOP);
28         
29         FileOutputStream fileOut=new FileOutputStream("D:\\工作簿.xls");
30         wb.write(fileOut);
31         fileOut.close();
32     }
33     
34     /**
35      * 創建一個單元格並為其設定指定的對齊方式
36      * @param wb 工作簿
37      * @param row 行
38      * @param column  列
39      * @param halign  水平方向對其方式
40      * @param valign  垂直方向對其方式
41      */
42     private static void createCell(Workbook wb,Row row,short column,short halign,short valign){
43         Cell cell=row.createCell(column);  // 創建單元格
44         cell.setCellValue(new HSSFRichTextString("我在這"));  // 設置值
45         CellStyle cellStyle=wb.createCellStyle(); // 創建單元格樣式
46         cellStyle.setAlignment(halign);  // 設置單元格水平方向對其方式
47         cellStyle.setVerticalAlignment(valign); // 設置單元格垂直方向對其方式
48         cell.setCellStyle(cellStyle); // 設置單元格樣式
    
}
}

 

  效果顯示:

  

  二、我的邊框

 

 1 package com.myjava.poi;
 2  
 3  import java.io.FileOutputStream;
 4  import java.util.Calendar;
 5  import java.util.Date;
 6  
 7  import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 8  import org.apache.poi.ss.usermodel.Cell;
 9  import org.apache.poi.ss.usermodel.CellStyle;
10  import org.apache.poi.ss.usermodel.CreationHelper;
11  import org.apache.poi.ss.usermodel.IndexedColors;
12  import org.apache.poi.ss.usermodel.Row;
13  import org.apache.poi.ss.usermodel.Sheet;
14  import org.apache.poi.ss.usermodel.Workbook;
15  
16 public class Border {
17  
18     public static void main(String[] args) throws Exception{
19          Workbook wb=new HSSFWorkbook(); // 定義一個新的工作簿
20         Sheet sheet=wb.createSheet("第一個Sheet頁");  // 創建第一個Sheet頁
21          Row row=sheet.createRow(1); // 創建一個行
22          
23          Cell cell=row.createCell(1); // 創建一個單元格
24          cell.setCellValue(4);
25          
26          CellStyle cellStyle=wb.createCellStyle(); 
27          cellStyle.setBorderBottom(CellStyle.BORDER_THIN); // 底部邊框
28          cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // 底部邊框顏色         
29         cellStyle.setBorderLeft(CellStyle.BORDER_THIN);  // 左邊邊框
30          cellStyle.setLeftBorderColor(IndexedColors.RED.getIndex()); // 左邊邊框顏色
31         
32          cellStyle.setBorderRight(CellStyle.BORDER_THIN); // 右邊邊框
33          cellStyle.setRightBorderColor(IndexedColors.BLUE.getIndex());  // 右邊邊框顏色         
34          cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); // 上邊邊框
35          cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());  // 上邊邊框顏色
36          
37         cell.setCellStyle(cellStyle);
38          FileOutputStream fileOut=new FileOutputStream("D:\\Border.xls");
39          wb.write(fileOut);
40          fileOut.close();
41      }
42 }

 

 

  效果顯示:

  

  三、我的背景

  

package com.myjava.poi;
 2 
 3 import java.io.FileOutputStream;
 4 import java.util.Calendar;
 5 import java.util.Date;
 6 
 7 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 8 import org.apache.poi.ss.usermodel.Cell;
 9 import org.apache.poi.ss.usermodel.CellStyle;
10 import org.apache.poi.ss.usermodel.CreationHelper;
11 import org.apache.poi.ss.usermodel.IndexedColors;
12 import org.apache.poi.ss.usermodel.Row;
13 import org.apache.poi.ss.usermodel.Sheet;
14 import org.apache.poi.ss.usermodel.Workbook;
15 
16 public class Bg {
17 
18     public static void main(String[] args) throws Exception{
19         Workbook wb=new HSSFWorkbook(); // 定義一個新的工作簿
20         Sheet sheet=wb.createSheet("第一個Sheet頁");  // 創建第一個Sheet頁
21         Row row=sheet.createRow(1); // 創建一個行
22         
23         Cell cell=row.createCell(1);
24         cell.setCellValue("看不清我");
25         CellStyle cellStyle=wb.createCellStyle();
26         cellStyle.setFillBackgroundColor(IndexedColors.AQUA.getIndex()); // 背景色
27         cellStyle.setFillPattern(CellStyle.BIG_SPOTS);  
28         cell.setCellStyle(cellStyle);
29         
30         
31         Cell cell2=row.createCell(2);
32         cell2.setCellValue("我的前景色與眾不同");
33         CellStyle cellStyle2=wb.createCellStyle();
34         cellStyle2.setFillForegroundColor(IndexedColors.RED.getIndex()); // 前景色
35         cellStyle2.setFillPattern(CellStyle.SOLID_FOREGROUND);  
36         cell2.setCellStyle(cellStyle2);
37         
38         FileOutputStream fileOut=new FileOutputStream("D:\\bg.xls");
39         wb.write(fileOut);
40         fileOut.close();
41     }
42 }

  效果顯示:

  

  四、合並單元格

  

package com.myjava.poi;
 2 
 3 import java.io.FileOutputStream;
 4 import java.util.Calendar;
 5 import java.util.Date;
 6 
 7 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 8 import org.apache.poi.ss.usermodel.Cell;
 9 import org.apache.poi.ss.usermodel.CellStyle;
10 import org.apache.poi.ss.usermodel.CreationHelper;
11 import org.apache.poi.ss.usermodel.IndexedColors;
12 import org.apache.poi.ss.usermodel.Row;
13 import org.apache.poi.ss.usermodel.Sheet;
14 import org.apache.poi.ss.usermodel.Workbook;
15 import org.apache.poi.ss.util.CellRangeAddress;
16 
17 public class GetTogether {
18 
19     public static void main(String[] args) throws Exception{
20         Workbook wb=new HSSFWorkbook(); // 定義一個新的工作簿
21         Sheet sheet=wb.createSheet("第一個Sheet頁");  // 創建第一個Sheet頁
22         Row row=sheet.createRow(1); // 創建一個行
23         
24         Cell cell=row.createCell(1);
25         cell.setCellValue("我們被合並單元格啦!");
26         
27         sheet.addMergedRegion(new CellRangeAddress(
28                 1, // 起始行
29                 2, // 結束行
30                 1, // 其實列
31                 2  // 結束列
32         ));
33         
34         
35         FileOutputStream fileOut=new FileOutputStream("D:\\Together.xls");
36         wb.write(fileOut);
37         fileOut.close();
38     }
39 }

 

  效果顯示:

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM