jsp页面显示数据导出到excel表中


Excel报表的方法,一个过於简单,一个只能用於window平台(因为使用jdbc-odbc bridge),且无法使用到Excel内部的各种公式或是方法,因此,今天介绍一个apache出的元件叫POI,它可以在UNIX或window平台处理word或Excel档案,而不需要依靠window的com,

并且可设定储存格格式、列印格式等等;今天我来介绍其中有关资料读取、新增、修改及删除的功能,若各位网友研究好其他的功能,麻烦Email给我(ljj@mlc.edu.tw),分享给大家!
一、需要用的档案:jakarta-poi-1.8.0-dev-20020917.jar
 几乎每天都有1.8.0的最新版(但非正式版),正式的版本是1.5.0
 http://jakarta.apache.org/builds/jakarta-poi/nightly/
 将档案复制到classpath所指到的地方
二、有兴趣的朋友可以参考
  http://jakarta.apache.org/poi/
三、先建立一个叫做book1.xls的Excel档,内容如下
----------------------------------
项目  单价  数量   合计
CPU   7000  5    35000
硬碟  2500  2    5000
记忆体 1600  3    4800
----------------------------------
其中合计的栏位是设定公式,单价*数量
 
四、资料读取范例
<%@ page contentType="text/html;charset=MS950" import="java.util.*,java.io.*" %>
<%@ page import="org.apache.poi.poifs.filesystem.*,org.apache.poi.hssf.usermodel.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=MS950">
<title>读取Excel档案</title>
</head>
<body>
<table border="1" width="100%">
<%
  FileInputStream finput = new FileInputStream(application.getRealPath("/")+"book1.xls" );
  //设定FileINputStream读取Excel档
  POIFSFileSystem fs = new POIFSFileSystem( finput );
  HSSFWorkbook wb = new HSSFWorkbook(fs);
  HSSFSheet sheet = wb.getSheetAt(0);
  //读取第一个工作表,宣告其为sheet
  finput.close();
  HSSFRow row=null;
  //宣告一列
  HSSFCell cell=null;
  //宣告一个储存格
  short i=0;
  short y=0;
  //以巢状回圈读取所有储存格资料
  for (i=0;i<=sheet.getLastRowNum();i++)
  {
    out.println("<tr>");
    row=sheet.getRow(i);
    for (y=0;y<row.getLastCellNum();y++)
    {
       cell=row.getCell(y);
       out.print("<td>");
      
       //判断储存格的格式
       switch ( cell.getCellType() )
       {
           case HSSFCell.CELL_TYPE_NUMERIC:
               out.print(cell.getNumericCellValue());
               //getNumericCellValue()会回传double值,若不希望出现小数点,请自行转型为int
               break;
           case HSSFCell.CELL_TYPE_STRING:
               out.print( cell.getStringCellValue());
               break;
           case HSSFCell.CELL_TYPE_FORMULA:
               out.print(cell.getNumericCellValue());
               //读出公式储存格计算後的值
               //若要读出公式内容,可用cell.getCellFormula()
               break;
           default:
               out.print( "不明的格式");
               break;
       }
       out.println("</td>");
    }
    out.println("</tr>");
  }
%>
</table>
</body>
</html>
 
五、资料新增范例
<%@ page contentType="text/html;charset=MS950" import="java.util.*,java.io.*" %>
<%@ page import="org.apache.poi.poifs.filesystem.*,org.apache.poi.hssf.usermodel.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=MS950">
<title>插入资料至Excel档案</title>
</head>
<body>
<%
  FileInputStream finput = new FileInputStream(application.getRealPath("/")+"book1.xls" );
  //设定FileINputStream读取Excel档
  POIFSFileSystem fs = new POIFSFileSystem( finput );
  HSSFWorkbook wb = new HSSFWorkbook(fs);
  HSSFSheet sheet = wb.getSheetAt(0);
  //读取第一个工作表,宣告其为sheet
  finput.close();
  HSSFRow row=null;
  //宣告一列
  HSSFCell cell=null;
  //宣告一个储存格
  short i=4;
  row=sheet.createRow(i);
  //建立一个新的列,注意是第五列(列及储存格都是从0起算)
  cell=row.createCell((short)0);
  cell.setEncoding(HSSFCell.ENCODING_UTF_16);
  //设定这个储存格的字串要储存双位元
  cell.setCellValue("显示卡");
  cell=row.createCell((short)1);
  cell.setCellValue(1700);
  cell=row.createCell((short)2);
  cell.setCellValue(8);
  cell=row.createCell((short)3);
  //设定这个储存格为公式储存格,并输入公式
  cell.setCellFormula("B"+(i+1)+"*C"+(i+1));
  try
  {
    FileOutputStream fout=new FileOutputStream(application.getRealPath("/")+"book1.xls");
    wb.write(fout);
    //储存
    fout.close();
    out.println("储存成功<a href='book1.xls'>book1.xls</a>");
  }
  catch(IOException e)
  {
    out.println("产生错误,错误讯息:"+e.toString());
  }
%>
</body>
</html>
 
六、资料删除、修改范例
<%@ page contentType="text/html;charset=MS950" import="java.util.*,java.io.*" %>
<%@ page import="org.apache.poi.poifs.filesystem.*,org.apache.poi.hssf.usermodel.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=MS950">
<title>删除、修改资料至Excel档案</title>
</head>
<body>
<%
  FileInputStream finput = new FileInputStream(application.getRealPath("/")+"book1.xls" );
  //设定FileINputStream读取Excel档
  POIFSFileSystem fs = new POIFSFileSystem( finput );
  HSSFWorkbook wb = new HSSFWorkbook(fs);
  HSSFSheet sheet = wb.getSheetAt(0);
  //读取第一个工作表,宣告其为sheet
  finput.close();
  HSSFRow row=null;
  //宣告一列
  HSSFCell cell=null;
  //宣告一个储存格
  row=sheet.getRow((short)4);
  //取出第五列
  if (row!=null)
     sheet.removeRow(row);
  //先侦测第五列存不存在,若在的话将第五列删除
  row=sheet.getRow((short)3);
  //取出第四列
  cell=row.getCell((short)2);
  //取出第三个储存格
  cell.setCellValue(7);
  //设定该储存格值为7
  cell=row.getCell((short)3);
  cell.setCellFormula(cell.getCellFormula());
  //上两行为取出公式储存格,并重新计算(因为刚才更新过计算公式的值)
  //如果不做,公式计算後的值不会更新
  try
  {
    FileOutputStream fout=new FileOutputStream(application.getRealPath("/")+"book1.xls");
    wb.write(fout);
    //储存
    fout.close();
    out.println("储存成功<a href='book1.xls'>book1.xls</a>");
  }
  catch(IOException e)
  {
    out.println("产生错误,错误讯息:"+e.toString());
  }
%>
</body>
</html>

本文作者:
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3