For Microsoft Excel 97-2003 worksheet (.xls)
If (31 = cell. getcellstyle (). getfillforegroundcolor () // determines whether the foreground color of a cell is pale blue. If (10 = book. getfontat (cell. getcellstyle (). getfontindex ()). getcolor () // determines whether the cell font color is red.
For Microsoft Excel worksheet (.xlsx)
If (0 = cell. getcellstyle (). getfillforegroundcolor () // determines whether the foreground color of a cell is pale blue. If (0 = book. getfontat (cell. getcellstyle (). getfontindex ()). getcolor () // determines whether the cell font color is red.
The specific Java sample code is as follows:
1 import Java. io. fileinputstream; 2 Import Java. io. filenotfoundexception; 3 Import Java. io. ioexception; 4 Import Org. apache. poi. hssf. usermodel. hssfworkbook; 5 import Org. apache. poi. SS. usermodel. cell; 6 Import Org. apache. poi. SS. usermodel. dataformatter; 7 Import Org. apache. poi. SS. usermodel. row; 8 Import Org. apache. poi. SS. usermodel. sheet; 9 Import Org. apache. poi. SS. usermodel. workbook; 10 Import Org. apa Che. poi. xssf. usermodel. xssfworkbook; 11 12 public class testexcel {13 Private Static final dataformatter formatter = new dataformatter (); 14 15/** 16 * Get cell content 17*18 * @ Param cell19 * Cell Object 20 * @ return convert the cell content into a string 21 */22 Private Static string getcellcontent (Cell cell) {23 return formatter. formatcellvalue (cell); 24} 25 26 Private Static string getexcelvalue (string filepath, int sheetindex) {27 strin G value = ""; 28 try {29 // create 30 workbook book = NULL for the Excel Workbook file; 31 try {32 book = new xssfworkbook (New fileinputstream (filepath )); 33} catch (exception ex) {34 book = new hssfworkbook (New fileinputstream (filepath); 35} 36 37 sheet = book. getsheetat (sheetindex); 38 // get all rows in the Excel file 39 int rows = sheet. getphysicalnumberofrows (); 40 // system. out. println ("rows:" + rows); 41 // traverse rows 42 43 for (int I = 0; I <rows; I ++) {44 // read the upper left cell 45 row = sheet. getrow (I); 46 // The row is not null. 47 If (row! = NULL) {48 // obtain all columns in the Excel file 49 int cells = row. getphysicalnumberofcells (); 50 // system. out. println ("cells:" + cells); 51 52 // traverse column 53 for (Int J = 0; j <cells; j ++) {54 // obtain the column value 55 cell = row. getcell (j); 56 If (cell! = NULL) {57 // If (31 = 58 // cell. getcellstyle (). getfillforegroundcolor () & 59 // 10 = 60 // book. getfontat (cell. getcellstyle (). getfontindex ()). getcolor () 61 If (0 = cell. getcellstyle (). getfillforegroundcolor () 62 & 0 = book. getfontat (cell. getcellstyle (). getfontindex ()). getcolor () 63 value + = "th" + (I + 1) + "row th" + (J + 1) + "column content is: "+ getcellcontent (cell) +", "; 64} 65} 66 67} 68} 69} catch (filenotfoundexception e) {70 E. printstacktrace (); 71} catch (ioexception e) {72 E. printstacktrace (); 73} 74 75 return value; 76 77} 78 79 public static void main (string [] ARGs) {80 81 string filepath = "F: // example.xls "; 82 int sheetindex = 0; 83 84 string [] val = getexcelvalue (filepath, sheetindex ). split (","); 85 for (INT I = 0; I <Val. length; I ++) {86 system. out. println (Val [I]); 87} 88} 89}
Poi obtains the red font of the Excel cell and the content of the pale blue foreground color.