Types 用CFD电子表格读取列格式
使用Types 用CFD电子表格读取列格式,types,coldfusion,format,cfspreadsheet,Types,Coldfusion,Format,Cfspreadsheet,使用cfspreadsheet读取电子表格时,是否可以获取电子表格中列或单元格的数据类型或格式 我正在将电子表格数据从excel电子表格转换为数据库表。到目前为止,我只是将所有内容格式化为varchar,但如果我可以将日期指定为日期,将整数指定为整数,那就太好了。不幸的是,没有使用cfspreadsheet或内置的电子表格函数。它们只返回显示的内容,而不返回基础值。但是,您可以通过点击基础POI来实现自己的功能 需要记住的几件事: 与数据库表不同,电子表格列可以包含多种数据类型。仅仅因为第一个
cfspreadsheet
读取电子表格时,是否可以获取电子表格中列或单元格的数据类型或格式
我正在将电子表格数据从excel电子表格转换为数据库表。到目前为止,我只是将所有内容格式化为varchar,但如果我可以将日期指定为日期,将整数指定为整数,那就太好了。不幸的是,没有使用
cfspreadsheet
或内置的电子表格函数。它们只返回显示的内容,而不返回基础值。但是,您可以通过点击基础POI来实现自己的功能
需要记住的几件事:
- 与数据库表不同,电子表格列可以包含多种数据类型。仅仅因为第一个单元格包含日期并不能保证该列中的所有单元格也包含日期。因此,与任何导入一样,请确保在将所有值插入数据库表之前验证它们
- 这些方法仅包括填充的行和单元格。将跳过空行和单元格。因此,列值并不总是连续的
- 工作表、行和列索引基于零(0)
//拿到你想看的那张纸
cfSheet=SpreadSheetRead(“c:/path/to/somefile.xls”);
工作簿=cfSheet.get工作簿();
sheetIndex=workbook.getActiveSheetIndex();
sheet=工作簿.getSheetAt(sheetIndex);
//用于区分日期和数字的实用程序
dateUtil=createObject(“java”,“org.apache.poi.ss.usermodel.dateUtil”);
//处理行和列
rows=sheet.rowditerator();
while(rows.hasNext()){
currentRow=rows.next();
数据={};
cells=currentRow.cellIterator();
而(cells.hasNext()){
currentCell=cells.next();
col={};
col.value=“”;
col.type=“”;
col.column=currentCell.getColumnIndex()+1;
col.row=currentCell.getRowIndex()+1;
if(currentCell.getCellType()等式currentCell.CELL\u类型\u字符串){
col.value=currentCell.getRichStringCellValue().getString();
col.type=“字符串”;
}
else if(currentCell.getCellType()EQ currentCell.CELL\u TYPE\u NUMERIC){
if(DateUtil.isCellDateFormatted(currentCell)){
col.value=currentCell.getDateCellValue();
col.type=“日期”;
}
否则{
col.value=currentCell.getNumericCellValue();
col.type=“编号”;
}
}
else if(currentCell.getCellType()EQ currentCell.CELL\u TYPE\u BOOLEAN){
col.value=currentCell.getBooleanCellValue();
col.type=“boolean”;
}
//…处理其他类型单元格类型为空、单元格类型为错误、单元格类型为公式
数据[“列”和列]=列;
}
//此行已完成。显示所有值
写操作(数据);
}
<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>