如果是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示例代码如下:

 import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class TestExcel {
private static final DataFormatter FORMATTER = new DataFormatter(); /**
* 获取单元格内容
*
* @param cell
* 单元格对象
* @return 转化为字符串的单元格内容
*/
private static String getCellContent(Cell cell) {
return FORMATTER.formatCellValue(cell);
} private static String getExcelValue(String filePath, int sheetIndex) {
String value = "";
try {
// 创建对Excel工作簿文件
Workbook book = null;
try {
book = new XSSFWorkbook(new FileInputStream(filePath));
} catch (Exception ex) {
book = new HSSFWorkbook(new FileInputStream(filePath));
} Sheet sheet = book.getSheetAt(sheetIndex);
// 获取到Excel文件中的所有行数
int rows = sheet.getPhysicalNumberOfRows();
// System.out.println("rows:" + rows);
// 遍历行 for (int i = 0; i < rows; i++) {
// 读取左上端单元格
Row row = sheet.getRow(i);
// 行不为空
if (row != null) {
// 获取到Excel文件中的所有的列
int cells = row.getPhysicalNumberOfCells();
// System.out.println("cells:" + cells); // 遍历列
for (int j = 0; j < cells; j++) {
// 获取到列的值
Cell cell = row.getCell(j);
if (cell != null) {
// if (31 ==
// cell.getCellStyle().getFillForegroundColor() &&
// 10 ==
// book.getFontAt(cell.getCellStyle().getFontIndex()).getColor())
if (0 == cell.getCellStyle().getFillForegroundColor()
&& 0 == book.getFontAt(cell.getCellStyle().getFontIndex()).getColor())
value += "第" + (i + 1) + "行 第" + (j + 1) + "列 的内容是: " + getCellContent(cell) + ",";
}
} }
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} return value; } public static void main(String[] args) { String filePath = "F://example.xls";
int sheetIndex = 0; String[] val = getExcelValue(filePath, sheetIndex).split(",");
for (int i = 0; i < val.length; i++) {
System.out.println(val[i]);
}
}
}

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

  1. POI实现EXCEL单元格合并及边框样式

    POI实现EXCEL单元格合并及边框样式     下面例子为创建产生一个excel,合并单元格,然后为合并后的单元格添加边框 package test; import java.io.FileOutp ...

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

    POI对Excel单元格进行颜色设置 学习了:http://www.myexception.cn/program/1932587.html HSSFWorkbook workbook = new HS ...

  3. POI如何自动调整Excel单元格中字体的大小

    问题 目的是要将Excel中的文字全部显示出来,可以设置对齐格式为[缩小字体填充],但是这样的话只能展示出一行数据,字体会变得很小.还有一种办法,设置对齐格式为[自动换行],然后让单元格中的字体自动调 ...

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

    当读取如图所示的excel时,显示为第1行 第1列 的内容是:合并单元格 其它在合并单元格区域内的单元格不显示 示例代码如下: import java.io.FileInputStream; impo ...

  5. c#怎样获取excel单元格的RGB颜色

    这段时间一直在做office的工作.前2天获取单元格的颜色的问题一直没搞明确. 開始我想用的就是Npoi.主要前一部分的工作都是用Npoi完毕的 row.GetCell(j).CellStyle.Fi ...

  6. 修复Java使用POI合并Excel单元格后,边框不显示的问题

    使用Apache POI生成Excel文档时,当进行单元格合并操作后,被合并的单元格边框会消失,使用如下方式可以解决. 创建方法: public void setBorderStyle(int bor ...

  7. POI 设置Excel单元格背景色(setFillForegroundColor)

    背景介绍:使用Java开发信息系统项目,项目中往往会涉及到报表管理部分,而Excel表格首当其冲称为最合适的选择,但是对单元格操作时对于设置单元格的背景颜色却很少提及,本文旨在方便单元格背景颜色设计. ...

  8. java POI实现Excel单元格数据换行

    当我们通过POI设置了表格的列宽的时候,如果文字过长,希望文字能够自己折行显示. 截取代码如下: Workbook wb = new XSSFWorkbook(); //or new HSSFWork ...

  9. 以字符串形式获取excel单元格中的内容

    public static String getCellValue(XSSFCell cell) { if (cell == null) { return ""; } switch ...

随机推荐

  1. Huffman树实现_详细注释

    //最优二叉树 #include <iostream> #include <iomanip> using namespace std; //定义结点类型 //[weight | ...

  2. WPF 实现 DataGrid/ListView 分页控件

    在WPF中,通常会选用DataGrid/ListView进行数据展示,如果数据量不多,可以直接一个页面显示出来.如果数据量很大,2000条数据,一次性显示在一个页面中,不仅消耗资源,而且用户体验也很糟 ...

  3. PAT A 1022. Digital Library (30)【结构体排序检索】

    https://www.patest.cn/contests/pat-a-practise/1022 直接模拟, 输入,按id排序,检索 #include <iostream> #incl ...

  4. [Unity3D插件]2dToolKit系列三 碰撞检测功能的实现以及障碍物的随机摆放

    貌似有一段时间没更新2dtoolkit系列了,这段时间一直在忙着其他事情,今天开始继续这个插件系列的教程,网上搜索,貌似关于这个插件的教程无非还是跟官方的教程很类似,有的甚至都没有自己照着亲手实践一遍 ...

  5. 安卓更新sdk的代理

     mirrors.neusoft.edu.cn80    

  6. Struts2 动态方法调用

    01.Struts 2基本结构 使用Struts2框架实现用登录的功能,使用struts2标签和ognl表达式简化了试图的开发,并且利用struts2提供的特性对输入的数据进行验证,以及访问Servl ...

  7. MVC和WebForm 中国省市区三级联动

    MVC和WebForm是微软B/S端的两条腿,两种不同的设计理念,相对来说MVC更优于WebForm对于大数据的交互,因为WebForm是同一时间传输所有数据,而MVC它只是传输所用到的数据,更精确, ...

  8. 数据结构之KMP算法next数组

    我们要找到一个短字符串(模式串)在另一个长字符串(原始串)中的起始位置,也就是模式匹配,最关键的是找到next数组.最简单的算法就是用双层循环来解决,但是这种算法效率低,kmp算法是针对模式串自身的特 ...

  9. Difinition Of Done

    A Story is Sprint ready (Rally Defined) when............. The story has well defined and testable ac ...

  10. 使用python解析Json字符串-获取Json字符串关键字

    import json data = { "statusCode": 0, "data": { ", "height": &quo ...