您的位置:首页 > 其它

利用poi.jar对excel进行读取、修改和删除

2016-07-07 11:27 585 查看
1、目前POI的最新发布版本是3.10_FINAL.该版本保护的jar包有:

Maven artifactIdPrerequisitesJAR
poicommons-logging, commons-codec, log4jpoi-version-yyyymmdd.jar
poi-scratchpadpoipoi-scratchpad-version-yyyymmdd.jar
poi-ooxmlpoi, poi-ooxml-schemaspoi-ooxml-version-yyyymmdd.jar
poi-ooxml-schemasxmlbeanspoi-ooxml-schemas-version-yyyymmdd.jar
poi-examplespoi, poi-scratchpad, poi-ooxmlpoi-examples-version-yyyymmdd.jar
ooxml-schemasxmlbeansooxml-schemas-1.1.jar
HSSF - 提供读写Microsoft Excel XLS格式档案的功能。

XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能。

HWPF - 提供读写Microsoft Word DOC格式档案的功能。

HSLF - 提供读写Microsoft PowerPoint格式档案的功能。

HDGF - 提供读Microsoft Visio格式档案的功能。

HPBF - 提供读Microsoft Publisher格式档案的功能。

HSMF - 提供读Microsoft Outlook格式档案的功能。

2、读取excel

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.util.*,java.io.*" %>
<%@ page import="org.apache.poi.poifs.filesystem.*,org.apache.poi.hssf.usermodel.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>读取Excel档案</title>
</head>
<body>
<center>
<h2>Jsp读取excel中的数据</h2>
<table border="1" width="100%">
<%
//设定FileInputStream读取Excel中的数据
FileInputStream finput=new FileInputStream(application.getRealPath("/")+"book1.xls");
POIFSFileSystem fs=new POIFSFileSystem(finput);
HSSFWorkbook wb=new HSSFWorkbook(fs);
//读取第一个工作表,其为sheet
HSSFSheet sheet=wb.getSheetAt(0);
wb.close();
finput.close();
//声明一列
HSSFRow row=null;
//声明一个存储格
HSSFCell cell=null;
short i=0;
short y=0;
//读取所有存储格资料
for(i=0;i<=sheet.getLastRowNum();i++)
{
out.println("<tr>");
row=sheet.getRow(i);
for(y=0;y<row.getLastCellNum();y++)
{
cell=row.getCell(y);
out.println("<td>");
//判断存储格的格式
switch(cell.getCellType())
{
case HSSFCell.CELL_TYPE_NUMERIC:
out.println(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_STRING:
out.println(cell.getStringCellValue());
break;
case HSSFCell.CELL_TYPE_FORMULA:
out.println(cell.getNumericCellValue());
break;
default:
out.println("不明格式");
break;
}
}
out.println("</td>");
}
out.println("</tr>");
%>
</table>
</center>
</body>
</html>
3、添加excel

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.util.*,java.io.*" %>
<%@ page import="org.apache.poi.poifs.filesystem.*,org.apache.poi.hssf.usermodel.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>插入资料至Excel档案</title>
</head>
<body>
<center>
<h2>向Excel中添加记录</h2>
<%
//设定FileInputStream读取Excel中的数据
FileInputStream finput=new FileInputStream("D:\\Jee\\dainchiimage\\book1.xls");
POIFSFileSystem fs=new POIFSFileSystem(finput);
HSSFWorkbook wb=new HSSFWorkbook(fs);
//读取第一个工作表,其为sheet
HSSFSheet sheet=wb.getSheetAt(0);
finput.close();
//声明一列
HSSFRow row=null;
//声明一个存储格
HSSFCell cell=null;
short i=4;
//建立一个新的列,注意是第五列(列及存储格都是从0开始)
row=sheet.createRow(i);
cell=row.createCell((short)0);
cell.setCellValue("UML");
cell=row.createCell((short)1);
cell.setCellValue("40");
cell=row.createCell((short)2);
cell.setCellValue("3");
cell=row.createCell((short)3);
//设定这个存储格为公式存储格,并输入公式
cell.setCellFormula("B"+(i+1)+"*C"+(i+1));
try{
FileOutputStream fout=new FileOutputStream(application.getRealPath("/")+"book1.xls");
wb.write(fout);
fout.close();
wb.close();
out.println("存储成功<a href='book1.xls'>book1.xls</a>");
}catch(IOException e)
{
out.println("产生错误,错误信息:"+e.toString());
}
%>
</center>
</body>
</html>


4、删除excel

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.util.*,java.io.*" %>
<%@ page import="org.apache.poi.poifs.filesystem.*,org.apache.poi.hssf.usermodel.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>删除资料至Excel档案</title>
</head>
<body>
<center>
<h2>删除Excel表中的数据</h2>
<%
//设定FileInputStream读取Excel中的数据
FileInputStream finput=new FileInputStream(application.getRealPath("/")+"book1.xls");
POIFSFileSystem fs=new POIFSFileSystem(finput);
HSSFWorkbook wb=new HSSFWorkbook(fs);
//读取第一个工作表,其为sheet
HSSFSheet sheet=wb.getSheetAt(0);
finput.close();
//声明一列
HSSFRow row=null;
//声明一个存储格
HSSFCell cell=null;
//取出第3列
row=sheet.getRow((short)2);
if(row!=null)
sheet.removeRow(row);
try{
FileOutputStream fout=new FileOutputStream(application.getRealPath("/")+"book1.xls");
wb.write(fout);
fout.close();
out.println("删除成功<a href='book1.xls'>book1.xls</a>");
}catch(IOException e)
{
out.println("产生错误,错误信息:"+e.toString());
}
%>
</center>
</body>
</html>
4、poi包和程序源代码在我的github中,地址:https://github.com/San-Shui/Excel
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: