码迷,mamicode.com
首页 > 编程语言 > 详细

Java读取excel数据保存入库

时间:2018-10-15 16:16:25      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:dex   getc   tco   ack   nts   ber   mem   int start   ring   

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数据保存入库

标签:dex   getc   tco   ack   nts   ber   mem   int start   ring   

原文地址:https://www.cnblogs.com/Generalwake/p/9791257.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!