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 ...
随机推荐
- Ring3句柄表的枚举
由于windows并没有给出枚举所有句柄所用到的API,要获得句柄,我们必须使用未公开的Native API才可以,使用如下函数: NTSTATUS WINAPI NtQuerySystemInfor ...
- Source Insight、Xshell(putty)、Xftp
body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...
- Effective Java 第三版——29. 优先考虑泛型
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
- web项目各个clean
project clean:清楚tomcat下的已编译的java类.class文件,包括js但不包括jsp server clean:clean tomcat work dictionary:清除to ...
- junit断言总结
我们平时编写自己的测试类,如果没有断言,那么就没写测试的必要了. JUnit框架用一组assert方法封装了最常见的测试任务.这些assert方法可以极大地简化单元测试的编写. Assert类包含了一 ...
- PHP7.1 报错 Warning Illegal string offset
报错如下: Warning: Illegal string offset '阿根廷' in F:\wnmp\www\test.php on line 24 Warning: Illegal strin ...
- iOS页面切换动画实现方式。
iOS页面切换动画实现方式. 1.使用UIView animateWithDuration:animations:completion方法 Java代码 [UIView animateWithDura ...
- JAVA常用知识点总结---集合篇
一.Collection 与 Collections的区别:1. Collections:java.util.Collections 是一个包装类.它包含有各种有关集合操作的静态多态方法.此类不能实例 ...
- 覆盖equals()时总要覆盖hashCode()
覆写如下: public class User{ private Integer id; private String userName; private String passWord; publi ...
- 浅谈python中的闭包函数
闭包函数初探 通常我们定义函数都是这样定义的 def foo(): pass 其实在函数式编程中,函数里面还可以嵌套函数,如下面这样 def foo(): print("hello worl ...