POI是APACHE的一个Jakarta Sub Project,Pure Java,用于操作DOC,PPT,XLS的库,使用很简单,对于大部分Windows Client像蟑螂一样遍布世间的时候,这个库用处就很大了哦。
今天是POI的Sub Project HSSF-----Java API To Access Microsoft Excel Format Files
Example One:创建XLS
HSSFWorkbook wb = new HSSFWorkbook();//构建新的XLS文档对象
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);//注意,参数是文件输出流对象
fileOut.close();
Example Two:创建Sheet
HSSFWorkbook wb = new HSSFWorkbook();//创建文档对象
HSSFSheet sheet1 = wb.createSheet("new sheet");//创建Sheet对象,参数为Sheet的标题
HSSFSheet sheet2 = wb.createSheet("second sheet");//同上,注意,同是wb对象,是一个XLS的两个Sheet
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
Example Three:创建小表格,并为之填上数据
HSSFWorkbook document = new HSSFWorkbook();//创建XLS文档
HSSFSheet salary = document.createSheet("薪水");//创建Sheet
HSSFRow titleRow = salary.createRow(0);//创建本Sheet的第一行
titleRow.createCell((short) 0).setCellValue("工号");//设置第一行第一列的值
titleRow.createCell((short) 1).setCellValue("薪水");//......
titleRow.createCell((short) 2).setCellValue("金额");//设置第一行第二列的值
File filePath = new File(baseDir+"excel/example/");
if(!filePath.exists())
filePath.mkdirs();
FileOutputStream fileSystem = new FileOutputStream(filePath.getAbsolutePath()+"/Three.xls");
document.write(fileSystem);
fileSystem.close();
Example Four :带自定义样式的数据(eg:Date)
HSSFWorkbook document = new HSSFWorkbook();
HSSFSheet sheet = document.createSheet("日期格式");
HSSFRow row = sheet.createRow(0);
HSSFCell secondCell = row.createCell((short) 0);
/**
* 创建表格样式对象
*/
HSSFCellStyle style = document.createCellStyle();
/**
* 定义数据显示格式
*/
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
/**
* setter
*/
secondCell.setCellValue(new Date());
/**
* 设置样式
*/
secondCell.setCellStyle(style);
File filePath = new File(baseDir+"excel/example/");
if(!filePath.exists())
filePath.mkdirs();
FileOutputStream fileSystem = new FileOutputStream(filePath.getAbsolutePath()+"/Four.xls");
document.write(fileSystem);
fileSystem.close();
Example Five:读取XLS文档
File filePath = new File(baseDir+"excel/example/");
if(!filePath.exists())
throw new Exception("没有该文件");
/**
* 创建对XLS进行读取的流对象
*/
POIFSFileSystem reader = new POIFSFileSystem(new FileInputStream(filePath.getAbsolutePath()+"/Three.xls"));
/**
* 从流对象中分离出文档对象
*/
HSSFWorkbook document = new HSSFWorkbook(reader);
/**
* 通过文档对象获取Sheet
*/
HSSFSheet sheet = document.getSheetAt(0);
/**
* 通过Sheet获取指定行对象
*/
HSSFRow row = sheet.getRow(0);
/**
* 通过行、列定位Cell
*/
HSSFCell cell = row.getCell((short) 0);
/**
* 输出表格数据
*/
log.info(cell.getStringCellValue());
至此,使用POI操作Excel的介绍告一段落,POI是一个仍然在不断改善的项目,有很多问题,比如说中文问题,大数据量内存溢出问题等等,但这个Pure Java的库的性能仍然是不容质疑的,是居家旅行必备良品。
而且开源软件有那么一大点好处是,可以根据自己的需要自己去定制。如果大家有中文、性能等问题没解决的,可以跟我索要我已经改好的库。当然,你要自己看原代码,我也不拦你。
分享到:
相关推荐
poi的所有jar包,poi, poi-ooxml,poi-ooxml-schemas的各个版本jar poi的所有jar包,poi, poi-ooxml,poi-ooxml-schemas的各个版本jar
poi-3.17 对work、excel操作,包含有: poi-3.17.jar poi-ooxml-3.17.jar poi-scratchpad-3.17.jar poi-ooxml-schemas-3.17.jar commons-codec-1.10.jar commons-collections4-4.1.jar commons-logging-1.2.jar ...
当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:...目前,比较常用的实现Java导入、导出Excel的技术有两种Jakarta POI和Java Excel。本poi-4.1.2.jar、poi-ooxml-schemas-4.1.2.jar、等
ooxml-schemas/poi/poi-examples/poi-ooxml/poi-ooxml-schemas/poi-scratchpad
HSSF Excel XLS poi HSLF PowerPoint PPT poi-scratchpad HWPF Word DOC poi-scratchpad HDGF Visio VSD poi-scratchpad HPBF Publisher PUB poi-scratchpad HSMF Outlook MSG poi-scratchpad XSSF Excel XLSX...
poi-4.1.2完整jar包。 poi-4.1.2.jar poi-examples-4.1.2.jar poi-excelant-4.1.2.jar poi-ooxml-4.1.2.jar poi-ooxml-schemas-4.1.2.jar poi-scratchpad-4.1.2.jar
这里三个poi架包是java程序导出excel文件必须要用到的
poi-ooxml-3.7.jar包下载,最新可用支持office各种文档,Java POI读取Office excel (2003,2007)及相关jar包 其中的poi-ooxml-3.7.jar
Java POI读取Office excel (2003,2007)及相关jar包 其中必要的poi-ooxml-schemas-3.7-20101029.jar
含有java中对excel操作的三个jar包,分别为poi-3.15,poi-3.16,poi-3.17三个版本
poi--4.导入excelpoi--4.导入excelpoi--4.导入excelpoi--4.导入excelpoi--4.导入excelpoi--4.导入excel
poi-3.9jar包,支持excel03和excel07
用户poi开发,将excel中的数据导入到数据库中所用的jar包。poi-3.8-20120326.jar 和 poi-excelant-3.8-20120326.jar
Android 解析Excel文件的jar包,可以将从手机中导入app的excel文件解析成JavaBean对象,需要配合poi-ooxml-schemas-3.12-20150511-a.jar使用。
Excel工作表java导入导出,HSSFWorkbook,XSSFWorkbook,poi-4.0.1.jar,poi-ooxml-4.0.1.jar稳定jar包,ExcleUtils工具类
附件是java poi 3.17的lib库及其依赖库,全部下载引用后,可以进行excel的读写操作,无需引用其他lib库。poi-3.17适用于jdk1.7。 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft ...
CHM格式的Excel开源框架POI -4.12 API 文档,CHM格式的Excel开源框架POI -4.12 API 文档,CHM格式的Excel开源框架POI -4.12 API 文档,CHM格式的Excel开源框架POI -4.12 API 文档,CHM格式的Excel开源框架POI -4.12 ...
poi4.0相关jar包: poi-4.0.0.jar poi-examples-4.0.0.jar poi-excelant-4.0.0.jar poi-ooxml-4.0.0.jar poi-ooxml-schemas-4.0.0.jar poi-scratchpad-4.0.0.jar
poi-3.5-FINAL-20090928.jar 支持读取word/excel 2007