nongzha
2015-06-15 11:11:02
原
java通过jxls框架实现导入导出excel文件实例
//使用jxls报表生成工具,把java实体类导出生成 Excel文件或导入 Excel 插入数据库 //读取 public class ReadExcel { private final static String xmlConfig="student.xml"; public List read(){ InputStream inputXML = new BufferedInputStream(ReadExcel.class.getResourceAsStream(xmlConfig)); XLSReader mainReader; String path=ReadExcel.class.getResource("/").getPath(); path=path.substring(1,path.indexOf("/WebRoot")+1)+"WebRoot/Excel/stu.xls"; try { mainReader = ReaderBuilder.buildFromXML(inputXML ); InputStream inputXLS = new BufferedInputStream(new FileInputStream(path)); Student stu=new Student(); List students = new ArrayList(); Map beans = new HashMap(); beans.put("stu", stu); beans.put("students", students); XLSReadStatus readStatus = mainReader.read(inputXLS, beans); return students; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static void main(String[] args) { ReadExcel re=new ReadExcel(); List<Student> list=re.read(); System.out.println("ID\t name\t subject\t score"); for(Student stu:list){ System.out.println(stu.getIdname()+"\t "+stu.getName()+"\t "+stu.getSubject()+"\t "+stu.getScorename()); } } }
//写入 public class WriteExcel { public static void write(List list){ List students = new ArrayList(); Map beans = new HashMap(); beans.put("students", list); XLSTransformer transformer = new XLSTransformer(); String path=ReadExcel.class.getResource("/").getPath(); path=path.substring(1,path.indexOf("/WebRoot")+1)+"WebRoot/Excel/"; try { transformer.transformXLS(path+"/student.xls", beans, path+"/stus.xls"); } catch (ParsePropertyException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public List getStudetns(){ List<Student> list=new ArrayList<Student>(); Student stu=null; PreparedStatement pre=null; ResultSet re=null; try{ pre=DBConector.getCon().prepareStatement("select * from student"); re=pre.executeQuery(); while(re.next()){ stu=new Student(); stu.setId(re.getLong(1)); stu.setName(re.getString(2)); stu.setSubject(re.getString(3)); stu.setScore(re.getLong(4)); list.add(stu); } }catch(Exception e){ e.printStackTrace(); }finally{ try{ if(re!=null) re.close(); if(pre!=null) pre.close(); }catch(Exception e){ } } return list; } public static void main(String[] args) { WriteExcel w=new WriteExcel(); w.write(w.getStudetns()); } }
//sql create table STUDENT ( ID NUMBER(8) not null primary key, NAME VARCHAR2(50) not null, SUBJECT VARCHAR2(50) not null, SCORE NUMBER(8) )
Excel模板文件:
对应Excel文件
猜你喜欢
- apache poi操作excel实现导入导出的demo,有easyui实现的前台界面
- java Excel导入导出工具类icexls实例(导入导出各只需两行代码)
- java实现一个配置简单功能强大的excel工具类,搞定大多数excel文件导入导出
- java 通过Apache poi导出excel代码demo实例
- springboot整合easyexcel实现excel文件上传解析,下载导出简单实例
- springboot+mybatis+apache poi实现把excel文件导入数据库
- springboot2+mybatis+EasyExcel实现从mysql数据库在线导出excel文件
- java poi 导出excel表格
- java实现无组件导出CSV文件的代码片段
- java导出easyUI的Excel文件实例,简单操作
- Java web简单的word文件导出例子
- apache poi报表导入导出实例 基于服务器下载报表。导入项目既可以运行。
请下载代码后再发表评论
文件名:jxls-poi.rar,文件大小:2850.34K
下载
- /
- /jxls-poi
- /jxls-poi/.classpath
- /jxls-poi/.mymetadata
- /jxls-poi/.project
- /jxls-poi/WebRoot
- /jxls-poi/WebRoot/Excel
- /jxls-poi/WebRoot/Excel/stu.xls
- /jxls-poi/WebRoot/Excel/student.xls
- /jxls-poi/WebRoot/Excel
- /jxls-poi/src
- /jxls-poi/src/org
- /jxls-poi/src/org/terry
- /jxls-poi/src/org/terry/jxls
- /jxls-poi/src/org/terry
- /jxls-poi/src/org
- /jxls-poi


王涛的1 LV1
2023年8月4日
jinandfei LV12
2021年12月8日
sl0018 LV13
2021年9月23日
1_2_3_4_5_6_7 LV1
2021年2月21日
hdf999 LV12
2021年1月13日
子陵96 LV4
2020年9月7日
wjh12345654321 LV14
2020年7月17日
zmy001 LV11
2020年5月19日
丫丫123456 LV3
2020年2月26日
1848472526 LV15
2019年10月15日

597117933 LV9
5月27日
王涛的1 LV1
2023年8月4日
gvin001 LV14
2023年5月11日
微信网友_6411404521459712
2023年3月30日
暂无贡献等级
是你爸爸啊100 LV5
2022年8月8日
duanyui LV3
2022年4月13日
微信网友_5845420553359360 LV4
2022年2月25日
jinandfei LV12
2021年12月8日
Yanxigul
2021年12月1日
暂无贡献等级
tansuo阿郎 LV8
2021年11月29日