我有一张excel表,其中添加有红色背景,更改有黄色背景,删除是灰色.
我希望做的是读取工作表,并根据单元格背景颜色执行相关的数据库操作.
我希望做的是读取工作表,并根据单元格背景颜色执行相关的数据库操作.
通常我会在每个列中创建每种类型的操作,或者添加另一列来确定操作.
我有什么选择来获取电子表格对象中返回的“格式”?
谢谢
最佳答案
依靠细胞颜色听起来脆弱的IMO.分配显式操作列将是IMO的更好方法.
也就是说,可以访问颜色.但是,没有内置的CF方法.您必须深入了解基础POI.电子表格中的第一个iterate through the cells:
<cfscript>
// get the sheet you want to read
cfSheet = SpreadSheetRead("c:/path/to/somefile.xlsx");
workbook = cfSheet.getWorkBook();
sheetIndex = workbook.getActiveSheetIndex();
sheet = workbook.getSheetAt( sheetIndex );
// process the rows and columns
rows = sheet.rowIterator();
while (rows.hasNext()) {
currentRow = rows.next();
// loop through populated cells in this row
cells = currentRow.cellIterator();
while (cells.hasNext()) {
currentCell = cells.next();
// .... get color
}
}
</cfscript>
然后对于每个单元格,extract the style color.未经测试,但这样的事情应该有效. (见XSSFColor)
cellColor = currentCell.getCellStyle().getFillForegroundColorColor();
colorValue = cellColor.getARGBHex();
更新:
正如评论中提到的@Sean,CF9没有上述方法.不幸的是,getFillForegroundColorColor()和getARGBHex()在3.7左右被引入,但CF与早期版本捆绑在一起:3.5(我认为).因此,您必须使用索引颜色方法(或升级POI jar).
// only create once
colors = createObject("java", "org.apache.poi.ss.usermodel.IndexedColors");
//....
cellColor = currentCell.getCellStyle().getFillForegroundColor();
if (cellColor == colors.RED.getIndex()) {
WriteDump("This cell is RED. Do something...");
}
相关文章
转载注明原文:可以使用coldfusion读取Excel工作表上单元格的背景颜色 - 代码日志