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. SSH key introduction

    Preface At the first time, we take the connection with GitLab remote server. You need to type userna ...

  2. php连接memcahed出现Cannot assign requested address (99)的解决方法

    今天在将服务器合并后,发现php偶尔会报出 Server *.*.*.* (tcp *****) failed with: Cannot assign requested address (99) 的 ...

  3. python_5_模块

    创:5_4_2017 修: 什么是模块? --标准库+第三方库+自定义,为实现某一方面的功能集合(变量,函数,类) 如何安装第三方库? --pip install 第三方库 如何导入和使用模块? -- ...

  4. xml格式字符串转为Map

    import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom ...

  5. linkin大话数据结构--泛型

    泛型(Generic) 什么是泛型? java5开始出现的一种对Java语言类型的一种拓展,以支持创建可以按类型进行参数化的类.可以把类型参数看作是使用参数类型时指定的类型占位符,就好比方法的形式参数 ...

  6. IO (五)

    1 序列化和反序列化 1.1 ObjectOutputStream 序列化 1.1.1 概述 ObjectOutputStream将Java对象的基本数据写入OutputStream,可以使用Obje ...

  7. java IO(一):File类

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  8. git修改最后一次commit的内容

    提交修改 $ git add test.txt $ git commit -m "提交test.txt文件" 修改注释说明 如果需要修改commit的注释说明,则执行以下命令: $ ...

  9. 07_Python变量内存地址、小数据池

    一.变量在内存中的地址 变量:用来标识(identify)一块内存区域.为了方便表示内存,我们操作变量实质上是在操作变量指向的那块内存单元.编译器负责分配.我们可以使用Python内建函数id()来获 ...

  10. vue中多个input绑定enter按键事件

    默认再每个input上加 keyup.enter 是可以绑定事件的 可是多个input一起绑定同一个事件就比较麻烦 所以就在vue dom里面遍历查找并绑定事件 let handleEnter = ( ...