利用poi.jar对excel进行读取、修改和删除
2016-07-07 11:27
585 查看
1、目前POI的最新发布版本是3.10_FINAL.该版本保护的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
4、删除excel
Maven artifactId | Prerequisites | JAR |
---|---|---|
poi | commons-logging, commons-codec, log4j | poi-version-yyyymmdd.jar |
poi-scratchpad | poi | poi-scratchpad-version-yyyymmdd.jar |
poi-ooxml | poi, poi-ooxml-schemas | poi-ooxml-version-yyyymmdd.jar |
poi-ooxml-schemas | xmlbeans | poi-ooxml-schemas-version-yyyymmdd.jar |
poi-examples | poi, poi-scratchpad, poi-ooxml | poi-examples-version-yyyymmdd.jar |
ooxml-schemas | xmlbeans | ooxml-schemas-1.1.jar |
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
相关文章推荐
- 虚拟机改mac地址,一定要关机才能生效呀,惨痛教训
- Swift-函数
- ARM、X86/Atom、MIPS、PowerPC
- Citrix结合CKEY实现双因子认证
- 巧用Ajax的beforeSend 提高用户体验
- jpg转换成PDF 如何将多张图片合成一个PDF文件
- Block的简单识记
- Xposed开发入门笔记
- String、StringBuffer、StringBuilder
- json中文编码问题
- Shell 数组的定义和使用
- Entity Framework Tutorial Basics(39):Raw SQL Query
- 通过分区(Partitioning)提高Spark的运行性能
- POJ2251
- 图算法总结
- 淘宝上拉查看图文详情效果
- Sql Server合并多行询数据到一行:使用自连接、FOR XML PATH('')、STUFF或REPLACE函数
- html
- yii2.0的mvc中的model
- 登录phpmyadmin提示: #1045 无法登录 MySQL 服务器