我想做一個程序,在那裏我會從excel文件中讀取數據並將它們存儲在表格中。我有多個文件,其中大部分代碼都能正常工作。但是當我嘗試包含日期列的文件時,我遇到了一些問題。例外,當我試圖從excel文件中填充表格日期值java
首先用於顯示在我的控制檯中的數據我已經做下面的代碼爲不同類型的數據:
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
if (DateUtil.isCellDateFormatted(cell)) {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"dd/MM/yyyy");
System.out.println(cell.getDateCellValue());
} else {
Double value = cell.getNumericCellValue();
Long longValue = value.longValue();
System.out.print(cell.getNumericCellValue());
}
} else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
System.out.print(cell.getRichStringCellValue());
} else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
System.out.print(cell.getBooleanCellValue());
} else if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
System.out.print("null");
}
而且,當我分析的數據我使用下面的代碼爲不同類型的數據:
if (cell != null) {
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
if (DateUtil.isCellDateFormatted(cell)) {
tableFields.put(String.valueOf(cell
.getDateCellValue()), cell.getCellType());
} else {
tableFields
.put(String.valueOf(cell
.getNumericCellValue()), cell
.getCellType());
}
} else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
tableFields.put(cell.getStringCellValue(), cell
.getCellType());
} else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
tableFields.put(String.valueOf(cell
.getBooleanCellValue()), cell.getCellType());
} else if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
tableFields.put(cell.getStringCellValue(), cell
.getCellType());
}
當我創建該表:
switch (fieldType) {
case Cell.CELL_TYPE_NUMERIC:
str = fieldName + " dateFormat";
str = fieldName + " INTEGER";
break;
case Cell.CELL_TYPE_STRING:
str = fieldName + " VARCHAR(255)";
break;
case Cell.CELL_TYPE_BOOLEAN:
str = fieldName + " INTEGER";
break;
case Cell.CELL_TYPE_BLANK:
str = "null";
break;
default:
break;
}
而且在E第二,當我填充表中的值,我用下面的代碼:
switch (fieldType) {
case Cell.CELL_TYPE_NUMERIC:
str = fieldValue;
break;
case Cell.CELL_TYPE_STRING:
str = "\'" + fieldValue + "\'";
break;
case Cell.CELL_TYPE_BOOLEAN:
str = fieldValue;
break;
case Cell.CELL_TYPE_BLANK:
str = "null";
break;
default:
str = "";
break;
}
但是,當我運行程序我得到這個異常:
SQLException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Apr 21 00:00:00 EEST 1987)' at line 1
SQLState: 42000
VendorError: 1064
誰能幫我,爲什麼我得到它? 日期有形式:26/4/1980
你能告訴我們你在哪裏使用dateFormat實例嗎? – mauretto
您試圖從Excel文件中獲取值並將其添加到SQL表中? – DaGLiMiOuX
是的!我正在嘗試讀取所有數據格式的Excel文件,並將它們存儲在MySQL中的表中。 – dedmar