在使用cfspreadsheet读取电子表格中的列或单元格时,是否可以获取其数据类型或格式
我正在将电子表格数据从excel电子表格转换为数据库表。到目前为止,我只是将所有内容格式化为varchars,但如果我能将日期指定为日期,将整数指定为整数,那就更好了。
发布于 2012-01-27 15:04:58
不幸的是,没有使用cfspreadsheet或内置的电子表格函数。它们只返回所显示的内容,而不返回基础值。但是,您可以通过点击底层的POI workbook来滚动自己的内容。
有几件事需要牢记:
要进行处理,只需抓取所需的表并迭代行和单元格即可。在列中循环时,检查单元格类型并提取原始值(即日期、字符串、数字...)
来源:Busy Developers' Guide to HSSF and XSSF Features
<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>https://stackoverflow.com/questions/9028511
复制相似问题