Apache POI 读写 E…
来源:互联网 发布:mrt数据导入导出怎么做 编辑:程序博客网 时间:2024/06/05 17:55
Apache POI 是 Apache 软件基金会的开放源码函式库,POI 提供 API 给 Java 程序对 MicrosoftOffice 格式档案读和写的功能。
这里演示了 POI 对新版 Excel (.xlsx) 和 旧版 Excel (.xls) 两种格式文件的读写操作。
POI下载处
需要导入的jar
xmlbeans-2.6.0.jar (POI完整包内的
curvesapi-1.03.jar
poi-3.14-20160307.jar
poi-excelant-3.14-20160307.jar
poi-ooxml-3.14-20160307.jar
poi-ooxml-schemas-3.14-20160307.jar
poi-scratchpad-3.14-20160307.jar
[java] view plain copy
- import
java.io.FileInputStream; - import
java.io.FileOutputStream; - import
java.io.IOException; - import
java.io.InputStream; - import
java.util.Iterator; -
- 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; -
- import
org.apache.poi.xssf.usermodel.XSSFCell; - import
org.apache.poi.xssf.usermodel.XSSFRow; - import
org.apache.poi.xssf.usermodel.XSSFSheet; - import
org.apache.poi.xssf.usermodel.XSSFWorkbook; -
- public
class ReadWriteExcelFile - {
-
-
public static void readXLSFile() throwsIOException -
{ -
InputStream ExcelFileToRead = new FileInputStream( "C:/Test.xls"); -
HSSFWorkbook wb = new HSSFWorkbook(ExcelFileToRead); -
-
HSSFSheet sheet = wb.getSheetAt(0); -
HSSFRow row; -
HSSFCell cell; -
-
Iterator rows = sheet.rowIterator(); -
-
while (rows.hasNext()) -
{ -
row = (HSSFRow) rows.next(); -
Iterator cells = row.cellIterator(); -
-
while (cells.hasNext()) -
{ -
cell = (HSSFCell) cells.next(); -
-
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) -
{ -
System.out.print(cell.getStringCellValue() + " " ); -
} -
else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) -
{ -
System.out.print(cell.getNumericCellValue() + " " ); -
} -
else -
{ -
// U Can Handel Boolean, Formula, Errors -
} -
} -
System.out.println(); -
} -
-
} -
-
public static void writeXLSFile() throwsIOException -
{ -
-
String excelFileName = "C:/Test.xls";// name of excel file -
-
String sheetName = "Sheet1";// name of sheet -
-
HSSFWorkbook wb = new HSSFWorkbook(); -
HSSFSheet sheet = wb.createSheet(sheetName); -
-
// iterating r number of rows -
for ( intr 0;= r 5;< r++) -
{ -
HSSFRow row = sheet.createRow(r); -
-
// iterating c number of columns -
for ( intc 0;= c 5;< c++) -
{ -
HSSFCell cell = row.createCell(c); -
-
cell.setCellValue("Cell " + "r + " + c); -
} -
} -
-
FileOutputStream fileOut = new FileOutputStream(excelFileName); -
-
// write this workbook to an Outputstream. -
wb.write(fileOut); -
fileOut.flush(); -
fileOut.close(); -
} -
-
public static void readXLSXFile() throwsIOException -
{ -
InputStream ExcelFileToRead = new FileInputStream( "C:/Test.xlsx"); -
XSSFWorkbook wb = new XSSFWorkbook(ExcelFileToRead); -
-
XSSFWorkbook test = new XSSFWorkbook(); -
-
XSSFSheet sheet = wb.getSheetAt(0); -
XSSFRow row; -
XSSFCell cell; -
-
Iterator rows = sheet.rowIterator(); -
-
while (rows.hasNext()) -
{ -
row = (XSSFRow) rows.next(); -
Iterator cells = row.cellIterator(); -
while (cells.hasNext()) -
{ -
cell = (XSSFCell) cells.next(); -
-
if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) -
{ -
System.out.print(cell.getStringCellValue() + " " ); -
} -
else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) -
{ -
System.out.print(cell.getNumericCellValue() + " " ); -
} -
else -
{ -
// U Can Handel Boolean, Formula, Errors -
} -
} -
System.out.println(); -
} -
-
} -
-
public static void writeXLSXFile() throwsIOException -
{ -
-
String excelFileName = "C:/Test.xlsx";// name of excel file -
-
String sheetName = "Sheet1";// name of sheet -
-
XSSFWorkbook wb = new XSSFWorkbook(); -
XSSFSheet sheet = wb.createSheet(sheetName); -
-
// iterating r number of rows -
for ( intr 0;= r 5;< r++) -
{ -
XSSFRow row = sheet.createRow(r); -
-
// iterating c number of columns -
for ( intc 0;= c 5;< c++) -
{ -
XSSFCell cell = row.createCell(c); -
-
cell.setCellValue("Cell " + "r + " + c); -
} -
} -
-
FileOutputStream fileOut = new FileOutputStream(excelFileName); -
-
// write this workbook to an Outputstream. -
wb.write(fileOut); -
fileOut.flush(); -
fileOut.close(); -
} -
-
public static void main(String[] throwsargs) IOException -
{ -
writeXLSFile(); -
readXLSFile(); -
-
writeXLSXFile(); -
readXLSXFile(); -
} - }
POI 所支持的文档格式
ooxml-schemas
阅读全文
0 0
- Apache POI 读写 E…
- Prostitute boycotts Chinese on e…
- Java eclipse: e
- HDOJ 1847 Good Luck in CET-4 E…
- 【原创】 linux-mysql E…
- Oracle E-Business Suit…
- Oracle E-Business Suit…
- 2FSK:Error: Unexpected MATLAB e…
- HTTP error 500 Internal server e…
- HDOJ 1012 u Calculate e
- 集线器 交换机 路由器 网桥 …
- Windows apache 反向代…
- Apache 虚拟主机 在各种…
- Apache HTTP Server存在…
- 使用 Apache OpenJPA 开发 EJB 3.0…
- Windows/Linux 环境下安装 Apache …
- linux apache 服务器开启rewrite …
- 成功配置了apache tomcat + …
- linux压缩和解压缩命令大全
- 使用Mybatis遇到的错误
- Linux rpm 命令参数使用…
- mac os 下安装oracle 客户端
- 程序中闭包有什么用
- Apache POI 读写 E…
- Tomcat服务器&http笔记
- hdu2874 Connections between cities(LCA)
- SpringMVC中url-pattern /和/…
- 基于vue-cli的vuex学习笔记
- CommonsMultipartFile与MultipartF…
- Class.getResource和ClassLoader.g…
- Springmvc构造RESTful详细讲解
- spring-data-elasticsearch和shield插件结合