【问题标题】:can background color of a cell on an excel sheet be read using coldfusion可以使用coldfusion读取Excel工作表上单元格的背景颜色吗
【发布时间】:2014-02-04 15:25:31
【问题描述】:

我有一个 Excel 表,其中添加的背景为红色,更改的背景为黄色,删除的背景为灰色。 我希望做的是通读工作表,并根据单元格背景颜色,执行相关的数据库操作。

通常我会将每种类型的操作放在自己的列中,或者添加另一列来确定操作。

我有哪些选项可以获取电子表格对象中返回的“格式”?

谢谢

【问题讨论】:

  • 您使用的是什么版本的 ColdFusion?

标签: excel coldfusion colors cfspreadsheet


【解决方案1】:

依靠单元格颜色听起来很脆弱 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(); 

更新:

正如cmets中提到的@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...");          
    }

【讨论】:

  • 如果你知道它是哪个单元格,你可以抓住它而无需像这样迭代:ss.getWorkbook().getSheetAt(0).getRow(0).getCell(0).getCellStyle().getFillForegroundColor() 其中ss 是你的电子表格对象,而工作表 0 是你想要的工作表,在这个例子中你想要单元格 A1 (0,0)。另外,我在测试中发现 getFillForegroundColor() 实际上是您想要的,而不是背景。 YMMV。
  • (编辑)糟糕,复制粘贴错误。很好的捕捉@Sean。是的。如果您不熟悉 POI 内容,只需 a) 注意空值 b) 注意从零 (0) 开始的索引,而不是像 CF 中那样从一 (1) 开始。
  • 好吧,他想做的就是弄清楚它是否是绿色的。所以他不需要RGB代码。他只需要将它与绿色代码相匹配。您可以通过运行返回 17 的greenCode = createobject('java', 'org.apache.poi.hssf.util.HSSFColor$GREEN').getIndex(); 来获取“绿色”的代码。然后您可以在上面的评论中检查getFillForegroundColor() 的结果以获取单元格的颜色索引。如果它们匹配,则单元格为绿色!您可以从这里获取各种颜色代码:poi.apache.org/apidocs/org/apache/poi/hssf/util/HSSFColor.html 只需将 GREEN 替换为颜色
  • 嗯,我的样式对象没有名为 getXXXColorColor() 的方法。在那里引发错误。你确定@Leigh?
  • 我有 getFillForegroundColor()getFillBackgroundColor() 两者都返回一个短。但是这些方法没有 ColorColor() 版本。
猜你喜欢
  • 1970-01-01
  • 2019-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多