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

最新文章

  1. 第 15 章 CSS 文本样式[下]
  2. paip.输出内容替换在Apache 过滤器filter的设置
  3. LNAMP架构中后端Apache获取用户真实IP地址的2种方法(转)
  4. 轻量级IOC框架:Ninject (下)
  5. 【转】C#高性能大容量SOCKET并发(二):SocketAsyncEventArgs封装
  6. 在 Fedora 里安装自带的 MATE和 cinnamon
  7. 迁移笔记:php缓存技术memcached
  8. [.NET] 《Effective C#》快速笔记(三)- 使用 C# 表达设计
  9. echarts Map(地图) 不同颜色区块显示
  10. Dynamics 365新引入了多选选项集类型字段
  11. HTTP权威指南_Note
  12. js笔记2
  13. Yet Another Maxflow Problem CodeForces - 903G (最小割,线段树)
  14. [日常工作] cmd以及bash 直接使用当前目录的方法
  15. css文字链接滑过向上移动1像素
  16. mfc 动态为控件添加事件2
  17. c#实现word,excel转pdf代码及部分Office 2007文件格式转换为xps和pdf代码整理
  18. VS2010/MFC编程入门之四十九(图形图像:CDC类及其屏幕绘图函数)
  19. Windows下误删资料的恢复
  20. 终端多窗口分屏Terminator

热门文章

  1. 根据字符长度动态确定UIlabel宽高
  2. union和union all 合并查询
  3. Google之Chromium浏览器源码学习——base公共通用库(二)
  4. devpress 的gridview 控件的行拖拽 z
  5. 通过VS创建简单的WCF服务
  6. Official online document, install svn server in centOS
  7. Mysql的一些使用
  8. python中threading的用法
  9. BZOJ2087 : [Poi2010]Sheep
  10. jquery 抽奖示例