jsp頁面顯示資料匯出到excel表中 __js

來源:互聯網
上載者:User
  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>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.