当您使用cfspreadsheet
阅读时,是否可以在电子表格中获取列或单元格的数据类型或格式?
我正在将电子表格数据从Excel电子表格转换为数据库表格。到目前为止,我只是将所有内容都格式化为varchars,但如果我可以将日期指定为日期和整数作为整数,那将会很好。
答案 0 :(得分:3)
不幸的是,没有使用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>