1.上传:

public String uploadFile(CommonsMultipartFile file,String uploadPath,String realUploadPath){
InputStream is = null;
OutputStream os = null;
Calendar calendar = Calendar.getInstance();//获取时间
long excelName = calendar.getTime().getTime(); try {
is = file.getInputStream();
String des = realUploadPath + "/"+Long.toString(excelName)+file.getOriginalFilename();
os = new FileOutputStream(des); byte[] buffer = new byte[1024];
int len = 0; while((len = is.read(buffer))>0){
os.write(buffer);
} } catch (Exception e) {
e.printStackTrace();
}finally{
if(is!=null){
try{
is.close();
}catch (Exception e2){
e2.printStackTrace();
}
} if(os!=null){
try{
os.close();
}catch (Exception e2){
e2.printStackTrace();
}
}
}
//返回路径
return uploadPath + "/"+Long.toString(excelName)+file.getOriginalFilename();
}

2.解析:

常用的Excel解析方式有两种JXL,POI

jxl用起来相对简单,但只支持Excel2003版本,也就是说jxl无法解析.xlsx的Excel文件,而POI会识别Excel版本进行解析,所以大部分人更倾向于POI

jxl使用时需要在项目中导入jxl.jar包

poi需要导入

poi-3.14-20160307.jar

poi-ooxml-3.14-20160307.jar

poi-ooxml-schemas-3.14-20160307.jar

commons-io-1.4.jar

commons-fileupload-1.2.1.jar

2.1   JXL解析代码:

    public String readExcel(CommonsMultipartFile file,HttpServletRequest request)throws IOException, WriteException{  

            StringBuffer sb = new StringBuffer();//将读取的内容存入StringBUffer中
try {
Workbook book = Workbook.getWorkbook(file.getInputStream());
try{
Sheet sheet = book.getSheet(0);
for(int i = 0 ; i < 3 ; i++){//i表示行数
for(int j = 0 ; j < 4 ; j++){//j表示列数
sb.append(sheet.getCell(j, i).getContents()+"\t");
}
sb.append("\n");
}
System.out.println(sb);
}finally{
if(book != null){
book.close();
}
}
} catch (BiffException e) {
System.err.println(e+"");
} catch (IOException e) {
System.err.println(e+"文件读取错误");
}
return "";
}

2.2   POI解析代码:

 private POIFSFileSystem fs;
private HSSFWorkbook wb;
private HSSFSheet sheet;
private HSSFRow row;
/*读取标题excel第一行内容*/
public String[] readExcelTitle(InputStream is) {
try {
fs = new POIFSFileSystem(is);
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace();
}
sheet = wb.getSheetAt(0);
row = sheet.getRow(0);
// 标题总列数
int colNum = row.getPhysicalNumberOfCells();
System.out.println("colNum:" + colNum);
String[] title = new String[colNum];
for (int i = 0; i < colNum; i++) {
//title[i] = getStringCellValue(row.getCell((short) i));
title[i] = getCellFormatValue(row.getCell((short) i));
}
return title;
}
/*读取内容*/
public void readExcelContent(InputStream is) {
Map<Integer, ModelCourse> content = new HashMap<Integer, ModelCourse>();
ModelCourse model=new ModelCourse();
try {
fs = new POIFSFileSystem(is);
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace();
}
sheet = wb.getSheetAt(0);
// 得到总行数
int rowNum = sheet.getLastRowNum();
row = sheet.getRow(0);
int colNum = row.getPhysicalNumberOfCells(); // 正文内容从第二行开始,第一行为表头的标题
for (int i = 1; i <=rowNum; i++) {
row = sheet.getRow(i);
int j = 0; while (j < colNum) { if(j==1){
model.setCourse_id(getCellFormatValue(row.getCell((short) j)));
}
else if(j==2){
model.setCourse_name(getCellFormatValue(row.getCell((short) j)));
}
else if(j==3){
model.setCourse_time(getCellFormatValue(row.getCell((short) j)));
}
else if(j==4){
model.setCourse_place(getCellFormatValue(row.getCell((short) j)));
}
j++; }
content.put(i, model);
addCourse(model);
}
}

Java上传Excel并解析的更多相关文章

  1. java上传excel文件及解析

      java上传excel文件及解析 CreateTime--2018年3月5日16:25:14 Author:Marydon 一.准备工作 1.1 文件上传插件:swfupload: 1.2 文件上 ...

  2. java上传excel到后台解析入库

    背景:最近需要做一个excel模板导入的功能,以便用户可以自己增删改查数据,当然,只有特别的用户才能有此权限,捋了捋思路,还是从前端写起 实现: 页面最后的效果如下,可以自己修改,删除,导入导出数据, ...

  3. 使用ocupload和POI一键上传Excel并解析导入数据库

    使用的工具如下:  JQuery ocupload jquery.ocupload-1.1.2.js Apache POI poi-3.9.jar 如果是Maven项目添加依赖如下: <depe ...

  4. Springboot 上传excel并解析文件内容

    最近在做一个物业的系统,需要通过excel上传业主的信息,解析并入库. 参考:https://www.cnblogs.com/jyyjava/p/8074322.html 话不多说,直接上核心代码 i ...

  5. Java上传且后台解析XML文件

    后台代码: import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.InputStream ...

  6. java的poi技术下载Excel模板上传Excel读取Excel中内容(SSM框架)

    使用到的jar包 JSP: client.jsp <%@ page language="java" contentType="text/html; charset= ...

  7. postman上传excel,java后台读取excel生成到指定位置进行备份,并且把excel中的数据添加到数据库

    最近要做个前端网页上传excel,数据直接添加到数据库的功能..在此写个读取excel的demo. 首先新建springboot的web项目 导包,读取excel可以用poi也可以用jxl,这里本文用 ...

  8. Salesforce LWC学习(三十二)实现上传 Excel解析其内容

    本篇参考:salesforce lightning零基础学习(十七) 实现上传 Excel解析其内容 上一篇我们写了aura方式上传excel解析其内容.lwc作为salesforce的新宠儿,逐渐的 ...

  9. Uploadify上传Excel到数据库

    前两章简单的介绍了Uploadify上传插件的基本使用和相关的属性说明.这一章结合Uploadify+ssh框架+jquery实现Excel上传并保存到数据库.         以前写的这篇文章 Jq ...

随机推荐

  1. 静态html制作之psd转html

    很多时候我们开发拿到的是psd文件,这个时候如果公司有前端,会帮你制作成静态html, 很多小公司是没有这个配置的,所以只能业务开发的自己上马.可以有多种实现方式: 1.对于简单的切图,可以在线网站切 ...

  2. Azure Automation Deploy (ARM)

    以下说明仅是对虚拟机定时开关机的一个Demo,如果读者的定时任务比较复杂,需要通过修改Runbook脚本文件实现对应的功能. 1.创建automation账户 2.添加凭据 3.添加一个runbook ...

  3. java1.8--OptionalInt,OptionalDouble,OptionalLong类

    OptionalInt,OptionalDouble,OptionalLong类的工作方式与Optional类十分类似,只不过他们是专门操作int,都变了,long类型的值而设计的.因此,他们分别定义 ...

  4. wampserver 的Apache启动错误提示:The requested URL / was not found on this server.

    打开localhost显示以下错误 原因:之前我配置了虚拟主机,所以服务器是从虚拟环境访问的,localhost也就访问不到 解决方法:打开httpd.conf配置文件,将Include conf/e ...

  5. jenkins持续集成简介[一]

    标签(linux): jenkins 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 什么时持续集成? Continuous integration(CI) 持续 ...

  6. MySQL主从复制-xtrabackup的使用与延时复制(附原理图)

    标签(linux): mysql 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 xtrabackup是percona公司针对MySQL开发的一款开源的物理备份工 ...

  7. 【转】GPS定位原理

    一.距离测定原理 1.伪距测量 伪距测量是利用全球卫星定位系统进行导航定位的最基本的方法,其基本原理是:在某一瞬间利用GPS接收机同时测定至少四颗卫星的伪距,根据已知的卫星位置 和伪距观测值,采用距离 ...

  8. mkdir -p 参数的使用

    ssh root@%s -o ConnectTimeout=2 "ssh root@%s ConnectTimeout=2 "if [ ! -d /root/scripts ]; ...

  9. python--关于赋值与深浅拷贝的认识

    作为一个自学python的小白,平时用到深浅拷贝的机会很少,因此对其也是一知半解.但是,作为一个立志成为后端工程狮的男人,眼里揉不了沙子,于是专门花时间补了补课,在此记录一下学习心得.    在讲深浅 ...

  10. JAX-RS和Jersey

    一:JAX-RS JAX-RS是JAVA EE6 引入的一个新技术. JAX-RS即Java API for RESTful Web Services,是一个Java 编程语言的应用程序接口,支持按照 ...