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. servlet入门学习之生命周期

    一. 什么是Servlet Servlet是用Java语言编写的服务器端小程序,驻留在web服务器中,并在其中运行,扩展了web服务器的动态处理功能. 用java语言编写的java类 在web容器中运 ...

  2. linux 磁盘加密和tpm搭配使用1

    一.基本名称,容易混淆 1.dm-crypt是linux的2.6内核开始集成的一种磁盘加密功能.十几年来,连sche调度算法都被改了N次,但dm-crypt一直稳定在内核中,稳定性还是很好的. 2.c ...

  3. linux 巨页使用测试以及勘误1

    linux使用hugetlbfs的方式来支持巨页,也成为大页. 网上看到有人说巨页不支持read,和write调用,只支持mmap,但是看3.10内核代码的时候发现: const struct fil ...

  4. linux_目录结构

    目录的作用是什么? 1. 归档和分类 2. 区分同名文件 什么是FHS? 目录层次标准,linux目录规范标准 linux系统目录有哪些特点? 1. 逻辑上所有目录都在 / 目录下,根目录是所有目录的 ...

  5. windows 下安装和运行 hadoop

    windows下安装hadoop,直接去官网采用简单暴力的方法: 1.下载hadoop的安装包:http://hadoop.apache.org/->左边点Releases->点mirro ...

  6. 前端工程构建工具FIS3

    FIS3 是面向前端的工程构建工具.解决前端工程中性能优化.资源加载(异步.同步.按需.预加载.依赖管理.合并.内嵌).模块化开发.自动化工具.开发规范.代码部署等问题. 一.安装 全局安装fis3 ...

  7. 【转】shell学习笔记(六)——流程控制之for循环

    基本语法格式: for 变量 in 列表 do 命令行(通常用到循环变量) done ********Linux Shell for循环写法总结******** for((i=1;i<</ ...

  8. TCP连接之未连接队列的理解[转]

    tcp服务器在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接. 第一次握手:建立连接时,客户端发送syn包(syn=j)到服务器,并进入SYN_SEND状态,等待服务器确认 ...

  9. Effective Java 之-----消除过期的对象引用

    public class Stack { private Object[] elements; private int size = 0; private static final int DEFAU ...

  10. Optaplanner - 入门介绍

    OptaPlanner背景 在上一篇里喷了不少水,这一篇准备放点干货:其实也没办法完全干,因为很多预备知道在交待一下.好了,说一下关于OptaPlanner的背景.应用兼容性及其原理. 这一篇先说一下 ...