1. 程式人生 > >java使用POI建立Excel工作薄

java使用POI建立Excel工作薄

   最近做的一個專案中涉及到了在程式中生成excel文件的功能,以前也做過。在做的這些專案中使用的方法很多,幾乎每一次 方法都不一樣。最簡單的是直接用jsp把jsp的檔案響應的內容型別給改成application/x-msexcel,這樣來對付那些小而簡單的excel檔案是沒有什麼問題了,並且還方便快捷。

第二種用常用的也是比較流行的當然是Java excel API了,jxl.jar可以幫助我們完成任務。

第三種就是今天用到的POI。POI是Apache jakarta的子專案,使用簡單方便,對中文支援也非常好,功能比較強大。下面是一個利用它建立excel的小例子:(當然,你要首先匯入POI包)

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="org.apache.poi.hssf.usermodel.*,java.io.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'poiExcel.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
 
  <body>
    This is my JSP page. <br>
    <%
   
      //建立新的Excel工作簿
      HSSFWorkbook workbook = new HSSFWorkbook();
     
      //在excel中新建一個工作表,起名字為jsp
      HSSFSheet sheet = workbook.createSheet("JSP");
     
      //建立第一行
      HSSFRow row = sheet.createRow(0);
      //建立第一列
      HSSFCell cell = row.createCell((short)0);
      //定義單元格為字串型別
      cell.setEncoding(HSSFCell.ENCODING_UTF_16);
      //在單元格中輸入一些內容
      cell.setCellValue("作者");
      //建立第二列
      cell = row.createCell((short)1);
      //定義單元格為字串型別
      cell.setEncoding(HSSFCell.ENCODING_UTF_16);
      //在單元格中輸入一些內容
      cell.setCellValue("編輯");
      //檔案存放位置
      String filename = application.getRealPath("/")+"test.xls";
      //新建一輸出流
      FileOutputStream fout = new FileOutputStream(filename);
      //存檔
      workbook.write(fout);
     
      fout.flush();
      //結束關閉
      fout.close();
   
      out.println("excel 檔案生成,存放在<br>"+filename);
     %>
  </body>
</html>
下面是利用poi來讀取excel的小例子:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="org.apache.poi.hssf.usermodel.*,java.io.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'readExcel.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
 
  <body>
    This is my JSP page. <br>
    <%
   
    //讀取excel
    String filename = application.getRealPath("/")+"test.xls";
   
    HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filename));
    //按名引用excel工作表
    HSSFSheet sheet = workbook.getSheet("JSP");
    //也可以用以下方式來獲取excel的工作表,採用工作表的索引值
    //HSSFSheet sheet = workbook.getSheetAt(0);
    HSSFRow row = sheet.getRow(0);
    HSSFCell cell = row.getCell((short)0);
    //列印讀取值
    out.println(cell.getStringCellValue()+"&nbsp;&nbsp;&nbsp;");
    cell = row.getCell((short)1);
    out.println(cell.getStringCellValue()+"&nbsp;&nbsp;&nbsp;");
   
     %>
   
  </body>
</html>