当前位置:文档之家› poi读取excel并输出到jsp页面

poi读取excel并输出到jsp页面

代码来源于网络,只做了部分修改,我也是刚用到很多都不懂,找了一些例子之类的自己用,有些不错的分享给大家,仅供参考。

下面这个是用servlet做(注意web.xml );其实原理一样;源码如下readExcelServlet.javapackage com.test.servlet;import java.io.FileInputStream;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import ermodel.HSSFCell;import ermodel.HSSFDateUtil;import ermodel.HSSFRow;import ermodel.HSSFSheet;import ermodel.HSSFWorkbook;import org.apache.poi.poifs.filesystem.POIFSFileSystem;/*** author:wx * describe:Reads the Excel example **/public class readExcelServlet extends HttpServlet {/****/private static final long serialVersionUID = 1L;/*** * Constructor of the object.*/public readExcelServlet() {super();}/*** Destruction of the servlet. <br>**/public void destroy() {super.destroy();}// Just puts "destroy" string in log// Put your code here/*** doGet*/public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}/*** doPost*/public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=gb2312");PrintWriter out = response.getWriter();String filePath = new String(request.getParameter("file").getBytes("GBK"), "gb2312");//注意如果这里出现乱码导致文件路径错误试着改下GBK,换成ISO-8859-1out.print("文件路径:" + filePath + "<br>");try {POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filePath));// 创建工作簿HSSFWorkbook workBook = new HSSFWorkbook(fs);/*** 获得Excel中工作表个数*/out.println("工作表个数 :" + workBook.getNumberOfSheets() + "<br>");for (int i = 0; i < workBook.getNumberOfSheets(); i++) {out.println("<font color='red'> " + i+ " ***************工作表名称:"+ workBook.getSheetName(i)+ " ************</font><br>");// 创建工作表HSSFSheet sheet = workBook.getSheetAt(i);int rows = sheet.getPhysicalNumberOfRows(); // 获得行数if (rows > 0) {sheet.getMargin(HSSFSheet.TopMargin);for (int j = 0; j < rows; j++) { // 行循环HSSFRow row = sheet.getRow(j);if (row != null) {int cells = row.getLastCellNum();// 获得列数for (short k = 0; k < cells; k++) { // 列循环HSSFCell cell = row.getCell(k);if (cell != null) {String value = "";switch (cell.getCellType()) {case HSSFCell.CELL_TYPE_NUMERIC:if (HSSFDateUtil.isCellDateFormatted(cell)) {// 如果是date类型则,获取该cell的date值value = HSSFDateUtil.getJavaDate(cell.getNumericCellValue()).toString();out.println("第"+ j + "行,第" + k+ "列值:" + value + "<br>");} else {// 纯数字value = String.valueOf(cell.getNumericCellValue());out.println("第"+ j + "行,第" + k+ "列值:" + value + "<br>");}break;/* 此行表示单元格的内容为string类型*/case HSSFCell.CELL_TYPE_STRING: // 字符串型value = cell.getRichStringCellValue().toString();out.println("第" + j + "行,第" + k + "列值:"+ value + "<br>");break;caseHSSFCell.CELL_TYPE_FORMULA:// 公式型// 读公式计算值value = String.valueOf(cell .getNumericCellValue());if(value.equals("NaN")) {// 如果获取的数据值为非法值,则转换为获取字符串//value = cell .getRichStringCellValue().toString();}// cell.getCellFormula();读公式out.println("第" + j + "行,第" + k + "列值:"+ value + "<br>");break;caseHSSFCell.CELL_TYPE_BOOLEAN:// 布尔value = " "+cell.getBooleanCellValue();out.println("第" + j + "行,第" + k + "列值:"+ value + "<br>");break;/* 此行表示该单元格值为空 */case HSSFCell.CELL_TYPE_BLANK: // 空值value = "";out.println("第" + j + "行,第" + k + "列值:"+ value + "<br>");break;case HSSFCell.CELL_TYPE_ERROR: // 故障value = "";out.println("第" + j + "行,第" + k + "列值:"+ value + "<br>");break;default:value = cell.getRichStringCellValue().toString();out.println("第" + j + "行,第" + k + "列值:"+ value + "<br>");}}}}}}}} catch (Exception ex) {ex.printStackTrace();}out.print("<script>alert('解析完毕');</script>");out.flush();out.close();}public void init() throws ServletException {// Put your code here}}下面是jsp页面;readExcel.jsp<%@page language="java"import="java.util.*"pageEncoding="gb2312"%> <%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><form action="readExcelServlet"method="post">上传附件:<input type="file"name="file"><br><input type="submit"value="开始解析"></form><br><br><br><form action="ToExcel"><input type="submit"value="导出Excel"></form></body></html>。

相关主题