POI读取Excel

package 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 org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**author:wx
* describe:Reads the Excel example
* */
public class ReadExcel extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Constructor of the object.
*/
public ReadExcel() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
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(
"ISO-8859-1"), "gb2312");

out.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;
case HSSFCell.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;
case HSSFCell.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();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
下面是前台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 'index.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>
<form action="ReadExcel" 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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值