国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

POI獲取excel單元格紅色字體,淡藍(lán)色前景色的內(nèi)容

2019-11-14 21:07:21
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
POI獲取Excel單元格紅色字體,淡藍(lán)色前景色的內(nèi)容

如果是Microsoft Excel 97-2003 工作表 (.xls)
if(31 == cell.getCellStyle().getFillForegroundColor()) //判斷單元格前景色為淡藍(lán)色if(10 == book.getFontAt(cell.getCellStyle().getFontIndex()).getColor()) //判斷單元格字體顏色為紅色
如果是Microsoft Excel 工作表 (.xlsx)
if(0 == cell.getCellStyle().getFillForegroundColor()) //判斷單元格前景色為淡藍(lán)色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      * 獲取單元格內(nèi)容17      * 18      * @param cell19      *            單元格對(duì)象20      * @return 轉(zhuǎn)化為字符串的單元格內(nèi)容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             // 創(chuàng)建對(duì)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文件中的所有行數(shù)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) + "列 的內(nèi)容是: " + 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 }

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 额敏县| 龙海市| 林州市| 云梦县| 克山县| 慈溪市| 咸宁市| 仪陇县| 高台县| 南安市| 武功县| 苏尼特左旗| 呼图壁县| 新宾| 泽普县| 都安| 兴隆县| 南丹县| 绥阳县| 缙云县| 宜良县| 界首市| 湟中县| 柘荣县| 保定市| 洪江市| 霍林郭勒市| 古蔺县| 临安市| 黄山市| 衡南县| 武隆县| 思南县| 阳谷县| 太康县| 新沂市| 崇左市| 崇左市| 广平县| 白玉县| 昌江|