Java上传Excel并解析
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并解析的更多相关文章
- java上传excel文件及解析
java上传excel文件及解析 CreateTime--2018年3月5日16:25:14 Author:Marydon 一.准备工作 1.1 文件上传插件:swfupload: 1.2 文件上 ...
- java上传excel到后台解析入库
背景:最近需要做一个excel模板导入的功能,以便用户可以自己增删改查数据,当然,只有特别的用户才能有此权限,捋了捋思路,还是从前端写起 实现: 页面最后的效果如下,可以自己修改,删除,导入导出数据, ...
- 使用ocupload和POI一键上传Excel并解析导入数据库
使用的工具如下: JQuery ocupload jquery.ocupload-1.1.2.js Apache POI poi-3.9.jar 如果是Maven项目添加依赖如下: <depe ...
- Springboot 上传excel并解析文件内容
最近在做一个物业的系统,需要通过excel上传业主的信息,解析并入库. 参考:https://www.cnblogs.com/jyyjava/p/8074322.html 话不多说,直接上核心代码 i ...
- Java上传且后台解析XML文件
后台代码: import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.InputStream ...
- java的poi技术下载Excel模板上传Excel读取Excel中内容(SSM框架)
使用到的jar包 JSP: client.jsp <%@ page language="java" contentType="text/html; charset= ...
- postman上传excel,java后台读取excel生成到指定位置进行备份,并且把excel中的数据添加到数据库
最近要做个前端网页上传excel,数据直接添加到数据库的功能..在此写个读取excel的demo. 首先新建springboot的web项目 导包,读取excel可以用poi也可以用jxl,这里本文用 ...
- Salesforce LWC学习(三十二)实现上传 Excel解析其内容
本篇参考:salesforce lightning零基础学习(十七) 实现上传 Excel解析其内容 上一篇我们写了aura方式上传excel解析其内容.lwc作为salesforce的新宠儿,逐渐的 ...
- Uploadify上传Excel到数据库
前两章简单的介绍了Uploadify上传插件的基本使用和相关的属性说明.这一章结合Uploadify+ssh框架+jquery实现Excel上传并保存到数据库. 以前写的这篇文章 Jq ...
随机推荐
- jQuery时间格式插件-moment.js的使用
jQuery时间格式插件-moment.js的使用 moment.js插件的使用,使用之前在页面引入对应的js文件: 详细的操作可见moment中文官网:http://momentjs.cn/ 日期格 ...
- Intellij-创建Maven项目速度慢
原因: IDEA根据maven archetype的本质,其实是执行mvn archetype:generate命令,该命令执行时,需要指定一个archetype-catalog.xml文件. 该命令 ...
- margin 和 padding 的本质区别
问题? 如何弄清 margin 和 padding之间的区别? 那,答案呢? margin 边界, padding 填充 假如有一个盒子,padding就相当于盒子的厚度,盒子大小固定,通过修改pad ...
- 1 let和const
let命令 1)let声明的变量只在let命令所在的代码块内有效,如: { let a = ; ;} a // ReferenceError: a is not defined. b 对于for循 ...
- Servlet--继承HttpServlet写自己的Servlet
前面2篇关注的都是Servlet接口,在实际编码中一般不直接实现这个接口,而是继承HttpServlet类.因为j2e的包里面写好了GenericServlet和HttpServlet类来让我们简化编 ...
- Android开发之蓝牙Socket
蓝牙Server端就是通过线程来注册一个具有名称和唯一识别的UUID号的BluetoothServerSocket, 然后就一直监听Client端(BluetoothSocket)的请求,并对这些请求 ...
- 【转】UNIX时间戳与.net日期类的转换
1 将系统时间转换成UNIX时间戳 DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970,1,1)); ...
- The man Command
The man command is used to format and display the man pages. The man pages are a user manual that is ...
- Akamai CDN
Akamai CDN中的几个重要组件 mapping system 调度系统(映射client到edge cluster,进而到edge server) edge server platform 边缘 ...
- JAVA 调用 R 语言之升华篇
由于项目的需要,最近做了一个javaWeb调用R的组件,在这里,我把自己走的一些弯路给大家总结一下: 一.选取什么插件作为java和R之间的通信? 1. 在传统的方式中,大致可以分为两类:一类是JRI ...