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 版权声明:本 ...
随机推荐
- 产品经理学Python:条件控制
条件控制其实就是if...else...(如果...条件是成立的,就做...:反之,就做...)的使用,其基本结构是: 具体看下面这个例子: def account_login(): # 定义函数 p ...
- ③jQuery生成html元素
动态生成HTML元素 绑定事件
- R语言结合概率统计的体系分析---数字特征
现在有一个人,如何对这个人怎么识别这个人?那么就对其存在的特征进行提取,比如,提取其身高,其相貌,其年龄,分析这些特征,从而确定了,这个人就是这个人,我们绝不会认错. 同理,对数据进行分析,也是提取出 ...
- php学习之重要内置函数
1. require_once()函数 此函数在脚本执行期间包含并执行指定的文件,与require语句类似,唯一区别是如果该文件中的代码已经被包含了,则不会再次包含. require_once()函数 ...
- maven的三大生命周期
一.Maven的生命周期 Maven的生命周期就是对所有的构建过程进行抽象和统一.包含了项目的清理.初始化.编译.测试.打包.集成测试.验证.部署和站点生成等几乎所有的构建步骤. Maven的生命周期 ...
- 转:CentOS---网络配置详解
一.配置文件详解在RHEL或者CentOS等Redhat系的Linux系统里,跟网络有关的主要设置文件如下: /etc/host.conf 配置域名服务客户端的控制文件/etc/hos ...
- JAVA的Condition详解
Condition 将 Object 监视器方法(wait().notify()和notifyAll())分解成截然不同的对象,以便通过将这些对象与任意Lock实现组合使用,为每个对象提供多个等待 s ...
- 使用cl编译C/C++
每次写程序都是用VS下打开的,而且有智能提示,经常很容易看到自己哪里写错了,其实想联系自己写代码的能力,不应该要这些的,纯粹的不要智能提示 所以自己想用轻量级的编辑器写,然后就用了notepad++( ...
- 用CSS3实现饼状loading效果
原文地址:http://visugar.com/2017/05/17/CSS3%E9%A5%BC%E7%8A%B6loading%E6%95%88%E6%9E%9C/ 写在前面 (附录有源码及效果) ...
- Spark实战之读写HBase
1 配置 1.1 开发环境: HBase:hbase-1.0.0-cdh5.4.5.tar.gz Hadoop:hadoop-2.6.0-cdh5.4.5.tar.gz ZooKeeper:zooke ...