标签: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;
}
标签:dex getc tco ack nts ber mem int start ring
原文地址:https://www.cnblogs.com/Generalwake/p/9791257.html