所需jar文件为 jxl.jar 可以在此获取: http://www.uushare.com/user/sudyguo/files/2209594
这种方法导入excel效率较高,可批量导出; 更多更详细的用法,可以看: http://pcedu.pconline.com.cn/empolder/gj/java/0608/853669_1.html
实例:
此段代码用于生成excel文件
*/
WritableWorkbook book= Workbook.createWorkbook(new File(application.getRealPath("/")+"price.xls"));
//生成名为"第一页"的工作表,参数0表示这是第一页
WritableSheet sheet=book.createSheet("第一页",0);
//在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
WritableFont font1= new WritableFont(WritableFont.TIMES,14,WritableFont.BOLD);
WritableCellFormat format1=new WritableCellFormat(font1);
format1.setAlignment(jxl.format.Alignment.CENTRE);//设置为居中
sheet.setColumnView(1,20);//设置第1列宽度,6cm左右
sheet.mergeCells(0,0,7,0);
Label label=new Label(0,0,"统计报表",format1);
//将定义好的单元格添加到工作表中
sheet.addCell(label);
//将定义好的单元格添加到工作表中
WritableFont font2=new WritableFont(WritableFont.createFont("楷体_GB2312"),10,WritableFont.NO_BOLD );
WritableCellFormat format2=new WritableCellFormat(font2);
format2.setAlignment(jxl.format.Alignment.CENTRE);//设置为居中
//设置边框
format2.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.NONE);
label=new Label(0,2,"编号",format2);
sheet.addCell(label);
label=new Label(1,2,"采集日期",format2);
sheet.addCell(label);
label=new Label(2,2,"采集点",format2);
sheet.addCell(label);
label=new Label(3,2,"品种",format2);
sheet.addCell(label);
label=new Label(4,2,"种类",format2);
sheet.addCell(label);
label=new Label(5,2,"品种说明",format2);
sheet.addCell(label);
label=new Label(6,2,"最高价",format2);
sheet.addCell(label);
label=new Label(7,2,"最低价",format2);
sheet.addCell(label);
label=new Label(8,2,"平均价",format2);
sheet.addCell(label);
label=new Label(9,2,"状态",format2);
sheet.addCell(label);