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 版权声明:本 ...
随机推荐
- Android自定义评分控件:RatingStarView
RatingStarView Android自定义的评分控件,类似ProgressBar那样的,使用星星图标(full.half.empty)作为progress标识的评分/打分控件. 效果图 图1: ...
- 使用java API操作hdfs--拷贝部分文件到hdfs
要求如下: 自行在本地文件系统生成一个大约一百多字节的文本文件,写一段程序(可以利用Java API或C API),读入这个文件,并将其第101-120字节的内容写入HDFS成为一个新文件. impo ...
- LeetCode5. Longest Palindromic Substring 最长回文子串 4种方法
题目链接:https://leetcode.com/problems/longest-palindromic-substring/ 题意很简单,就是求一个字符串得最长子串,这里的子串指连续的. 本文给 ...
- 网站的高性能架构---Web前端性能优化
网站性能测试 不同视角下的网站性能 用户视角的网站性能:从用户角度,网站性能就是用户在浏览器上直观感受到的网站响应速度.用户的感受时间包括用户计算机和网站服务器通信的时间.网站服务器处理请求时间.用户 ...
- webpack的多文件打包问题
1.第三方库如vue,vue-router可以利用webpack中的entry指定vendor:['vue','vue-router']来打包在一个文件中 2.将这些文件单独提取出来,在页面中使用&l ...
- JSP servlet的配置与使用
1. servlet 的配置文件内容如下所示 <servlet> <description>This is the description of my J2EE com ...
- Libevent源码分析—event_set()
初始化完event_base后,下面要初始化event,通过调用event_set()实现 .相关源码位于event.c event_set() void event_set(struct event ...
- nodeJS之二进制buffer对象
前面的话 在ES6引入TypedArray之前,JavaScript语言没有读取或操作二进制数据流的机制.Buffer类被引入作为Nodejs的API的一部分,使其可以在TCP流和文件系统操作等场景中 ...
- linux平台下Hadoop下载、安装、配置
在这里我使用的linux版本是CentOS 6.4 CentOS-6.4-i386-bin-DVD1.iso 下载地址: http://mirrors.aliyun.com/cen ...
- javascript痛点之三闭包
先来看看第一节的例子 'use strict'; function num(){ //用var声明一个变量num1 var num1 = 15; } alert(num1);//num1 is not ...