【POI xlsx】使用POI對xlsx的單元格樣式進行設置 / 使用POI對xlsx的字體進行設置


POI的jar包版本3.14

3.17有很多就不一樣了!!!!

 

涉及到的樣式都在代碼中有說明:

  1 package com.it.poiTest;
  2 
  3 import java.io.FileNotFoundException;
  4 import java.io.FileOutputStream;
  5 import java.io.IOException;
  6 
  7 import org.apache.poi.hssf.util.HSSFColor;
  8 import org.apache.poi.sl.usermodel.Sheet;
  9 import org.apache.poi.ss.usermodel.Cell;
 10 import org.apache.poi.ss.usermodel.Color;
 11 import org.apache.poi.ss.usermodel.IndexedColors;
 12 import org.apache.poi.ss.usermodel.Row;
 13 import org.apache.poi.ss.util.CellRangeAddress;
 14 import org.apache.poi.xssf.usermodel.XSSFCellStyle;
 15 import org.apache.poi.xssf.usermodel.XSSFColor;
 16 import org.apache.poi.xssf.usermodel.XSSFSheet;
 17 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 18 
 19 public class MoreStyleCell {
 20     public static void main(String[] args) {
 21         XSSFWorkbook workbook = new XSSFWorkbook();
 22         try {
 23             FileOutputStream out = new FileOutputStream("moreStyleWrokNook.xlsx");
 24             XSSFSheet sheet = workbook.createSheet("stylesheet");
 25             
 26             /**
 27              * 簡單設置行高
 28              */
 29             //第一行
 30             Row row0 = sheet.createRow(0);
 31             row0.setHeight((short) (500));
 32             //第6列
 33             Cell cell = row0.createCell(5);
 34             cell.setCellValue("height=500");
 35             
 36             
 37             /**
 38              * 測試合並單元格之后的各個位置
 39              */
 40             //合並單元格                                參數1:第一行  /     參數2:最后一行 /   參數3:第一列 /  參數4:最后一列 [在此范圍之內]
 41             sheet.addMergedRegion(new CellRangeAddress(1,3,1,4));
 42             Row  row1 = sheet.createRow(1);
 43             Cell cell1 = row1.createCell(0);
 44             cell1.setCellValue("第二行 第一列");
 45             Cell cell2 = row1.createCell(1);
 46             cell2.setCellValue("第二行,第二列。應該是合並單元格");
 47             //既然合並了單元格,查看一下1.2在什么位置  【證明並沒有出現】
 48             Cell cell3 = row1.createCell(2);
 49             cell3.setCellValue("第二行,第三列");
 50             
 51             Cell cell4 = row1.createCell(5);
 52             cell4.setCellValue("第二行,第五列,也就是合並單元格之后的第三列");
 53             
 54             /**
 55              * 測試cellstyle的設置--單元格居中設置以及單元格內文字換行設置
 56              */
 57             row0 = sheet.createRow(4);
 58             row0.setHeight((short)1000);
 59             cell1 = row0.createCell(0);
 60             cell1.setCellValue("第五行 height=1000");
 61             //設置某一列的寬度
 62             sheet.setColumnWidth(0, 9000);
 63             XSSFCellStyle style1 = workbook.createCellStyle();
 64             //設置style---cell中水平的對齊方式
 65             style1.setAlignment(XSSFCellStyle.ALIGN_CENTER);
 66             //設置style---cell中垂直方向的對齊方式
 67             style1.setVerticalAlignment(XSSFCellStyle.VERTICAL_TOP);
 68             //給某個確定的cell設置樣式
 69             cell1.setCellStyle(style1);
 70             //為cell單元格追加內容
 71             //獲取到cell內的值【getRichStringCellValue獲取富文本類型的值,除了小數類型的使用這個方法獲取getNumericCellValue,其余類型均可以使用此方法獲取toString之后就可以轉化為其他的數據類型】
 72             String cell1Value = cell1.getRichStringCellValue().toString();
 73             //cell1.getNumericCellValue();
 74             //加上\n之后無法自動換行
 75             cell1.setCellValue(cell1Value+"\n"+"水平居中"+"\n"+"垂直居上");
 76             //設置style自動換行    這樣就可以自動換行了
 77             style1.setWrapText(true);
 78             cell1.setCellValue(cell1Value+"\n"+"水平居中"+"\n"+"垂直居上"+"\n"+"自動換行");
 79             System.out.println(cell1Value);
 80             
 81             /**
 82              * style樣式設置--設置border的邊框樣式以及顏色
 83              */
 84             row0 = sheet.createRow(5); 
 85             row0.setHeight((short)1000);
 86             cell1 = row0.createCell(5);
 87             cell1.setCellValue(6.6);
 88             XSSFCellStyle style2 = workbook.createCellStyle();
 89             style2.setBorderBottom(XSSFCellStyle.BORDER_THIN);
 90             style2.setBorderLeft(XSSFCellStyle.BORDER_HAIR);
 91             style2.setBorderRight(XSSFCellStyle.BORDER_DOTTED);
 92             style2.setBorderTop(XSSFCellStyle.BORDER_NONE);
 93             //顏色三種方式 給出   方式1:
 94             style2.setBottomBorderColor(IndexedColors.BLUE.getIndex());
 95             //方式2
 96             XSSFColor color = new XSSFColor();
 97             byte[] a = {127,0,13};
 98             //color.setRGB(a);
 99             color.setARGBHex("FF2906");
100             style2.setLeftBorderColor(color);
101             //方式3
102             style2.setRightBorderColor(HSSFColor.BLACK.index);
103             cell1.setCellStyle(style2);
104             
105             /**
106              * style設置---設置單元格的背景色與填充效果
107              */
108             row0 = sheet.createRow(6);
109             row0.setHeight((short) 1200);
110             cell1 = row0.createCell(6);
111             cell1.setCellValue(7.7);
112             XSSFCellStyle style3 = workbook.createCellStyle();
113             style3.setFillBackgroundColor(HSSFColor.RED.index);
114             //設置單元格的填充效果
115             style3.setFillPattern(XSSFCellStyle.LEAST_DOTS);
116             cell1.setCellStyle(style3);
117             
118             
119             /**
120              * style設置--設置單元格的前置填充顏色
121              */
122             row0 = sheet.createRow(7);
123             row0.setHeight((short) 1200);
124             cell1 = row0.createCell(7);
125             cell1.setCellValue(8.8);
126             XSSFCellStyle style4 = workbook.createCellStyle();
127             style4.setFillForegroundColor(IndexedColors.GREEN.index);
128             style4.setFillPattern(XSSFCellStyle.ALIGN_FILL);
129             cell1.setCellStyle(style4);
130             
131             
132             workbook.write(out);
133             
134         } catch (FileNotFoundException e) {
135             e.printStackTrace();
136         } catch (IOException e) {
137             e.printStackTrace();
138         }
139     }
140 }
View Code

 

【着重說明一點】:

合並單元格的方法

hssfSheet.addMergedRegion(new CellRangeAddress(3,11,1,4))

參數1  從第4行開始

參數2  到第12行結束 包含第12行

參數3  從第2列開始

參數4  到第5列結束  包含第5列

如果【開始行和結束行在同一行】   或者   【開始列和結束列在同一列】

這兩種情況允許同時出現一種或者都不出現。  

如果【開始行和結束行在同一行】   和   【開始列和結束列在同一列】同時出現的話,那就沒有合並單元格的意義了。同時就會報錯:【錯誤行號和上面例子中行號並不匹配,不必在意】

java.lang.IllegalArgumentException: Merged region B4 must contain 2 or more cells

同樣,如果控制不好,還會出現如下錯誤:

java.lang.IllegalStateException: Cannot add merged region B5:B6 to sheet because it overlaps with an existing merged region (B4:B5).

不能將合並的區域B5:B6添加到表格,因為它與現有的合並區域重疊(B4:B5)

就是你在下面合並的單元格 想要把上面已經合並的一部分單元格重疊了。

 

樣式最后的圖例:

 

其中,cell的邊框類型分別如下:

邊框范例圖

對應的靜態值

HSSFCellStyle. BORDER_DOTTED

HSSFCellStyle. BORDER_HAIR

HSSFCellStyle. BORDER_DASH_DOT_DOT

HSSFCellStyle. BORDER_DASH_DOT

HSSFCellStyle. BORDER_DASHED

HSSFCellStyle. BORDER_THIN

HSSFCellStyle. BORDER_MEDIUM_DASH_DOT_DOT

HSSFCellStyle. BORDER_SLANTED_DASH_DOT

HSSFCellStyle. BORDER_MEDIUM_DASH_DOT

HSSFCellStyle. BORDER_MEDIUM_DASHED

HSSFCellStyle. BORDER_MEDIUM

HSSFCellStyle. BORDER_THICK

HSSFCellStyle. BORDER_DOUBLE

 

其中,cell的背景紋理圖片如下:

圖案樣式

常量

HSSFCellStyle. NO_FILL

HSSFCellStyle. ALT_BARS

HSSFCellStyle. FINE_DOTS

HSSFCellStyle. SPARSE_DOTS

HSSFCellStyle. LESS_DOTS

HSSFCellStyle. LEAST_DOTS

HSSFCellStyle. BRICKS

HSSFCellStyle. BIG_SPOTS

HSSFCellStyle. THICK_FORWARD_DIAG

HSSFCellStyle. THICK_BACKWARD_DIAG

HSSFCellStyle. THICK_VERT_BANDS

HSSFCellStyle. THICK_HORZ_BANDS

HSSFCellStyle. THIN_HORZ_BANDS

HSSFCellStyle. THIN_VERT_BANDS

HSSFCellStyle. THIN_BACKWARD_DIAG

HSSFCellStyle. THIN_FORWARD_DIAG

HSSFCellStyle. SQUARES

HSSFCellStyle. DIAMONDS

 

 

下面使用POI對字體進行設置:

  1 package com.it.poiTest;
  2 
  3 import java.io.FileInputStream;
  4 import java.io.FileNotFoundException;
  5 import java.io.FileOutputStream;
  6 import java.io.IOException;
  7 
  8 import org.apache.poi.hssf.util.HSSFColor;
  9 import org.apache.poi.ss.usermodel.Cell;
 10 import org.apache.poi.ss.usermodel.IndexedColors;
 11 import org.apache.poi.ss.usermodel.Row;
 12 import org.apache.poi.xssf.usermodel.XSSFCellStyle;
 13 import org.apache.poi.xssf.usermodel.XSSFFont;
 14 import org.apache.poi.xssf.usermodel.XSSFSheet;
 15 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 16 
 17 public class FontTest {
 18 
 19     public static void main(String[] args) {
 20             try {
 21                 XSSFWorkbook workbook = new XSSFWorkbook();
 22                 FileOutputStream out = new FileOutputStream("FontSet.xlsx");
 23                 XSSFSheet sheet = workbook.createSheet("font  set");
 24                 sheet.setColumnWidth(1, 3000);
 25                 Row row = sheet.createRow(1);
 26                 row.setHeight((short) 900);
 27                 Cell cell = row.createCell(1);
 28                 cell.setCellValue("字體1");
 29                 
 30                 /**
 31                  * font設置---設置字體樣式
 32                  */
 33                 XSSFFont font1 = workbook.createFont();
 34                 //設置行高使用HSSFRow對象的setHeight和setHeightInPoints方法,這兩個方法的區別在於setHeightInPoints的單位是點,而setHeight的單位是1/20個點,所以setHeight的值永遠是setHeightInPoints的20倍。
 35                 //設置字號大小
 36                 //font1.setFontHeight(20);
 37                 //設置字號大小
 38                 font1.setFontHeightInPoints((short) 20);
 39                 //設置字體
 40                 font1.setFontName("Pristina");
 41                 //設置加粗
 42                 font1.setBold(true);
 43                 //設置斜體
 44                 font1.setItalic(true);
 45                 //設置字體顏色
 46                 font1.setColor(IndexedColors.PINK.getIndex());
 47                 //或者
 48                 //font1.setColor(HSSFColor.YELLOW.index);
 49                 XSSFCellStyle style = workbook.createCellStyle();
 50                 style.setFont(font1);
 51                 cell.setCellStyle(style);
 52                 
 53                 
 54                 /**
 55                  * 設置字體角度  順時針旋轉
 56                  */
 57                 row = sheet.createRow(2);
 58                 row.setHeight((short) 900);
 59                 cell = row.createCell(2);
 60                 XSSFCellStyle style1 = workbook.createCellStyle();
 61                 style1.setRotation((short) 0);
 62                 cell.setCellValue("0 'C");
 63                 cell.setCellStyle(style1);
 64                 
 65                 row = sheet.createRow(3);
 66                 row.setHeight((short) 900);
 67                 cell = row.createCell(3);
 68                 XSSFCellStyle style2 = workbook.createCellStyle();
 69                 style2.setRotation((short) 30);
 70                 cell.setCellValue("30  'C");
 71                 cell.setCellStyle(style2);
 72                 
 73                 row = sheet.createRow(4);
 74                 row.setHeight((short) 900);
 75                 cell = row.createCell(4);
 76                 XSSFCellStyle style3 = workbook.createCellStyle();
 77                 style3.setRotation((short) 180);
 78                 cell.setCellValue("180  'C");
 79                 cell.setCellStyle(style3);
 80                 
 81                 row = sheet.createRow(5);
 82                 row.setHeight((short) 900);
 83                 cell = row.createCell(5);
 84                 XSSFCellStyle style4 = workbook.createCellStyle();
 85                 style4.setRotation((short) 90);
 86                 cell.setCellValue("90  'C");
 87                 cell.setCellStyle(style4);
 88                 
 89                 
 90                 
 91                 
 92                 
 93                 
 94                 
 95                 
 96                 workbook.write(out);
 97                 out.close();
 98             } catch (FileNotFoundException e) {
 99                 e.printStackTrace();
100             } catch (IOException e) {
101                 e.printStackTrace();
102             }
103     }
104 
105 }
View Code

 

樣式顯示如下:

 


免責聲明!

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



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