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