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数据保存入库的更多相关文章

  1. Java读取Excel数据

    Java读取Excel数据,解析文本并格式化输出 Java读取Excel数据,解析文本并格式化输出 Java读取Excel数据,解析文本并格式化输出 下图是excel文件的路径和文件名 下图是exce ...

  2. POI读取Excel数据保存到数据库,并反馈给用户处理信息(导入带模板的数据)

    今天遇到这么一个需求,将课程信息以Excel的形式导入数据库,并且课程编号再数据库中不能重复,也就是我们需要先读取Excel提取信息之后保存到数据库,并将处理的信息反馈给用户.于是想到了POI读取文件 ...

  3. [转] JAVA读取excel数据(插入oracle数据库)

    原文地址:http://blog.csdn.net/zczzsq/article/details/16803349 本实例做的是读取execl(只能读取.xls的execl,即只能读取03版的),如果 ...

  4. Java 读取Excel数据——POI-3.11 XSSF

    POI  - the Java API for Microsoft Documents 1.在Apache官网下载Apache最新poi版本:poi-bin-3.11-20141221.zip,解压: ...

  5. 使用java读取excel数据

    package excelOperation2; import java.io.File; import java.io.FileNotFoundException; import java.util ...

  6. java的poi技术读取Excel数据到MySQL

    这篇blog是介绍java中的poi技术读取Excel数据,然后保存到MySQL数据中. 你也可以在 : java的poi技术读取和导入Excel了解到写入Excel的方法信息 使用JXL技术可以在 ...

  7. JAVA反射机制示例,读取excel数据映射到JAVA对象中

    import java.beans.PropertyDescriptor; import java.io.File; import java.io.FileInputStream; import ja ...

  8. Java POI读取Excel数据,将数据写入到Excel表格

    1.准备 首先需要导入poi相应的jar包,包括: 下载地址:http://pan.baidu.com/s/1bpoxdz5 所需要的包的所在位置包括: 2.读取Excel数据代码 package S ...

  9. java的poi技术读取Excel数据

    这篇blog主要是讲述java中poi读取excel,而excel的版本包括:2003-2007和2010两个版本, 即excel的后缀名为:xls和xlsx. 读取excel和MySQL相关: ja ...

随机推荐

  1. 左耳听风-ARTS-第1周

    Algorithm https://leetcode.com/problems/longest-common-prefix/ class Solution { public String longes ...

  2. MySQL 5.6 以上版本支持三种sql_mode模式:ANSI、TRADITIONAL和STRICT_TRANS_TABLES。

    Field 'id' doesn't have a default value问题解决方法 运维的名义关注0人评论3323人阅读2018-01-23 17:37:42   MySQL 5.0 以上版本 ...

  3. oracle 删除用户

    -- 查询用户各进程相对应的 sid.serial#. -- 注意: username 必须大写 SELECT sid,serial#,username FROM v$session WHERE us ...

  4. Guava - Ordering

    guava中Ordering类是对Compartor接口的实现,但它也只是一个抽象类. 当调用Ordering.natural()方法时,它就会返回一个NaturalOrdering的对象,Natur ...

  5. 【C++】关键字inline

    1. 引入inline关键字的原因 在c/c++中,为了解决一些频繁调用的小函数大量消耗栈空间(栈内存)的问题,特别的引入了inline修饰符,表示为内联函数. 栈空间就是指放置程序的局部数据(也就是 ...

  6. MySQL事务的介绍+事务的特性+事务的开启

    事务介绍: 简单的说,事务就是指逻辑上的一组SQL语句操作,组成这组操作的各个SQL语句,要么全成功要么全失败. 例如:A给B转账5元,流程是从A的账户扣除5元,把5元打入B的账户,B的账户上收到5元 ...

  7. apache+php+mysql安装与使用

    偷个懒,用的系统自带的apache和php apache安装与使用 Mac自带apache默认路径 主程序 /usr/sbin/httpd 模块 /usr/libexec/apache2 配置 /et ...

  8. 如何重置Gitlab root用户密码

    一.切换到root用户 sudo su 二.进入gitlab控制台 gitlab-rails console production 三.查找用户对象 user = User.).first 四.重置密 ...

  9. 报错:Sqoop2 Exception: java.lang.NoSuchMethodError Message: org.apache.hadoop.security.authentication.client.Authenticator

    报错过程: 进入sqoop2之后, 输入命令:show connector,报错 报错现象: Exception has occurred during processing command Exce ...

  10. Notification之适配总结

    11. android通知栏8.0系统报错误: NotificationService: No Channel found for pkg=xxx.xxx.xxx, channelId=12345, ...