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 }
时间: 2024-08-02 10:17:20

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

POI读取excel单元格,获取单元格各类型值,返回字符串类型

package a; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.text.DecimalFormat; import org.apache.commons.lang3.StringUtils; import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.h

poi获取合并单元格内的第一行第一列的值

当读取如图所示的excel时,显示为第1行 第1列 的内容是:合并单元格 其它在合并单元格区域内的单元格不显示 示例代码如下: 1 import java.io.FileInputStream; 2 import java.io.FileNotFoundException; 3 import java.io.IOException; 4 5 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 6 import org.apache.poi.ss.

c#如何获取excel单元格的RGB颜色

这段时间一直在做office的工作.前2天获取单元格的颜色的问题一直没搞明白. 开始我想用的就是Npoi.主要前一部分的工作都是用Npoi完成的 row.GetCell(j).CellStyle.FillBackgroundColorColor 获取IColor接口.通过IColor的RGB属性获取可是经过大量用例测试这里获取的rgb并不准确只有部分颜色对的上. 如图 后来我甚至问了npoi的创始人也没有给我一个明确的回复. 我自己猜测因为row.GetCell(j).CellStyle.Fil

POI对Excel单元格进行颜色设置

学习了:http://www.myexception.cn/program/1932587.html HSSFWorkbook workbook = new HSSFWorkbook(); HSSFCellStyle style = workbook.createCellStyle(); style.setFillForegroundColor(HSSFColor.LIME.index); style.setFillBackgroundColor(HSSFColor.GREEN.index);

poi 取excel单元格内容时,需要判断单元格的类型,才能正确取出

以下内容非原创,原文链接http://blog.sina.com.cn/s/blog_4b5bc01101015iuq.html ate String getCellValue(HSSFCell cell) { String cellValue = ""; DecimalFormat df = new DecimalFormat("#"); switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_STRING: c

获取指定单元格注释的函数

有人在群里问如何获取Excel单元格的注释,就给他写了下面的代码. 1 Function FuncNote(strCell As Range) 2 Application.Volatile 3 FuncNote = strCell.Comment.Text 4 End Function 原文地址:https://www.cnblogs.com/wisever/p/8994725.html

Java用POI解析excel并获取所有单元格数据

1.导入POI相关jar包 org.apache.poi jar 2.代码示例 public List getAllExcel(File file, String tableName, String fname, String enterpriseId, String reportId, String projectId) throws FileNotFoundException, IOException, ClassNotFoundException, InstantiationExcepti

poi设置一个Excel单元格的内容为多种样式的方法

有的时候我们POI操作Excel时,需要将Excel单元格的内容设置为多种样式,比如:设置单元格的内容为两种颜色,方法如下: //定义字体 HSSFFont redFont = (HSSFFont) workbook.createFont(); redFont.setColor(HSSFColor.RED.index);// 红色 HSSFFont blueFont = (HSSFFont) workbook.createFont(); redFont.setColor(HSSFColor.BL

读取写入excel单元格

以下是一些对excel的一些基本操作 1:工程对excel类库的导入,如:c:\program files\Microsoft office\offiece11\excel.exe2:命名控件的引入: using Microsoft.office.Interop.Excel; 3:如果是对一个已经存在的excel文件进行操作则:Application app=new Application();Workbook wbook=app.Workbooks.Open("c:\\temp.xls&quo