这两天在做一个项目,要用到导出excel的功能,因为以前没使用过这个功能,和我一起学习吧
首先你要下载最新的poi插件。在java中使用ssh框架的时候你就没有必要自己下载poi了,因为在你添加框架支持的时候spring已经为你添加了poi插件,有现成的干嘛不用呢。
下载完毕
在项目里面添加poi的支持jar包。
接下来就是进行开发
首先就是创建一个工作薄,在官方文档里面有标准的创建代码可以参考
在这个里面有两个东东不知道你发现了没有HSSF和XSSF。知道他们的区别吗??其实你google也是可以找到的
其实在官方文档里面有明确的说明,
HSSF是POI项目的纯Java实现的Excel'97(-2007)文件格式。XSSF是POI项目的纯Java实现Excel 2007中的。xlsx(OOXML)文件格式。
HSSF和XSSF提供电子表格的创建,修改,读取和写入XLS电子表格的方式来阅读。
HSSF和XSSF提供电子表格的创建,修改,读取和写入XLS电子表格的方式来阅读。
我用的是Hssf
好了创建完工作薄之后就是创建单元格了,我在这边也不啰嗦太多,直接贴代码,因为在官方API文档里面都有现成的指南
HSSFWorkbook wb=new HSSFWorkbook();
HSSFSheet sheet=wb.createSheet();
HSSFRow row=sheet.createRow(0);
HSSFCell cell=row.createCell((short)0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("序号");
cell=row.createCell((short)1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("商品名称");
cell=row.createCell((short)2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("商品条码");
cell=row.createCell((short)3);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("商品价格");
cell=row.createCell((short)4);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("送货商家");
cell=row.createCell((short)5);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("商家地址");
cell=row.createCell((short)6);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("联系电话");
HSSFSheet sheet=wb.createSheet();
HSSFRow row=sheet.createRow(0);
HSSFCell cell=row.createCell((short)0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("序号");
cell=row.createCell((short)1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("商品名称");
cell=row.createCell((short)2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("商品条码");
cell=row.createCell((short)3);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("商品价格");
cell=row.createCell((short)4);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("送货商家");
cell=row.createCell((short)5);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("商家地址");
cell=row.createCell((short)6);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("联系电话");
好了一个excel文件创建的大部分任务完成现在就是填充文件内容
List<Goods> goods=this.findAll();
for(int i=0;i<goods.size();++i){
Goods g=goods.get(i);
row=sheet.createRow(i+1);
cell=row.createCell((short)0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(i+1);
cell=row.createCell((short)1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(g.getGoodsname());
cell=row.createCell((short)2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(g.getBarcode());
cell=row.createCell((short)3);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(g.getPrice());
cell=row.createCell((short)4);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(g.getGoodsprovides().getProName());
cell=row.createCell((short)4);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(g.getGoodsprovides().getProAddress());
}
for(int i=0;i<goods.size();++i){
Goods g=goods.get(i);
row=sheet.createRow(i+1);
cell=row.createCell((short)0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(i+1);
cell=row.createCell((short)1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(g.getGoodsname());
cell=row.createCell((short)2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(g.getBarcode());
cell=row.createCell((short)3);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(g.getPrice());
cell=row.createCell((short)4);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(g.getGoodsprovides().getProName());
cell=row.createCell((short)4);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(g.getGoodsprovides().getProAddress());
}
最后就是创建IO流,输出就ok
File file=new File("test.xls");
OutputStream out;
InputStream sin = null;
try {
out = new FileOutputStream(file);
wb.write(out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
sin=new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return sin;
OutputStream out;
InputStream sin = null;
try {
out = new FileOutputStream(file);
wb.write(out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
sin=new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return sin;
转载注明出处:诚尚IT
电脑培训基地,