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. Thing in java 第四章,控制执行流程,练习题答案

    /** * Created by Sandy.Liu on 2018/7/19. * Thinking in java, version 4, chapter 4, practice 1 * Writ ...

  2. Centos6两个镜像文件的合并方法

    1.相关目录: /mnt/dvd1和/mnt/dvd2 用于挂载 Centos 镜像 /mnt/dvd3 合并后的镜像文件 /mnt/iso ISO储存 mkdir -p /mnt/dvd1 /mnt ...

  3. request&response

    /* request&response 1.什么是HttpServletRequest HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP ...

  4. 从已删除邮箱copy数据到活动邮箱

    Start Windows PowerShell Start > search for "PowerShell" > Start Windows PowerShell ...

  5. webRTC中音频相关的netEQ(二):数据结构

    上篇(webRTC中音频相关的netEQ(一):概述)是netEQ的概述,知道了它主要是用于解决网络延时抖动丢包等问题提高语音质量的,也知道了它有两大单元MCU和DSP组成.MCU 主要是把从网络收到 ...

  6. js和php刷新页面的方法

    js中3个最优的刷新页面的方法 window.location.reload(): window.history.go(0): document.execCommand(''Refresh''): p ...

  7. 三、thymeleaf模板引擎构建前台html, 后台使用 ModelAndView 和 Model 模型

    项目源码:https://github.com/y369q369/springBoot.git      ->     thymeleaf 私聊QQ: 1486866853 1.pom.xml中 ...

  8. spring ref history Design philosophy

    一.前言 Spring 框架可以说是 Java 开发人员使用的最流行的应用程序开发框架之一.它目前由大量提供一系列服务的模块组成.包括模块容器,为构建横切关注点提供支持的面向切面编程(AOP),安全框 ...

  9. mvc部分视图转换成html字符串

    public static class RenderViewTostring { /// <summary> ///将部分视图转成html 字符串方便我们扩展使用 /// </sum ...

  10. 入门Spring ioc

    简单的来记录一下自己的SSM框架入门--------IOC篇段 ioc(控制反转 -将对象的创建的权利从类型本身来创建,来交给spring工厂来创建)的配置. <bean>:是可以指spr ...