首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用cfspreadsheet读取列格式

使用cfspreadsheet读取列格式
EN

Stack Overflow用户
提问于 2012-01-27 11:09:05
回答 1查看 2.1K关注 0票数 2

在使用cfspreadsheet读取电子表格中的列或单元格时,是否可以获取其数据类型或格式

我正在将电子表格数据从excel电子表格转换为数据库表。到目前为止,我只是将所有内容格式化为varchars,但如果我能将日期指定为日期,将整数指定为整数,那就更好了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-27 15:04:58

不幸的是,没有使用cfspreadsheet或内置的电子表格函数。它们只返回所显示的内容,而不返回基础值。但是,您可以通过点击底层的POI workbook来滚动自己的内容。

有几件事需要牢记:

  • 与数据库表不同,电子表格列可以包含多种数据类型。仅仅因为第一个单元格包含日期并不能保证该列中的所有单元格也都包含日期。因此,与任何导入一样,请确保在将所有值插入数据库表之前对其进行验证。
  • 这些方法只包含填充的行和单元格。跳过空白行和单元格。因此,列值并不总是基于零(0)的contiguous.
  • Sheet,行索引和列索引

要进行处理,只需抓取所需的表并迭代行和单元格即可。在列中循环时,检查单元格类型并提取原始值(即日期、字符串、数字...)

来源:Busy Developers' Guide to HSSF and XSSF Features

代码语言:javascript
复制
<cfscript>
// get the sheet you want to read
cfSheet = SpreadSheetRead("c:/path/to/somefile.xls"); 
workbook = cfSheet.getWorkBook();
sheetIndex = workbook.getActiveSheetIndex();
sheet = workbook.getSheetAt( sheetIndex );

// utility used to distinguish between dates and numbers
dateUtil = createObject("java", "org.apache.poi.ss.usermodel.DateUtil");

// process the rows and columns
rows = sheet.rowIterator();
while (rows.hasNext()) {
    currentRow = rows.next();
    data = {}; 

    cells = currentRow.cellIterator();
    while (cells.hasNext()) { 
        currentCell = cells.next();

        col = {};
        col.value  = "";
        col.type   = "";
        col.column = currentCell.getColumnIndex()+ 1;
        col.row    = currentCell.getRowIndex()+ 1;

        if (currentCell.getCellType() EQ currentCell.CELL_TYPE_STRING) {
               col.value = currentCell.getRichStringCellValue().getString();
            col.type = "string";
        }
        else if (currentCell.getCellType() EQ currentCell.CELL_TYPE_NUMERIC) {
            if (DateUtil.isCellDateFormatted(currentCell)) {
                 col.value = currentCell.getDateCellValue();
                 col.type = "date";
            } 
            else {
                 col.value = currentCell.getNumericCellValue();
                 col.type = "number";
            }
        }
        else if (currentCell.getCellType() EQ currentCell.CELL_TYPE_BOOLEAN) {
            col.value = currentCell.getBooleanCellValue();
            col.type = "boolean";
        }
        // ... handle other types CELL_TYPE_BLANK, CELL_TYPE_ERROR, CELL_TYPE_FORMULA

        data["COL"& col.column] = col;
    }

    // this row is finished. display all values
    WriteDump(data);
}
</cfscript>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9028511

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档