最近做的一个项目中涉及到了在程序中生成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()+" ");
cell = row.getCell((short)1);
out.println(cell.getStringCellValue()+" ");
%>
</body>
</html>