POI獲取excel單元格紅色字體,淡藍色前景色的內容


如果是Microsoft Excel 97-2003 工作表 (.xls)

if(31 == cell.getCellStyle().getFillForegroundColor()) //判斷單元格前景色為淡藍色
if(10 == book.getFontAt(cell.getCellStyle().getFontIndex()).getColor()) //判斷單元格字體顏色為紅色

如果是Microsoft Excel 工作表 (.xlsx)

if(0 == cell.getCellStyle().getFillForegroundColor()) //判斷單元格前景色為淡藍色
if(0 == book.getFontAt(cell.getCellStyle().getFontIndex()).getColor()) //判斷單元格字體顏色為紅色

 

具體的java示例代碼如下:

 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.apache.poi.xssf.usermodel.XSSFWorkbook;
11 
12 public class TestExcel {
13     private static final DataFormatter FORMATTER = new DataFormatter();
14 
15     /**
16      * 獲取單元格內容
17      * 
18      * @param cell
19      *            單元格對象
20      * @return 轉化為字符串的單元格內容
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         String value = "";
28         try {
29             // 創建對Excel工作簿文件
30             Workbook book = null;
31             try {
32                 book = new XSSFWorkbook(new FileInputStream(filePath));
33             } catch (Exception ex) {
34                 book = new HSSFWorkbook(new FileInputStream(filePath));
35             }
36 
37             Sheet sheet = book.getSheetAt(sheetIndex);
38             // 獲取到Excel文件中的所有行數
39             int rows = sheet.getPhysicalNumberOfRows();
40             // System.out.println("rows:" + rows);
41             // 遍歷行
42 
43             for (int i = 0; i < rows; i++) {
44                 // 讀取左上端單元格
45                 Row row = sheet.getRow(i);
46                 // 行不為空
47                 if (row != null) {
48                     // 獲取到Excel文件中的所有的列
49                     int cells = row.getPhysicalNumberOfCells();
50                     // System.out.println("cells:" + cells);
51 
52                     // 遍歷列
53                     for (int j = 0; j < cells; j++) {
54                         // 獲取到列的值
55                         Cell 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 += "第" + (i + 1) + "行 第" + (j + 1) + "列 的內容是: " + 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 }


免責聲明!

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



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