首先下载jxl-2.6.12.jar或者在maven中添加
<dependency> <groupId>net.sourceforge.jexcelapi</groupId> <artifactId>jxl</artifactId> <version>2.6.12</version> </dependency>
后台代码如下:
WritableWorkbook wwb = null; wwb=Workbook.createWorkbook(new File("c:\\1.xls"));//创建excel文件 WritableSheet ws = wwb.createSheet("sheet1", 0);//创建一个可写入的工作表 ws.setColumnView(11,30);//设置列的宽度 ws.setColumnView(0,25); ws.setColumnView(6,20); ws.setColumnView(9,20); //设置表头 ws.addCell(new Label(0,0,"商品图片")); ws.addCell(new Label(1,0,"商品名称")); ws.addCell(new Label(2,0,"市场价格")); ws.addCell(new Label(3,0,"会员价格")); ws.addCell(new Label(4,0,"被测人")); ws.addCell(new Label(5,0,"状态")); ws.addCell(new Label(6,0,"收样品时间")); ws.addCell(new Label(7,0,"收件人")); ws.addCell(new Label(8,0,"物流单号")); ws.addCell(new Label(9,0,"发报告时间")); ws.addCell(new Label(10,0,"物流单号")); ws.addCell(new Label(11,0,"检测报告")); //添加行数据,遍历需要写入excel的集合数据 for (int i=1;i<=odvs.size();i++){ OrderDetailVo row=odvs.get(i-1); System.out.println(row); ws.addCell(new Label(0,i,row.getThumbNail())); ws.addCell(new Label(1,i,row.getName())); ws.addCell(new Label(2,i,row.getMarketPrice().toString())); ws.addCell(new Label(3,i,row.getMemberPrice().toString())); ws.addCell(new Label(4,i,row.getPersonName())); ws.addCell(new Label(5,i,row.getStatus())); ws.addCell(new Label(6,i,row.getRecieveOrderDate().toString())); ws.addCell(new Label(7,i,row.getContact())); ws.addCell(new Label(8,i,row.getReceiveTrackNumber())); ws.addCell(new Label(9,i,row.getReportSendDate().toString())); ws.addCell(new Label(10,i,row.getReportTrackNumber())); ws.addCell(new Label(11,i,row.getReportDownloadPath())); } wwb.write();//从内存中写入文件中 wwb.close(); //关闭资源,释放内存
转换后的1.xls文档如下
参考文章:
http://pengfeicao521.iteye.com/blog/1415699
本文出自 “一无所有-天行者” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1680812
原文地址:http://tianxingzhe.blog.51cto.com/3390077/1680812