码迷,mamicode.com
首页 > 其他好文 > 详细

POI获取excel单元格红色字体,淡蓝色前景色的内容

时间:2014-09-11 15:19:52      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   java   ar   strong   

bubuko.com,布布扣

如果是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 }

POI获取excel单元格红色字体,淡蓝色前景色的内容

标签:style   blog   http   color   io   os   java   ar   strong   

原文地址:http://www.cnblogs.com/DurantSimpson/p/3966472.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!