jsp导出excel文件,使用POI和jxl

导读:
   jsp导出excel文件,使用POI和jxl
  使用POI
  <%@ page language="java"contentType="text/html;charset=gb2312"%>
  <%@ page language="java"import ="java.util.*,org.apache.poi.hssf.usermodel.HSSFWorkbook,org.apache.poi.hssf.usermodel.HSSFSheet,org.apache.poi.hssf.usermodel.HSSFRow,org.apache.poi.hssf.usermodel.HSSFCell"%>
   None.gif
  
  <%
<   None.gif
  
  response.setContentType("APPLICATION/OCTET-STREAM");
   None.gif
  
  response.setHeader("Content-Disposition",
   None.gif
  
  "attachment; filename=/"test.xls"+
   None.gif
  
   None.gif
  
  "/"");
   None.gif
  
   None.gif
  
  HSSFWorkbook wb =
  newHSSFWorkbook();
   None.gif
  
  HSSFSheet sheet =wb.createSheet("sheet1");
   None.gif
  
   None.gif
  
  //以下以写表头
   None.gif
  
  //表头为第一行
   None.gif
  
  HSSFRow row =sheet.createRow((short) 0);
   None.gif
  
   None.gif
  
  HSSFCell cell1 =row.createCell((short) 0);
   None.gif
  
  HSSFCell cell2 =row.createCell((short) 1);
   None.gif
  
  HSSFCell cell3 =row.createCell((short) 2);
   None.gif
  
   None.gif
  
  cell1.setEncoding((short) 1);
   None.gif
  
  cell1.setCellType(1);
   None.gif
  
  cell2.setEncoding((short) 1);
   None.gif
  
  cell2.setCellType(1);
   None.gif
  
  cell3.setEncoding((short) 1);
   None.gif
  
  cell3.setCellType(1);
   None.gif
  
   None.gif
  
  //定义表头的内容
   None.gif
  
  cell1.setCellValue("测试");
   None.gif
  
  cell2.setCellValue("测试2");
   None.gif
  
  cell3.setCellValue("测试3");
   None.gif
  
  for(inti =
  0i <
  4i++)
   dot.gif
  {
  //定义数据从第二行开始
  row =sheet.createRow((short) i +
  1);
  cell1 =row.createCell((short) 0);
  cell2 =row.createCell((short) 1);
  cell3 =row.createCell((short) 2);
  cell1.setEncoding((short) 1);
  cell1.setCellType(1);
  cell2.setEncoding((short) 1);
  cell2.setCellType(1);
  cell3.setEncoding((short) 1);
  cell3.setCellType(1);
  //填充内容
  cell1.setCellValue("ggg");
  cell2.setCellValue("00000");
  cell3.setCellValue("adfasdf");
  }
   None.gif
  
  wb.write(response.getOutputStream());
   None.gif
  
  response.getOutputStream().flush();
   None.gif
  
  response.getOutputStream().close();
   None.gif
  
  %>
  POI包下载地址:http://www.apache.org/dyn/closer.cgi/jakarta/poi/
  利用JExcelApi来动态生成excel文档
  首先,请到http://www.andykhan.com/jexcelapi/index.html下载java excel api,主页上同时有比较详细的介绍。
  最新版本为2.4.3,
  同时也可以到:http://www.andykhan.com/jexcelapi/jexcelapi_2_4_3.tar.gz下载到该最新版的API,由于该项目是开源的,所以下载的文件中已经包含了源代码,同样的,文件中也有javadoc,大家在开发中可以参考javadoc。
  下载完毕后,我们需要把文件中的jxl.jar加入到你的开发classpath中。
  下图是现在要生产的excel截图:
   jsp导出excel文件,使用POI和jxl_第1张图片
  
  代码如下:
  File excel = new File("d:/aming.xls");
  if(!excel.exists()){
  excel.createNewFile();
  }
  WritableWorkbook wwb = Workbook.createWorkbook(excel);
  WritableSheet ws = wwb.createSheet("testexcel",0);
  Label lable = null;
  
  //对中文的支持非常好
  lable = new Label(0,0,"我的中国心");
  ws.addCell(lable);
  
  //可以定义模板格式化你的cell
  WritableFont wf = new WritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
  WritableCellFormat wcf = new WritableCellFormat(wf);
  wcf.setBackground(Colour.WHITE);
  lable = new Label(0,1,"fdsl",wcf);
  ws.addCell(lable);
  
  wf = new WritableFont(WritableFont.TIMES,18,WritableFont.BOLD,true);
  wcf = new WritableCellFormat(wf);
  lable = new Label(0,2,"aming",wcf);
  ws.addCell(lable);
  
  //cell的类型同样可以定义为数字类型
  Number nb = new Number(0,3,21.4321321);
  ws.addCell(nb);
  
  //支持格式化你的数字串
  NumberFormat nf = new NumberFormat("#.###");
  wcf = new WritableCellFormat(nf);
  nb = new Number(0,4,21.43254354354354,wcf);
  ws.addCell(nb);
  //cell的类型可以为boolean类型
  Boolean bl = new Boolean(0,5,true);
  ws.addCell(bl);
  //cell的类型同样可以为日期,时间
  DateTime dt = new DateTime(0,6,new Date());
  ws.addCell(dt);
  //并且可以很好格式化你的日期格式
  DateFormat df = new DateFormat("MM dd yyyy hh:mm:ss");
  wcf = new WritableCellFormat(df);
  dt = new DateTime(0,7,new Date(),wcf);
  ws.addCell(dt);
  
  //开始写文件了
  wwb.write();
  wwb.close();
  上面的下载地址无法打开.
  下载请到:http://prdownloads.sourceforge.net/jexcelapi

本文转自
http://www.loohost.com/thread-1311-1-1.html

你可能感兴趣的:(java,excel,jsp,wcf,javadoc,date,api)