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 版权声明:本 ...
随机推荐
- spring cloud微服务搭建第一天
martin fowler大神提出微服务的概念后,各种微服务的技术满天飞,现在用的比较多的是spring cloud和阿里的dubbo,由于dubbo 在16年10月份就停止更新了,这里我们讲解spr ...
- java线程(四)
java5线程并发库 线程并发库是JDK 1.5版本级以上才有的针对线程并发编程提供的一些常用工具类,这些类被封装在java.concurrent包下. 该包下又有两个子包,分别是atomic和loc ...
- Lazyman功能实现
题目要求是这样的: 实现一个LazyMan,可以按照以下方式调用: LazyMan("Hank")输出: Hi! This is Hank! LazyMan("Hank& ...
- 最简单的html5语言
什么是 HTML5? HTML5 是下一代 HTML 标准. 最小的HTML5文档 下面是一个简单的HTML5文档: <</span>!DOCTYPE html><< ...
- python selenium 元素定位(三)
上两篇的博文中介绍了python selenium的环境搭建和编写的第一个自动化测试脚本,从第二篇的例子中看出来再做UI级别的自动化测试的时候,有一个至关重要的因素,那就是元素的定位,只有从页面上找到 ...
- 在wamp下使用netbeans开启Xdbug
1.到http://www.xdebug.org 下载与PHP对应的xdebug版本,也可以把phpinfo源码粘贴到http://www.xdebug.org/find-binary.php,提交后 ...
- 用css3过滤做遮罩效果
<!DOCTYPE html><html ng-app="myApp" ng-controller="myController">< ...
- mysql之 binlog维护详细解析(开启、binlog相关参数作用、mysqlbinlog解读、binlog删除)
binary log 作用:主要实现三个重要的功能:用于复制,用于恢复,用于审计.binary log 相关参数:log_bin设置此参数表示启用binlog功能,并指定路径名称log_bin_ind ...
- 使用wamp扩展php时出现服务未启动的解决方法
今天在使用wamp扩展php的插件时,出现了如下图所示的错误提示 网上查了查,都说是端口原因,修改Apache的 80端口,但是并没有解决问题. 最后我终于找到了解决方法,步骤很简单,如下: 首先,在 ...
- angular实现的文字上下无缝滚动
最近在学习angularJs,业余时间随便写了一个文字上下无缝滚动的例子,主要写了一个小小的指令. css代码:主要控制样式 <style type="text/css"&g ...