java实现Excel的导入、导出

所属分类: 软件编程 / java 阅读数: 25
收藏 0 赞 0 分享

一、Excel的导入

导入可采用两种方式,一种是JXL,另一种是POI,但前者不能读取高版本的Excel(07以上),后者更具兼容性。由于对两种方式都进行了尝试,就都贴出来分享(若有错误,请给予指正)

方式一、JXL导入  所需jar包 JXL.jar

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导入 

所需jar包
poi-3.6-20091214.jar
poi-ooxml-3.6-20091214.jar
poi-ooxml-schemas-3.6-20091214.jar
xmlbeans-2.3.0.jar
dom4j-1.6.1.jar
jdom-2.0.6.jar

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;
} 

二、Excel导出

采用JXL实现

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的面向对象编程基本概念学习笔记整理

这篇文章主要介绍了Java的面向对象编程基本概念学习笔记整理,包括类与方法以及多态等支持面向对象语言中的重要特点,需要的朋友可以参考下
收藏 0 赞 0 分享

Eclipse下编写java程序突然不会自动生成R.java文件和包的解决办法

这篇文章主要介绍了Eclipse下编写java程序突然不会自动生成R.java文件和包的解决办法 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

基于Java实现杨辉三角 LeetCode Pascal's Triangle

这篇文章主要介绍了基于Java实现杨辉三角 LeetCode Pascal's Triangle的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Java中Spring获取bean方法小结

Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,如何在程序中获取Spring配置的bean呢?下面通过本文给大家介绍Java中Spring获取bean方法小结,对spring获取bean方法相关知识感兴趣的朋友一起学习吧
收藏 0 赞 0 分享

如何计算Java对象占用了多少空间?

在Java中没有sizeof运算符,所以没办法知道一个对象到底占用了多大的空间,但是在分配对象的时候会有一些基本的规则,我们根据这些规则大致能判断出来对象大小,需要的朋友可以参考下
收藏 0 赞 0 分享

剖析Java中的事件处理与异常处理机制

这篇文章主要介绍了Java中的事件处理与异常处理机制,讲解Java是如何对事件或者异常作出响应以及定义异常的一些方法,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Java的Struts2框架的结构及其数据转移方式

这篇文章主要介绍了详解Java的Struts2框架的结构及其数据转移方式,Struts框架是Java的SSH三大web开发框架之一,需要的朋友可以参考下
收藏 0 赞 0 分享

Java封装好的mail包发送电子邮件的类

本文给大家分享了2个java封装好的mail包发送电子邮件的类,并附上使用方法,小伙伴们可以根据自己的需求自由选择。
收藏 0 赞 0 分享

在Java的Struts中判断是否调用AJAX及用拦截器对其优化

这篇文章主要介绍了在Java的Struts中判断是否调用AJAX及用拦截器对其优化的方法,Struts框架是Java的SSH三大web开发框架之一,需要的朋友可以参考下
收藏 0 赞 0 分享

java多线程Future和Callable类示例分享

JAVA多线程实现方式主要有三种:继承Thread类、实现Runnable接口、使用ExecutorService、Callable、Future实现有返回结果的多线程。其中前两种方式线程执行完后都没有返回值,只有最后一种是带返回值的。今天我们就来研究下Future和Callab
收藏 0 赞 0 分享
查看更多