利用POI读取Excel数据,实现同时读取多个Excel文件,兼容所有浏览器
来源:互联网 发布:杰亚伯拉罕知乎 编辑:程序博客网 时间:2024/05/21 22:53
POI读取多个Excel数据
1、利用POI读取Excel数据,实现同时读取多个Excel文件,兼容所有浏览器。
2、实现过程: 客户端上传多个Excel文件到服务器,服务器读取数据后返回到客户端。
3、亮点:实现多个Excel文件上传,并判断文件类型是否正确和文件是否选择、servlet+poi+jsp、兼容所有浏览器、POI读取Excel技术列子简单易懂
4、理论东东就不多说了,直接看代码,代码直接复制就可以用了。 技术QQ群: 180258862
5、servlet代码:
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class XlsImport extends HttpServlet {
public XlsImport() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
try{
Iterator<FileItem> it = getUPFiles(request) ;
while (it.hasNext()) {
FileItem item = it.next();
if(item!=null && !item.isFormField() && item.getSize()>0){
InputStream f = item.getInputStream();
out.println(importExcle(f));
}
}
out.flush();
out.close();
}catch (Exception e) {
e.printStackTrace();
}
}
public void init() throws ServletException {
// Put your code here
}
public String importExcle(InputStream in) {
StringBuffer buf = new StringBuffer();
try {
int beginRowIndex = 0;// 从excel 中开始读取的起始行数
int totalRows = 0;// 从excel 表的总行数
// 根据文件的输入流,创建对Excel 工作薄文件的引用
HSSFWorkbook workbook = new HSSFWorkbook(in);
// 默认exce的书页是“sheet1”
HSSFSheet sheet = workbook.getSheetAt(0);
// 得到该excel 表的总行数
totalRows = sheet.getLastRowNum();
System.out.println("xls总行数"+sheet.getLastRowNum());
// 循环读取excel表格的每行记录,并逐行进行保存
for (int i = beginRowIndex; i <= totalRows; i++) {
HSSFRow row = sheet.getRow(i);
// 获取一行每列的数据
HSSFCell num0 = row.getCell((short) 0);
HSSFCell num1 = row.getCell((short) 1);
HSSFCell num2 = row.getCell((short) 2);
HSSFCell num3 = row.getCell((short) 3);
// 将数据赋给相关的变量
String str0 = num0.getRichStringCellValue().getString().trim();
String str1 = num1.getRichStringCellValue().getString().trim();
String str2 = num2.getRichStringCellValue().getString().trim();
String str3 = num3.getRichStringCellValue().getString().trim();
System.out.println(str0+"-->"+str1+"-->"+str2+"-->"+str3);
buf.append(str0+"-->"+str1+"-->"+str2+"-->"+str3);
}
return buf.toString();
} catch (Exception e) {
e.printStackTrace();
return e.fillInStackTrace().toString();
} finally {
try {
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 获取上传的文件集合
* @param request
* @return
* @throws FileUploadException
*/
public static Iterator<FileItem> getUPFiles(HttpServletRequest request) throws FileUploadException{
DiskFileItemFactory fac = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(fac);
return upload.parseRequest(request).iterator();
}
}
6、jsp代码|:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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>Excel技术:2013年2月26日</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="Excel技术:2013年2月26日">
<meta http-equiv="description" content="Excel技术:2013年2月26日">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<script type="text/javascript" src="<%=basePath%>js/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
function getXlsFile(){
$("#btn_import").attr('disabled',true);
$("#btn_import").val("正在执行......");
var xls = $("input[name='xlsfile']").val();
var isfile = 0;
var count = 0;
$("input[name='xlsfile']").each(function(){
//判断文件类型是否正确
if($(this).val()=="" || ($(this).val().lastIndexOf(".xls")>-1 ||$(this).val().lastIndexOf(".xlsx")>-1)) {
} else{
isfile=1;
}
//是否至少存在一个文件
if($(this).val()!="" && count==0){
count++;
}
});
if(isfile==0 && count!=0 ){
$("#form1").submit();
}else if(count==0){
alert("至少要选择一个文件");
}
else{
alert("文件格式不正确,请选择xls、xlsx格式文件.");
}
$("#btn_import").attr('disabled',false);
$("#btn_import").val("确认导入");
}
</script>
<body>
<form action="<%=basePath%>poi/xlsimport" method="post" id="form1" enctype="multipart/form-data">
<pre>
<input type="file" id="xlsfile" name="xlsfile" class="xls" />
<input type="file" id="xlsfiles" name="xlsfile" class="xls" />
<input type="button" value="确认导入" id="btn_import" onclick="getXlsFile()"/>
</pre></form>
</body>
</html>
- 利用POI读取Excel数据,实现同时读取多个Excel文件,兼容所有浏览器
- 利用POI读取Excel文件
- 利用POI读取excel
- 利用poi读取excel
- java利用poi读取excel中数据
- POI读取Excel数据
- POI 读取excel数据
- JAVA利用Apache Poi读取Excel文件
- poi 读取excel 文件
- poi读取excel文件
- POI读取excel文件
- poi读取excel文件
- java利用poi读取excel
- java利用poi读取excel
- java利用poi读取excel
- EXCEL:读取多个EXCEL文件数据到WORD文档
- struts:poi读取excel文件(兼容2003、2007)
- java使用POI读取excel文件,兼容xls和xlsx
- OpenGL两种投影方式
- Web 开发最有用的50款 jQuery 插件集锦——《内容滑块篇》
- Android之使用传感器获取相应数据
- oracle完整事务控制-包实现
- 《Windows游戏编程大师技巧》第七章 Deom 13
- 利用POI读取Excel数据,实现同时读取多个Excel文件,兼容所有浏览器
- 自己开发的web快速开发平台教程(4),树形结构(Tree)的使用教程
- 社区网站系统 jsGen
- 黑马程序员——C#学习日记之控制台输出
- msql优化
- 识别字符串中的整数并转换为数字形式
- JQueryEasyUI学习笔记(七)datagrid
- unload data tools--ociuldr3
- NYOJ 286 动物统计