Java读取excel数据保存入库

Posted 扬帆起航-梦起者

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java读取excel数据保存入库相关的知识,希望对你有一定的参考价值。

Java开发读取excel表格数据入库保存:

List<Map<String, Object>> list = null;

String filePath = filePaths + "/" + userID + "/" + accountID + "/" + busDate + "/";
String fileName = FileUpload.fileUp(file, filePath, "invoice-" + System.currentTimeMillis());
list = ReadExcal.readExcel(filePath, fileName, 0, 0);

 

调用的方法:

public static List<Map<String, Object>> readExcel(String filePath, String fileName, int startrow, int startcol)
throws Exception {
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
File file = new File(filePath, fileName);
FileInputStream fs = new FileInputStream(file);
Workbook wk = null;
FormulaEvaluator evaluator = null;
try {
wk = new XSSFWorkbook(fs);
evaluator = wk.getCreationHelper().createFormulaEvaluator();
} catch (Exception ex) {
wk = new HSSFWorkbook(new FileInputStream(file));
evaluator = wk.getCreationHelper().createFormulaEvaluator();
}
// Workbook wk = WorkbookFactory.create(fs);
// XSSFWorkbook wk=new XSSFWorkbook(fs);
Sheet sheet = wk.getSheetAt(0);
int rowNum = sheet.getLastRowNum() + 1;

for (int i = startrow; i < rowNum; i++) {
Row row = sheet.getRow(i);
if (row == null) {

continue;
}

int cellNum = row.getLastCellNum();
if (cellNum < 3) {
continue;
}

Map<String, Object> map = new LinkedHashMap<String, Object>();

for (int j = 0; j < cellNum; j++) {

Cell cell = row.getCell(j);
String cellvalue = null;
if (cell != null) {
switch (cell.getCellType()) {
case 0:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
Date dt = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());//获取成DATE类型
cellvalue = dateformat.format(dt);
break;
} else {
//cellvalue = cell.getNumericCellValue()+"";
DecimalFormat df = new DecimalFormat("0.##");
cellvalue = df.format(cell.getNumericCellValue())+"";
break;
}
case 1:
cellvalue = cell.getStringCellValue();
break;
case 2:
//处理公式的值
CellValue evaluate = null;
try {
double numericCellValue = cell.getNumericCellValue();
System.out.println(numericCellValue);
DecimalFormat df = new DecimalFormat("0.##");
cellvalue = df.format(numericCellValue)+"";
} catch (Exception e) {
e.printStackTrace();

String cellFormula = cell.getCellFormula();
evaluate = evaluator.evaluate(cell);
//CellValue evaluate = evaluator.evaluate(cell);
int cellType = evaluate.getCellType();
if(cellType==0){
try {
DecimalFormat df = new DecimalFormat("0.##");
cellvalue = df.format(cell.getNumericCellValue())+"";
} catch (Exception a) {
a.printStackTrace();
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
double numberValue = evaluate.getNumberValue();
Date dt = HSSFDateUtil.getJavaDate(numberValue);//获取成DATE类型
cellvalue = dateformat.format(dt);
}
if(cellvalue==null){
cellvalue = String.valueOf(evaluate);
}
}else if(cellType==1){
cellvalue = evaluate.getStringValue();
}else if(cellType==3){
cellvalue = "";
}else if(cellType==4){
cellvalue = String.valueOf(evaluate.getBooleanValue());
}else if(cellType==5){
cellvalue = String.valueOf(evaluate.getErrorValue());
}else{
cellvalue = "";
}
}
break;
case 3:
cellvalue = "";
break;
case 4:
cellvalue = String.valueOf(cell.getBooleanCellValue());
break;
case 5:
cellvalue = String.valueOf(cell.getErrorCellValue());
break;
}
} else {
cellvalue = "";
}
map.put("map" + j, cellvalue);
}
list.add(map);
}

return list;
}

 
















































































































以上是关于Java读取excel数据保存入库的主要内容,如果未能解决你的问题,请参考以下文章

Java通过jxl解析Excel文件入库,及日期格式处理方式 (附源代码)

java读取excel数据异常

python--读取excel通过django框架入库mysql(完整代码)

C#读取EXCEL中的信息,并保存到数据库

java poi技术读取到数据库

java jxl 读取excel时读取了很多空行 求解决办法