【JAVA技术】导出为excel代码

  

try{

     OutputStream os = response.getOutputStream();

     response.reset();// 清空输出流

     String filename = "查询结果.xls"; //文件名

     response.setCharacterEncoding("utf-8");

   response.setHeader("Content-disposition","attachment;filename=\""
     + new String(name.getBytes("gb2312"),"ISO8859-1") + "\"");// 设定输出文件头 

     response.setContentType("application/msexcel");// 定义输出类型

     WritableWorkbook wwb= Workbook.createWorkbook(os);//建立excel文件

     WritableFont font1= new WritableFont(WritableFont.TIMES,12,WritableFont.BOLD);

     WritableCellFormat format1=new WritableCellFormat(font1);

     format1.setAlignment(jxl.format.Alignment.CENTRE);

     format1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);

     WritableSheet ws= wwb.createSheet("查询结果",10); //创建表单

ws.addCell(new jxl.write.Label(j, i, "内容",format1));//往第i行、第j列添加内容为“内容”,格式为format1

     ws.setRowView(0,300);//指定行的宽度,可省

     ws.setColumnView(1,25);//指定列的宽度,可省

     ws.mergeCells(0,0,9,0); //合并单元格00列到0行第9

     ws.addCell(new jxl.write.Label(0, 0, "查询结果",format1)); //插入标题

     wwb.write();   //将数据写入所见的excel

     wwb.close();

     os.close(); // 关闭流

}catch (Exception e) {

     e.printStackTrace();

}

 

本文出自 “HAPPY_CANDY” 博客,谢绝转载!

你可能感兴趣的:(Excel)