java实现Excel的导入、导出
一、Excel的导入
publicstaticList<PutStorageInfo> readExcelByJXL(String filePath){
List<PutStorageInfo> infoList =newArrayList<PutStorageInfo>();
Map<String,List<String>> map =newHashMap<String,List<String>>();
infoList.clear();
try{
InputStream is =newFileInputStream(filePath);
Workbook workbook =Workbook.getWorkbook(is);
//获取第1张表
Sheet sheet = workbook.getSheet(0);
//获取总的列数
int columns = sheet.getColumns();
//获取总的行数
int rows = sheet.getRows();
//先列后行(j,i)
for(int i =1; i < rows; i++){
List<String> contentList =newArrayList<String>();
contentList.clear();
for(int j =1; j < columns; j++){
contentList.add(sheet.getCell(j,i).getContents());
}
map.put("StorageInfo"+i, contentList);
} //遍历map集合,封装成bean
for(Map.Entry<String,List<String>> entry : map.entrySet()){
List<String> list = entry.getValue();
PutStorageInfo storageInfo =newPutStorageInfo();
storageInfo.setProductcode(list.get(0));
storageInfo.setProductsort(list.get(1));
storageInfo.setProductbrand(list.get(2));
storageInfo.setProductname(list.get(3));
storageInfo.setProductquantity(list.get(4));
storageInfo.setProductcontent(list.get(5));
storageInfo.setProductnetweight(list.get(6));
storageInfo.setProductcountry(list.get(7));
storageInfo.setProductpdate(list.get(8));
storageInfo.setProductprice(list.get(9));
storageInfo.setProductmark(list.get(10)); infoList.add(storageInfo);
}
is.close();
}catch(Exception e){
e.printStackTrace();
}
return infoList;
}
方式二、POI导入
publicstaticList<PutStorageInfo> readExcelByPOI(String filePath){
List<PutStorageInfo> infoList =newArrayList<PutStorageInfo>();
Map<String,List<String>> map =newHashMap<String,List<String>>();
infoList.clear();
try{
InputStream is =newFileInputStream(filePath); int index = filePath.lastIndexOf(".");
String postfix = filePath.substring(index+1); Workbook workbook =null;
if("xls".equals(postfix)){
workbook =newHSSFWorkbook(is);
}elseif("xlsx".equals(postfix)){
workbook =newXSSFWorkbook(is);
}
//获取第1张表
Sheet sheet = workbook.getSheetAt(0);
//总的行数
int rows = sheet.getLastRowNum();
//总的列数--->最后一列为null则有问题,读取不完整,将表头的数目作为总的列数,没有的则补为null
int columns = sheet.getRow(0).getLastCellNum();
//先列后行
for(int i =1; i <= rows; i++){
Row row = sheet.getRow(i);
if(null!= row && row.getFirstCellNum()==-1){//这一行是空行,不读取
continue;
}
//这一行的总列数
// columns = row.getLastCellNum();
List<String> contentList =newArrayList<String>();
contentList.clear();
for(int j =1; j < columns; j++){
if(row.getCell(j)!=null){
row.getCell(j).setCellType(Cell.CELL_TYPE_STRING);
contentList.add(row.getCell(j).getStringCellValue());
}else{
contentList.add("");
}
}
map.put("StorageInfo"+i, contentList);
} //遍历map集合,封装成bean
for(Map.Entry<String,List<String>> entry : map.entrySet()){
List<String> list = entry.getValue();
PutStorageInfo storageInfo =newPutStorageInfo();
storageInfo.setProductcode(list.get(0));
storageInfo.setProductsort(list.get(1));
storageInfo.setProductbrand(list.get(2));
storageInfo.setProductname(list.get(3));
storageInfo.setProductquantity(list.get(4));
storageInfo.setProductcontent(list.get(5));
storageInfo.setProductnetweight(list.get(6));
storageInfo.setProductcountry(list.get(7));
storageInfo.setProductpdate(list.get(8));
storageInfo.setProductprice(list.get(9));
storageInfo.setProductmark(list.get(10)); infoList.add(storageInfo);
}
is.close();
}catch(Exception e){
e.printStackTrace();
} return infoList;
}
publicstaticvoid creatExcel(List<PutStorageInfo> storageInfoList,String fileName){
try{
OutputStream os =newFileOutputStream(fileName);
//创建可写的工作薄
WritableWorkbook workbook =Workbook.createWorkbook(os);
//创建第一张表
WritableSheet sheet = workbook.createSheet("Sheet1",0);
//设置根据内容自动宽度
CellView cellView =newCellView();
cellView.setAutosize(true);
//在下边for循环中为每一列设置 //设置列宽度,此种方式参数的意思,i-->对应的行或列 j-->要设置的宽度
// sheet.setColumnView(0, 100);
// sheet.setRowView(0, 300);
//设置字体加粗且背景颜色为黄色
WritableFont boldFont =newWritableFont(WritableFont.ARIAL,10,WritableFont.BOLD);//黑体
WritableCellFormat cellrFormate =newWritableCellFormat(boldFont);
cellrFormate.setBackground(Colour.YELLOW);
//先添加表头
List<String> titleList = getTitleList();
//循环创建单元格,先列后行
for(int i =0; i < titleList.size(); i++){
//sheet.setColumnView(i, cellView);
sheet.setColumnView(i,20); Label label =newLabel(i,0, titleList.get(i), cellrFormate);
sheet.addCell(label);
} LogUtil.logOut(JXLWriteExcel.class,storageInfoList.size()+""); String[][] content = convertToArr(storageInfoList);
//设置content的自适应当前列的宽度,文本太对会自动换行 new Label(j, i+1, content[i][j-1],contentFormat);
WritableCellFormat contentFormat =newWritableCellFormat();
contentFormat.setWrap(true); //然后添加入库信息条目
for(int i =0; i < storageInfoList.size(); i++){
Label labelID =newLabel(0,i+1,(i+1)+"");
sheet.addCell(labelID); for(int j =1; j < titleList.size(); j++){
Label label =newLabel(j, i+1, content[i][j-1]);
sheet.addCell(label);
}
} //把创建的内容写入到输出流中,并关闭输出流
workbook.write();
workbook.close();
os.close(); //将存储了入库bean的list清空
storageInfoList.clear(); }catch(Exception e){
e.printStackTrace();
}
} privatestaticString[][] convertToArr(List<PutStorageInfo> storageInfoList){
String[][] content =newString[storageInfoList.size()][11];
for(int i =0; i < storageInfoList.size(); i++){
PutStorageInfo info = storageInfoList.get(i);
//每个bean中总项有11项
content[i][0]= info.getProductcode();
content[i][1]= info.getProductsort();
content[i][2]= info.getProductbrand();
content[i][3]= info.getProductname();
content[i][4]= info.getProductquantity();
content[i][5]= info.getProductcontent();
content[i][6]= info.getProductnetweight();
content[i][7]= info.getProductcountry();
content[i][8]= info.getProductpdate();
content[i][9]= info.getProductprice();
content[i][10]= info.getProductmark();
}
return content; } privatestaticList<String> getTitleList(){
List<String> list =newArrayList<String>();
list.add("Item No.");
list.add("Product code");
list.add("Sort");
list.add("Brand");
list.add("Product Name");
list.add("Quantity(Pieces)");
list.add("Content");
list.add("Net Weight");
list.add("Country");
list.add("Best before date");
list.add("Price(EURO)");
list.add("Remarks"); return list;
}
java实现Excel的导入、导出的更多相关文章
- java实现excel的导入导出(poi详解)[转]
java实现excel的导入导出(poi详解) 博客分类: java技术 excel导出poijava 经过两天的研究,现在对excel导出有点心得了.我们使用的excel导出的jar包是poi这个 ...
- java 中Excel的导入导出
部分转发原作者https://www.cnblogs.com/qdhxhz/p/8137282.html雨点的名字 的内容 java代码中的导入导出 首先在d盘创建一个xlsx文件,然后再进行一系列 ...
- JAVA对Excel的导入导出
今天需要对比2个excel表的内容找出相同:由于要学的还很多上手很慢所以在这做个分享希望对初学的有帮助: 先是pom的配置: <dependency> <groupId>org ...
- java实现excel的导入导出(poi详解)
经过两天的研究,现在对excel导出有点心得了.我们使用的excel导出的jar包是poi这个阿帕奇公司的一个项目,后来被扩充了.是比较好用的excel导出工具. 下面来认识一下这个它吧. 我们知道要 ...
- Java实现大批量数据导入导出(100W以上) -(三)超过25列Excel导出
前面一篇文章介绍大数据量导出实现: Java实现大批量数据导入导出(100W以上) -(二)导出 这篇文章在Excel列较少时,按以上实际验证能很快实现生成.但如果列较多时用StringTemplat ...
- excel的导入导出的实现
1.创建Book类,并编写set方法和get方法 package com.bean; public class Book { private int id; private String name; ...
- java实现文件批量导入导出实例(兼容xls,xlsx)
1.介绍 java实现文件的导入导出数据库,目前在大部分系统中是比较常见的功能了,今天写个小demo来理解其原理,没接触过的同学也可以看看参考下. 目前我所接触过的导入导出技术主要有POI和iRepo ...
- Java实现大批量数据导入导出(100W以上) -(二)导出
使用POI或JXLS导出大数据量(百万级)Excel报表常常面临两个问题: 1. 服务器内存溢出: 2. 一次从数据库查询出这么大数据,查询缓慢. 当然也可以分页查询出数据,分别生成多个Excel打包 ...
- poi实现excel的导入导出功能
Java使用poi实现excel的导入导出功能: 工具类ExcelUtil,用于解析和初始化excel的数据:代码如下 package com.raycloud.kmmp.item.service.u ...
- Excel导入导出工具(简单、好用且轻量级的海量Excel文件导入导出解决方案.)
Excel导入导出工具(简单.好用且轻量级的海量Excel文件导入导出解决方案.) 置顶 2019-09-07 16:47:10 $9420 阅读数 261更多 分类专栏: java 版权声明:本 ...
随机推荐
- 关于使用命令添加jar进自己的pom文件中-maven项目添加jar包
现在几乎开发项目都是使用的maven项目,但是有的时候可以使用比较偏门或者新的jar可能在网上搜不到在pom文件里的配置应该如何写,因此写下这篇博客. 比如我现在想加入的AAA.jar这个包 打开cm ...
- C#集合之有序列表
如果需要基于键对所需集合排序,就可以使用SortedList<TKey,TValue>类.这个类按照键给元素排序.这个集合中的值和键都可以使用任何类型.定义为键的自定义类型需要实现ICom ...
- 使用MyEclipse生成实体类和Hibernate映射文件
1.打开MyEclipse DataBase Explorer. 如上图,点击图中2便可切换到MyEclipse DataBase Explorer,若没有该图标,则点击图中1,便可选择MyEclip ...
- MarkDown语法 学习笔记 效果源码对照
MarkDown基本语法学习笔记 Markdown是一种可以使用普通文本编辑器编写的标记语言,通过简单的标记语法,它可以使普通文本内容具有一定的格式. 下面将对Markdown的基本使用做一个介绍 目 ...
- 腾讯云更换yum源
最近公司使用了云服务器代替了机房的一些服务器,然而选择了腾讯云,个人观点通过比较还是阿里云好了点,可能为了使用腾讯云的一些其他功能吧,书归正文. 由于腾讯的源网站经常挂,在我使用过的一台腾讯云主机的时 ...
- Hibernate笔记一:HIbernate配置-HelloWorld
hibernate介绍: Hibernate是一个开源的对象关系映射框架,它对JDBC进行了轻量级的对象封装,使Java程序员可以随心所欲的使用对象编程思维来操纵数据库. 它不仅提供了从java类到数 ...
- ztree实现权限功能(横向显示)
最近在做权限功能的时候,采用的ztree实现的,但是产品要求最后一层的权限节点要横向显示.开始在网上找的解决方案是用css样式把最后一层的display设置为inline.在我本地电脑上看了下.效果不 ...
- 如何使用华为软件开发云快速部署PHP网站
华为软件开发云这个工具,从去年推出我就一直在关注,毕竟是华为最新的一款软件开发工具,最近我一直在使用华为软件开发云进行开发项目管理,它有在线编译和构建.云端在线代码检查等功能,编译省去了很多物理机器的 ...
- Android studio 3.0 引起的 outputFile sync failed:not vaild
我们大多使用 android studio 改变生成安装包命名会用以下方式: applicationVariants.all { variant -> variant.outputs.each ...
- javaWeb学习总结(10)- Filter(过滤器)学习(2)
在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可以使用Decorator(装饰器)模式对request.response对象进行包装,再把包装对象传给目 ...