最近项目中需要实现Excel的导出功能,于是自己在网上找各种资料最后实现了。在这也记录下方便以后拷贝。
用java实现Excel导入导出目前主要有两种方式,一种是JXL,一种是poi. 前者是一个开源的Java Excel API项目,通过Jxl,我们可以很方便的操作微软的Excel文档。而后者是Apache的一个POI项目,也可以操作Excel,两者相比之下:Jxl使用方便,但功能相对POI比较弱。我是使用jxl实现了导出功能。以下是源码:
public void importToExcel(OutputStream os){
try {
WritableWorkbook workbook = Workbook.createWorkbook(os);
//创建新的一页
WritableSheet sheet = workbook.createSheet("Sheet0",0);
sheet.setColumnView(0, 20);
sheet.setColumnView(1, 20);
sheet.setColumnView(2, 20);
sheet.mergeCells(0, 0, 0, 1); 合并单元格 四个参数 列 、行、 列、 行
sheet.mergeCells(1, 0, 1, 1); 合并单元格 四个参数 列 、行、 列、 行
sheet.mergeCells(2, 0, 2, 1); 合并单元格 四个参数 列 、行、 列、 行
sheet.mergeCells(3, 0, 5, 0); 合并单元格 四个参数 列 、行、 列、 行
sheet.mergeCells(6, 0, 8, 0); 合并单元格 四个参数 列 、行、 列、 行
// 更改标题字体
WritableCell cell = null;
WritableCellFormat wcf = null;
WritableCellFormat wcf2 = null;
Label label = null;
WritableCellFeatures wcfeatures = null;
cell = sheet.getWritableCell(0,0);
WritableFont size12 = new WritableFont(WritableFont.createFont("宋体"),12,WritableFont.BOLD);
wcf = new WritableCellFormat(size12);
wcf.setBackground(Colour.GRAY_25);// 设置单元格的背景颜色
wcf.setAlignment(Alignment.CENTRE);
wcf.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
wcf.setBorder(Border.ALL, BorderLineStyle.THIN); // 添加边框 ; // 设置对齐方式
WritableFont size11 = new WritableFont(WritableFont.createFont("宋体"),11,WritableFont.BOLD);
wcf2 = new WritableCellFormat(size11);
wcf2.setBackground(Colour.GRAY_25);// 设置单元格的背景颜色
wcf2.setAlignment(Alignment.CENTRE); // 设置对齐方式
wcf2.setBorder(Border.ALL, BorderLineStyle.THIN); // 添加边框
WritableCellFormat integerFormat = new WritableCellFormat (NumberFormats.INTEGER);
Label countDate = new Label(0,0,"日期");
sheet.addCell(countDate);
Label edition = new Label(1,0,"版本");
sheet.addCell(edition);
Label oprateSystem = new Label(2,0,"操作系统");
sheet.addCell(oprateSystem);
Label visitCount = new Label(3,0,"浏览量");
sheet.addCell(visitCount);
Label downCount = new Label(6,0,"下载量");
sheet.addCell(downCount);
Label visitNum = new Label(3,1,"次数");
sheet.addCell(visitNum);
Label visitPeople = new Label(4,1,"人数");
sheet.addCell(visitPeople);
Label visitHb = new Label(5,1,"环比");
sheet.addCell(visitHb);
Label downNum = new Label(6,1,"次数");
sheet.addCell(downNum);
Label downPeople = new Label(7,1,"人数");
sheet.addCell(downPeople);
Label downHb = new Label(8,1,"环比");
sheet.addCell(downHb);
cell = sheet.getWritableCell(0,0);
cell.setCellFormat(wcf);
cell = sheet.getWritableCell(1,0);
cell.setCellFormat(wcf);
cell = sheet.getWritableCell(2,0);
cell.setCellFormat(wcf);
cell = sheet.getWritableCell(3,0);
cell.setCellFormat(wcf);
cell = sheet.getWritableCell(6,0);
cell.setCellFormat(wcf);
cell = sheet.getWritableCell(3,1);
cell.setCellFormat(wcf2);
cell = sheet.getWritableCell(4,1);
cell.setCellFormat(wcf2);
cell = sheet.getWritableCell(5,1);
cell.setCellFormat(wcf2);
cell = sheet.getWritableCell(6,1);
cell.setCellFormat(wcf2);
cell = sheet.getWritableCell(7,1);
cell.setCellFormat(wcf2);
cell = sheet.getWritableCell(8,1);
cell.setCellFormat(wcf2);
List<CountModel> countResult = countDaoImpl.searchResult();
if (countResult!=null && countResult.size()>0) {
for (int i = 0; i < countResult.size(); i++) {
CountModel countModel = countResult.get(i);
Label label2 = new Label(0, i+2, countModel.getCountDate());
sheet.addCell(label2);
Label label3 = new Label(1, i+2, countModel.getEdition());
sheet.addCell(label3);
Label label4 = new Label(2, i+2, countModel.getOs());
sheet.addCell(label4);
Label label5 = new Label(3, i+2, countModel.getVisitTimes().toString(),integerFormat);
sheet.addCell(label5);
Label label6 = new Label(4, i+2, countModel.getVisitPeople().toString(),integerFormat);
sheet.addCell(label6);
Label label7 = new Label(5, i+2, countModel.getVisitHBee());
sheet.addCell(label7);
Label label8 = new Label(6, i+2, countModel.getDownloadTimes().toString(),integerFormat);
sheet.addCell(label8);
Label label9 = new Label(7, i+2, countModel.getDownloadPeople().toString(),integerFormat);
sheet.addCell(label9);
Label label10 = new Label(8, i+2, countModel.getDownHbee());
sheet.addCell(label10);
}
}
//把创建的内容写入到输出流中,并关闭输出流
workbook.write();
workbook.close();
os.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
下面是输出结果:
简单总结下:操作单元格的时候,一般都是先创建出一个wf(个人理解就相当于一个样式,里面我们可以定义单元格的背景颜色啊,内容的字体大小颜色边框等等。)然后在new lable的时候将这个当一个参数传递进去像这样(
Label label8 = new Label(6, i+2, countModel.getDownloadTimes().toString(),integerFormat);
),或者通过坐标拿到那个单元格,然后把这个样式set进去。像这样:
cell = sheet.getWritableCell(0,0);
cell.setCellFormat(wcf);
设置列宽用:
sheet.setColumnView(0, 20); //0代表第一列
合并单元格用
sheet.mergeCells(0, 0, 0, 1); //四个参数分别代表 起始列,起始行,终止列,终止行
九层之台,始于累土
用心 点滴 积累