日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POI--HSSFCellStyle类

發布時間:2025/3/17 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POI--HSSFCellStyle类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  • 通過POI來進行單元格格式的設定

設定格式使用「HSSFCellStyle」類。它有一個構造方法:

protected HSSFCellStyle(short index, ExtendedFormatRecord rec)
雖然有構造方法,但卻是protected的,所以不能直接使用,要通過一個工作簿workbook來生成格式對象。?

在POI里,格式好像是以workbook為單位來管理的,所以要先作成一個格式對象,保存在workbook里,然后再對已生成好的單元格進行設定。?


  • 在單元格里指定格式
1,要作成一個格式對象,可以使用「HSSFWorkbook」類的「createCellStyle」方法。?
public HSSFCellStyle createCellStyle()
2,要取出現有的格式對象的話,使用「HSSFWorkbook」類的「getCellStyleAt」方法,這個方法有參數,是被保存格式的INDEX號。?
public HSSFCellStyle getCellStyleAt(short idx)
3,對于某一個單元格,也可以取出它的格式對象。這時要使用「HSSFCell」類的「getCellStyle」方法。?
public HSSFCellStyle getCellStyle()
4,這樣的話,不管是新創建的或者是從現有的單元格里取出來的格式對象,都可以用來對某一個單元格進行格式的設定。設定方法使用「HSSFCell」類的「setCellStyle」方法。?
public void setCellStyle(HSSFCellStyle style)
package linkin;import java.io.FileOutputStream; import java.io.IOException;import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor;public class Linkin {public static void main(String[] args){HSSFWorkbook workbook = new HSSFWorkbook();HSSFSheet sheet = workbook.createSheet();HSSFRow row = sheet.createRow(1);//第二行HSSFCell cell = row.createCell(0);//2,1格cell.setCellValue("sample");//寫入sampleHSSFCellStyle style = workbook.createCellStyle();//創建個workbook的HSSFCellStyle格式對象style//設定格式style.setFillBackgroundColor(HSSFColor.WHITE.index);style.setFillForegroundColor(HSSFColor.LIGHT_ORANGE.index);style.setFillPattern(HSSFCellStyle.THICK_HORZ_BANDS);cell.setCellStyle(style);//對2,1格寫入上面的格式FileOutputStream out = null;try{out = new FileOutputStream("sample.xls");workbook.write(out);}catch (IOException e){System.out.println(e.toString());}finally{try{out.close();}catch (IOException e){System.out.println(e.toString());}}} }

?

  • POI單元格顏色設定
先從單元格顔色設定開始介紹。單元格的顔色有前景色和背景色。?
1,前景色的設定使用「HSSFCellStyle」類的「setFillForegroundColor」方法。?
public void setFillForegroundColor(short bg)

2,背景色的設定則使用「HSSFCellStyle」類的「setFillBackgroundColor」方法。?
public void setFillBackgroundColor(short bg)
兩個方法都是通過參數來設定具體什么顔色。該參數類型為short型,在「HSSFColor」類里,準備了各種各樣顔色的定義值。?


  • HSSFColor類
HSSFColor類定義如下:public class HSSFColor extends java.lang.Object?
而各種顔色又是作為HSSFColor類的子類,定義一覽表如下:?
HSSFColor.AQUA ??HSSFColor.BLACK
HSSFColor.BLUEHSSFColor.BLUE_GREY
HSSFColor.BRIGHT_GREENHSSFColor.BROWN
HSSFColor.CORALHSSFColor.CORNFLOWER_BLUE
HSSFColor.DARK_BLUEHSSFColor.DARK_GREEN
HSSFColor.DARK_REDHSSFColor.DARK_TEAL
HSSFColor.DARK_YELLOWHSSFColor.GOLD
HSSFColor.GREENHSSFColor.GREY_25_PERCENT
HSSFColor.GREY_40_PERCENTHSSFColor.GREY_50_PERCENT
HSSFColor.GREY_80_PERCENTHSSFColor.INDIGO
HSSFColor.LAVENDERHSSFColor.LEMON_CHIFFON
HSSFColor.LIGHT_BLUEHSSFColor.LIGHT_CORNFLOWER_BLUE
HSSFColor.LIGHT_GREENHSSFColor.LIGHT_ORANGE
HSSFColor.LIGHT_TURQUOISEHSSFColor.LIGHT_YELLOW
HSSFColor.LIMEHSSFColor.MAROON
HSSFColor.OLIVE_GREENHSSFColor.ORANGE
HSSFColor.ORCHIDHSSFColor.PALE_BLUE
HSSFColor.PINKHSSFColor.PLUM
HSSFColor.REDHSSFColor.ROSE
HSSFColor.ROYAL_BLUEHSSFColor.SEA_GREEN
HSSFColor.SKY_BLUEHSSFColor.TAN
HSSFColor.TEALHSSFColor.TURQUOISE
HSSFColor.VIOLETHSSFColor.WHITE
HSSFColor.YELLOW
設定顔色時,用這些子類的靜態常量「index」作為參數,使用方法如下:?
HSSFWorkbook workbook = new HSSFWorkbook(); HSSFCellStyle style = workbook.createCellStyle(); style.setFillForegroundColor(HSSFColor.LIME.index); style.setFillBackgroundColor(HSSFColor.GREEN.index);
如果這些顔色還不夠你用的話,那么下面介紹怎么設定自己想要的顔色。?

  • 填充模式
1,指定填充模式的話,使用「HSSFCellStyle」類的「setFillPattern」方法。?
public void setFillPattern(short fp)

2,指定的填充模式,在「HSSFCellStyle」類里也有定義,類型為static short型,如下所示:?
值 說明
NO_FILL No background
SOLID_FOREGROUND Solidly filled
FINE_DOTS Small fine dots
ALT_BARS Wide dots
SPARSE_DOTS Sparse dots
THICK_HORZ_BANDS Thick horizontal bands
THICK_VERT_BANDS Thick vertical bands
THICK_BACKWARD_DIAG Thick backward facing diagonals
THICK_FORWARD_DIAG Thick forward facing diagonals
BIG_SPOTS Large spots
BRICKS Brick-like layout
THIN_HORZ_BANDS Thin horizontal bands
THIN_VERT_BANDS Thin vertical bands
THIN_BACKWARD_DIAG Thin backward diagonal
THIN_FORWARD_DIAG Thin forward diagonal
SQUARES Squares
DIAMONDS Diamonds
  • 實際的使用方法如下:?
HSSFWorkbook workbook = new HSSFWorkbook(); HSSFCellStyle style = workbook.createCellStyle(); style.setFillForegroundColor(HSSFColor.LIME.index); style.setFillBackgroundColor(HSSFColor.GREEN.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);package linkin;import java.io.FileOutputStream; import java.io.IOException;import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor;public class Linkin {static HSSFWorkbook workbook;public static void main(String[] args){workbook = new HSSFWorkbook();HSSFSheet sheet = workbook.createSheet();HSSFRow row[] = new HSSFRow[12];for (int i = 0; i < 12; i++){row[i] = sheet.createRow(i);}HSSFCell cell[][] = new HSSFCell[12][4];for (int i = 0; i < 12; i++){for (int j = 0; j < 4; j++){cell[i][j] = row[i].createCell((short) j);}}setStyle(cell[0][0], "AQUA", HSSFColor.AQUA.index);setStyle(cell[0][1], "BLACK", HSSFColor.BLACK.index);setStyle(cell[0][2], "BLUE", HSSFColor.BLUE.index);setStyle(cell[0][3], "BLUE_GREY", HSSFColor.BLUE_GREY.index);setStyle(cell[1][0], "BRIGHT_GREEN", HSSFColor.BRIGHT_GREEN.index);setStyle(cell[1][1], "BROWN", HSSFColor.BROWN.index);setStyle(cell[1][2], "CORAL", HSSFColor.CORAL.index);setStyle(cell[1][3], "CORNFLOWER_BLUE", HSSFColor.CORNFLOWER_BLUE.index);setStyle(cell[2][0], "DARK_BLUE", HSSFColor.DARK_BLUE.index);setStyle(cell[2][1], "DARK_GREEN", HSSFColor.DARK_GREEN.index);setStyle(cell[2][2], "DARK_RED", HSSFColor.DARK_RED.index);setStyle(cell[2][3], "DARK_TEAL", HSSFColor.DARK_TEAL.index);setStyle(cell[3][0], "DARK_YELLOW", HSSFColor.DARK_YELLOW.index);setStyle(cell[3][1], "GOLD", HSSFColor.GOLD.index);setStyle(cell[3][2], "GREEN", HSSFColor.GREEN.index);setStyle(cell[3][3], "GREY_25_PERCENT", HSSFColor.GREY_25_PERCENT.index);setStyle(cell[4][0], "GREY_40_PERCENT", HSSFColor.GREY_40_PERCENT.index);setStyle(cell[4][1], "GREY_50_PERCENT", HSSFColor.GREY_50_PERCENT.index);setStyle(cell[4][2], "GREY_80_PERCENT", HSSFColor.GREY_80_PERCENT.index);setStyle(cell[4][3], "INDIGO", HSSFColor.INDIGO.index);setStyle(cell[5][0], "LAVENDER", HSSFColor.LAVENDER.index);setStyle(cell[5][1], "LEMON_CHIFFON", HSSFColor.LEMON_CHIFFON.index);setStyle(cell[5][2], "LIGHT_BLUE", HSSFColor.LIGHT_BLUE.index);setStyle(cell[5][3], "LIGHT_CORNFLOWER_BLUE", HSSFColor.LIGHT_CORNFLOWER_BLUE.index);setStyle(cell[6][0], "LIGHT_GREEN", HSSFColor.LIGHT_GREEN.index);setStyle(cell[6][1], "LIGHT_ORANGE", HSSFColor.LIGHT_ORANGE.index);setStyle(cell[6][2], "LIGHT_TURQUOISE", HSSFColor.LIGHT_TURQUOISE.index);setStyle(cell[6][3], "LIGHT_YELLOW", HSSFColor.LIGHT_YELLOW.index);setStyle(cell[7][0], "LIME", HSSFColor.LIME.index);setStyle(cell[7][1], "MAROON", HSSFColor.MAROON.index);setStyle(cell[7][2], "OLIVE_GREEN", HSSFColor.OLIVE_GREEN.index);setStyle(cell[7][3], "ORANGE", HSSFColor.ORANGE.index);setStyle(cell[8][0], "ORCHID", HSSFColor.ORCHID.index);setStyle(cell[8][1], "PALE_BLUE", HSSFColor.PALE_BLUE.index);setStyle(cell[8][2], "PINK", HSSFColor.PINK.index);setStyle(cell[8][3], "PLUM", HSSFColor.PLUM.index);setStyle(cell[9][0], "RED", HSSFColor.RED.index);setStyle(cell[9][1], "ROSE", HSSFColor.ROSE.index);setStyle(cell[9][2], "ROYAL_BLUE", HSSFColor.ROYAL_BLUE.index);setStyle(cell[9][3], "SEA_GREEN", HSSFColor.SEA_GREEN.index);setStyle(cell[10][0], "SKY_BLUE", HSSFColor.SKY_BLUE.index);setStyle(cell[10][1], "TAN", HSSFColor.TAN.index);setStyle(cell[10][2], "TEAL", HSSFColor.TEAL.index);setStyle(cell[10][3], "TURQUOISE", HSSFColor.TURQUOISE.index);setStyle(cell[11][0], "VIOLET", HSSFColor.VIOLET.index);setStyle(cell[11][1], "WHITE", HSSFColor.WHITE.index);setStyle(cell[11][2], "YELLOW", HSSFColor.YELLOW.index);FileOutputStream out = null;try{out = new FileOutputStream("sample.xls");workbook.write(out);}catch (IOException e){System.out.println(e.toString());}finally{try{out.close();}catch (IOException e){System.out.println(e.toString());}}}public static void setStyle(HSSFCell cell, String col, short fg){HSSFCellStyle style = workbook.createCellStyle();style.setFillForegroundColor(fg);style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);cell.setCellStyle(style);cell.setCellValue(col);} }


上面程序只指定了「ForegroundColor」,填充模式是「SOLID_FOREGROUND」,因此顔色應該是全部充滿整個單元格的




package linkin;import java.io.FileOutputStream; import java.io.IOException;import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor;public class Linkin {static HSSFWorkbook workbook;public static void main(String[] args){workbook = new HSSFWorkbook();HSSFSheet sheet = workbook.createSheet();HSSFRow row[] = new HSSFRow[5];for (int i = 0; i < 5; i++){row[i] = sheet.createRow(i);}HSSFCell cell[][] = new HSSFCell[5][4];for (int i = 0; i < 5; i++){for (int j = 0; j < 4; j++){cell[i][j] = row[i].createCell((short) j);}}setStyle(cell[0][0], "NO_FILL", HSSFCellStyle.NO_FILL);setStyle(cell[0][1], "SOLID_FOREGROUND", HSSFCellStyle.SOLID_FOREGROUND);setStyle(cell[0][2], "FINE_DOTS", HSSFCellStyle.FINE_DOTS);setStyle(cell[0][3], "ALT_BARS", HSSFCellStyle.ALT_BARS);setStyle(cell[1][0], "SPARSE_DOTS", HSSFCellStyle.SPARSE_DOTS);setStyle(cell[1][1], "THICK_HORZ_BANDS", HSSFCellStyle.THICK_HORZ_BANDS);setStyle(cell[1][2], "THICK_VERT_BANDS", HSSFCellStyle.THICK_VERT_BANDS);setStyle(cell[1][3], "THICK_BACKWARD_DIAG", HSSFCellStyle.THICK_BACKWARD_DIAG);setStyle(cell[2][0], "THICK_FORWARD_DIAG", HSSFCellStyle.THICK_FORWARD_DIAG);setStyle(cell[2][1], "BIG_SPOTS", HSSFCellStyle.BIG_SPOTS);setStyle(cell[2][2], "BRICKS", HSSFCellStyle.BRICKS);setStyle(cell[2][3], "THIN_HORZ_BANDS", HSSFCellStyle.THIN_HORZ_BANDS);setStyle(cell[3][0], "THIN_VERT_BANDS", HSSFCellStyle.THIN_VERT_BANDS);setStyle(cell[3][1], "THIN_BACKWARD_DIAG", HSSFCellStyle.THIN_BACKWARD_DIAG);setStyle(cell[3][2], "THIN_FORWARD_DIAG", HSSFCellStyle.THIN_FORWARD_DIAG);setStyle(cell[3][3], "SQUARES", HSSFCellStyle.SQUARES);setStyle(cell[4][0], "DIAMONDS", HSSFCellStyle.DIAMONDS);FileOutputStream out = null;try{out = new FileOutputStream("sample.xls");workbook.write(out);}catch (IOException e){System.out.println(e.toString());}finally{try{out.close();}catch (IOException e){System.out.println(e.toString());}}}public static void setStyle(HSSFCell cell, String fps, short fp){HSSFCellStyle style = workbook.createCellStyle();style.setFillForegroundColor(HSSFColor.WHITE.index);style.setFillForegroundColor(HSSFColor.LIGHT_ORANGE.index);style.setFillPattern(fp);cell.setCellStyle(style);cell.setCellValue(fps);} }

上面固定了「ForegroundColor」和「BackgroundColor」,而填充模式則做了各種嘗試


  • POI設置邊框
在做一個電子表格時,邊框的設置有時是必不可少的。這一節就來介紹邊框,設置時,可以指定邊框的位置,邊框的種類,邊框的顔色。?首先是邊框的位置和種類。對單元格設置邊框時,有上下左右位置之分,所以POI也準備了四個不同的方法。?
上部的邊框:?public void setBorderTop(short border)
下部的邊框:?public void setBorderBottom(short border)
左側的邊框:?public void setBorderLeft(short border)
右側的邊框:?public void setBorderRight(short border)

參數通過表示邊框種類的short型值來指定。下面是定義在「HSSFCellStyle」類里可以被指定值的一覽表。?
值說明
BORDER_DASH_DOTdash-dot border
BORDER_DASH_DOT_DOTdash-dot-dot border
BORDER_DASHEDdash border
BORDER_DOTTEDdot borderhair-line border
BORDER_DOUBLEdouble-line border
BORDER_HAIRhair-line border
BORDER_MEDIUMMedium border
BORDER_MEDIUM_DASH_DOTmedium dash-dot border
BORDER_MEDIUM_DASH_DOT_DOTmedium dash-dot-dot border
BORDER_MEDIUM_DASHEDMedium dashed border
BORDER_NONENo border
BORDER_SLANTED_DASH_DOTslanted dash-dot border
BORDER_THICKThick border
BORDER_THINThin border


比如要在單元格下邊框設置兩重線的邊框時,按如下方法:?

HSSFWorkbook workbook = new HSSFWorkbook(); HSSFCellStyle style = workbook.createCellStyle(); style.setBorderRight(HSSFCellStyle.BORDER_THIN);
下面再看看指定邊框顔色。同樣也分為上下左右邊框來操作。?
上部的邊框:?public void setTopBorderColor(short color)
下部的邊框:?public void setBottomBorderColor(short color)
左側的邊框:?public void setLeftBorderColor(short color)
右側的邊框:?public void setRightBorderColor(short color)

仍然是通過參數來指定顔色,而且使用方法和前面一節也是一樣。具體如下:?
HSSFWorkbook workbook = new HSSFWorkbook(); HSSFCellStyle style = workbook.createCellStyle(); style.setRightBorderColor(HSSFColor.RED.index); style.setBorderRight(HSSFCellStyle.BORDER_THIN);
package linkin;import java.io.FileOutputStream; import java.io.IOException;import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor;public class Linkin {public static void main(String[] args){HSSFWorkbook workbook = new HSSFWorkbook();HSSFSheet sheet = workbook.createSheet();HSSFRow row = sheet.createRow(1);HSSFCell cell1 = row.createCell((short) 1);HSSFCell cell2 = row.createCell((short) 2);HSSFCellStyle style1 = workbook.createCellStyle();style1.setBorderTop(HSSFCellStyle.BORDER_DOUBLE);style1.setBorderLeft(HSSFCellStyle.BORDER_DOUBLE);style1.setTopBorderColor(HSSFColor.GOLD.index);style1.setLeftBorderColor(HSSFColor.PLUM.index);cell1.setCellStyle(style1);HSSFCellStyle style2 = workbook.createCellStyle();style2.setBorderBottom(HSSFCellStyle.BORDER_DOUBLE);style2.setBorderRight(HSSFCellStyle.BORDER_DOUBLE);style2.setBottomBorderColor(HSSFColor.ORANGE.index);style2.setRightBorderColor(HSSFColor.SKY_BLUE.index);cell2.setCellStyle(style2);cell1.setCellValue("U & L");cell2.setCellValue("B & R");FileOutputStream out = null;try{out = new FileOutputStream("sample.xls");workbook.write(out);}catch (IOException e){System.out.println(e.toString());}finally{try{out.close();}catch (IOException e){System.out.println(e.toString());}}} }

上面程序既改了顔色,也設置了上和左的邊框各一個,右和下的邊框各一個。?



package linkin;import java.io.FileOutputStream; import java.io.IOException;import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor;public class Linkin {static HSSFWorkbook workbook;public static void main(String[] args){workbook = new HSSFWorkbook();HSSFSheet sheet = workbook.createSheet();HSSFRow row[] = new HSSFRow[5];for (int i = 0; i < 5; i++){row[i] = sheet.createRow(i);}HSSFCell cell[][] = new HSSFCell[5][3];for (int i = 0; i < 5; i++){for (int j = 0; j < 3; j++){cell[i][j] = row[i].createCell((short) j);}}setStyle(cell[0][0], "DASH_DOT", HSSFCellStyle.BORDER_DASH_DOT);setStyle(cell[0][1], "DASH_DOT_DOT", HSSFCellStyle.BORDER_DASH_DOT_DOT);setStyle(cell[0][2], "DASHED", HSSFCellStyle.BORDER_DASHED);setStyle(cell[1][0], "DOTTED", HSSFCellStyle.BORDER_DOTTED);setStyle(cell[1][1], "DOUBLE", HSSFCellStyle.BORDER_DOUBLE);setStyle(cell[1][2], "HAIR", HSSFCellStyle.BORDER_HAIR);setStyle(cell[2][0], "MEDIUM", HSSFCellStyle.BORDER_MEDIUM);setStyle(cell[2][1], "MEDIUM_DASH_DOT", HSSFCellStyle.BORDER_MEDIUM_DASH_DOT);setStyle(cell[2][2], "MEDIUM_DASH_DOT_DOT", HSSFCellStyle.BORDER_MEDIUM_DASH_DOT_DOT);setStyle(cell[3][0], "MEDIUM_DASHED", HSSFCellStyle.BORDER_MEDIUM_DASHED);setStyle(cell[3][1], "NONE", HSSFCellStyle.BORDER_NONE);setStyle(cell[3][2], "SLANTED_DASH_DOT", HSSFCellStyle.BORDER_SLANTED_DASH_DOT);setStyle(cell[4][0], "THICK", HSSFCellStyle.BORDER_THICK);setStyle(cell[4][1], "THIN", HSSFCellStyle.BORDER_THIN);FileOutputStream out = null;try{out = new FileOutputStream("sample.xls");workbook.write(out);}catch (IOException e){System.out.println(e.toString());}finally{try{out.close();}catch (IOException e){System.out.println(e.toString());}}}public static void setStyle(HSSFCell cell, String bn, short border){HSSFCellStyle style = workbook.createCellStyle();style.setBorderBottom(border);style.setBottomBorderColor(HSSFColor.ORANGE.index);cell.setCellStyle(style);cell.setCellValue(bn);} }


轉載于:https://www.cnblogs.com/LinkinPark/p/5233041.html

總結

以上是生活随笔為你收集整理的POI--HSSFCellStyle类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。