- 浏览: 464903 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (190)
- ExtJS (40)
- JavaScript (16)
- Java设计模式 (13)
- Html (10)
- Tomcat (9)
- myeclipse (5)
- JSP (5)
- CSS (10)
- JS/CSS (1)
- windows (12)
- Navicat For MySQL (1)
- struts2 (6)
- Spring2.0 (1)
- Java反射机制 (5)
- hibernate (9)
- java (17)
- B/SWeb完美打印方案 (1)
- 数据库相关 (4)
- java web报表 (1)
- 报表打印 (2)
- java泛型 (1)
- Servlet (2)
- SVN (7)
- Log4j (0)
- android (3)
最新评论
-
kation733:
斯蒂芬斯蒂芬多萨法阿凡达是否阿萨德
同时开启两个Tomcat -
BlueBing:
一路的夏天 写道楼主的代码会报错吧!在前面的时候String ...
使用Java反射机制遍历实体类的属性和类型 -
ying890:
非常感谢!
Ext JS中文乱码解决方案 -
zhylandroid:
谢谢了,对我很有帮助
DatabaseMetaData开发实务(上) -
wendy.wujing:
maxHttpHeaderSize="8192&qu ...
查看Tomcat配置的端口
项目中经常要解析和生成Excel文件,最常用的开源组件有poi与jxl。jxl是韩国人开发的,发行较早,但是更新的很慢,目前似乎还不支持excel2007。poi是apache下的一个子项目,poi应该是处理ms的office系列文档最好的组件了。poi3.6版本已经开始支持excel2007了。但是由于excel2007底层的实现似乎变成xml与excel2003底层存储发生了本质的变化,因此poi解析excel的类就存在差异了。
现在简单的介绍下poi常用的接口。
经常用的类一般都在org.apache.poi.hssf.usermodel(excel2003)或org.apache.poi.xssf.usermodel
(excel2007)。
- 工作薄: WorkBook是操作Excel的入口,HSSFWorkbook, XSSFWorkbook实现了该接口。
- 页:Sheet表示工作薄的分页。HSSFSheet, XSSFChartSheet, XSSFDialogsheet, XSSFSheet实现了该接口。
- Row:表示页中的一行。HSSFRow, XSSFRow实现了该接口。
- Cell:行中的一个单元格。HSSFCell, XSSFCell实现了该接口。
从上面的介绍得知:页是通过工作薄对象创建的,行是通过页对象创建的,单元格是通过行对象创建的。接下来,我们就开始发掘poi的强大功能吧。
- 创建一个空白的工作薄
- import java.io.FileOutputStream;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.ss.usermodel.Workbook;
- import org.apache.poi.xssf.usermodel.XSSFWorkbook;
- //2003版本
- Workbook wb = new HSSFWorkbook();
- FileOutputStream fileOut = new FileOutputStream("workbook.xls");
- wb.write(fileOut);
- fileOut.close();
- //2007版本
- // Workbook wb = new XSSFWorkbook();
- // FileOutputStream fileOut = new FileOutputStrea("workbook.xlsx");
- // wb.write(fileOut);
- // fileOut.close();
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
//2003版本
Workbook wb = new HSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
//2007版本
// Workbook wb = new XSSFWorkbook();
// FileOutputStream fileOut = new FileOutputStrea("workbook.xlsx");
// wb.write(fileOut);
// fileOut.close();
注意:Workbook 是org.apache.poi.ss.usermodel包下的一个接口,注意与以前版本的不同,HSSFWorkbook和XSSFWorkbook实现了该接口,这样就达到了面前接口编程。下面的excel工具类中要用到此点知识。
2. 创建两个空白页
- Workbook wb = new HSSFWorkbook();
- //Workbook wb = new XSSFWorkbook();
- Sheet sheet1 = wb.createSheet("new sheet");
- Sheet sheet2 = wb.createSheet("second sheet");
- FileOutputStream fileOut = new FileOutputStream("workbook.xls");
- wb.write(fileOut);
- fileOut.close();
Workbook wb = new HSSFWorkbook();
//Workbook wb = new XSSFWorkbook();
Sheet sheet1 = wb.createSheet("new sheet");
Sheet sheet2 = wb.createSheet("second sheet");
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
3. 创建单元格
- public void createRow() throws Exception {
- Workbook wb = new HSSFWorkbook();
- // Workbook wb = new XSSFWorkbook();
- CreationHelper createHelper = wb.getCreationHelper();
- Sheet sheet = wb.createSheet("new sheet");
- //创建一行并放一些单元格到该行中,行的索引是以0开始的
- Row row = sheet.createRow((short) 0);
- // 创建一个单元格并填充一个整数的值
- Cell cell = row.createCell(0);
- cell.setCellValue(1);
- //链式写法
- row.createCell(1).setCellValue(1.2);
- row.createCell(2).setCellValue(
- createHelper.createRichTextString("This is a string"));
- row.createCell(3).setCellValue(true);
- //输出文件
- FileOutputStream fileOut = new FileOutputStream("workbook.xls");
- wb.write(fileOut);
- fileOut.close();
- }
public void createRow() throws Exception {
Workbook wb = new HSSFWorkbook();
// Workbook wb = new XSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("new sheet");
//创建一行并放一些单元格到该行中,行的索引是以0开始的
Row row = sheet.createRow((short) 0);
// 创建一个单元格并填充一个整数的值
Cell cell = row.createCell(0);
cell.setCellValue(1);
//链式写法
row.createCell(1).setCellValue(1.2);
row.createCell(2).setCellValue(
createHelper.createRichTextString("This is a string"));
row.createCell(3).setCellValue(true);
//输出文件
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
4. 创建日期单元格
- public void createDateCell() throws Exception {
- Workbook wb = new HSSFWorkbook();
- // Workbook wb = new XSSFWorkbook();
- CreationHelper createHelper = wb.getCreationHelper();
- Sheet sheet = wb.createSheet("new sheet");
- // Create a row and put some cells in it. Rows are 0 based.
- Row row = sheet.createRow(0);
- // Create a cell and put a date value in it. The first cell is not
- // styled
- // as a date.
- Cell cell = row.createCell(0);
- cell.setCellValue(new Date());
- // we style the second cell as a date (and time). It is important to
- // create a new cell style from the workbook otherwise you can end up
- // modifying the built in style and effecting not only this cell but
- // other cells.
- CellStyle cellStyle = wb.createCellStyle();
- cellStyle.setDataFormat(createHelper.createDataFormat().getFormat(
- "yyyy/MM/dd hh:mm"));
- cell = row.createCell(1);
- // cell.setCellValue(new Date());
- Date date = new Date();
- cell.setCellValue(createHelper.createRichTextString(date.toString()));
- cell.setCellStyle(cellStyle);
- // you can also set date as java.util.Calendar
- cell = row.createCell(2);
- cell.setCellValue(Calendar.getInstance());
- cell.setCellStyle(cellStyle);
- // Write the output to a file
- FileOutputStream fileOut = new FileOutputStream("workbook.xls");
- wb.write(fileOut);
- fileOut.close();
- }
public void createDateCell() throws Exception {
Workbook wb = new HSSFWorkbook();
// Workbook wb = new XSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow(0);
// Create a cell and put a date value in it. The first cell is not
// styled
// as a date.
Cell cell = row.createCell(0);
cell.setCellValue(new Date());
// we style the second cell as a date (and time). It is important to
// create a new cell style from the workbook otherwise you can end up
// modifying the built in style and effecting not only this cell but
// other cells.
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(createHelper.createDataFormat().getFormat(
"yyyy/MM/dd hh:mm"));
cell = row.createCell(1);
// cell.setCellValue(new Date());
Date date = new Date();
cell.setCellValue(createHelper.createRichTextString(date.toString()));
cell.setCellStyle(cellStyle);
// you can also set date as java.util.Calendar
cell = row.createCell(2);
cell.setCellValue(Calendar.getInstance());
cell.setCellStyle(cellStyle);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
5. 创建不同的单元格样式
- public void createCellType() throws Exception{
- Workbook wb = new HSSFWorkbook();
- Sheet sheet = wb.createSheet("new sheet");
- Row row = sheet.createRow((short)2);
- row.createCell(0).setCellValue(1.1);
- row.createCell(1).setCellValue(new Date());
- row.createCell(2).setCellValue(Calendar.getInstance());
- row.createCell(3).setCellValue("a string");
- row.createCell(4).setCellValue(true);
- row.createCell(5).setCellType(HSSFCell.CELL_TYPE_ERROR);
- // Write the output to a file
- FileOutputStream fileOut = new FileOutputStream("workbook.xls");
- wb.write(fileOut);
- fileOut.close();
- }
public void createCellType() throws Exception{
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
Row row = sheet.createRow((short)2);
row.createCell(0).setCellValue(1.1);
row.createCell(1).setCellValue(new Date());
row.createCell(2).setCellValue(Calendar.getInstance());
row.createCell(3).setCellValue("a string");
row.createCell(4).setCellValue(true);
row.createCell(5).setCellType(HSSFCell.CELL_TYPE_ERROR);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
6. 设置单元格水平垂直对齐方式
- public static void main(String[] args) throws Exception {
- Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
- Sheet sheet = wb.createSheet();
- Row row = sheet.createRow((short) 2);
- row.setHeightInPoints(30);
- createCell(wb, row, (short) 0, XSSFCellStyle.ALIGN_CENTER, XSSFCellStyle.VERTICAL_BOTTOM);
- createCell(wb, row, (short) 1, XSSFCellStyle.ALIGN_CENTER_SELECTION, XSSFCellStyle.VERTICAL_BOTTOM);
- createCell(wb, row, (short) 2, XSSFCellStyle.ALIGN_FILL, XSSFCellStyle.VERTICAL_CENTER);
- createCell(wb, row, (short) 3, XSSFCellStyle.ALIGN_GENERAL, XSSFCellStyle.VERTICAL_CENTER);
- createCell(wb, row, (short) 4, XSSFCellStyle.ALIGN_JUSTIFY, XSSFCellStyle.VERTICAL_JUSTIFY);
- createCell(wb, row, (short) 5, XSSFCellStyle.ALIGN_LEFT, XSSFCellStyle.VERTICAL_TOP);
- createCell(wb, row, (short) 6, XSSFCellStyle.ALIGN_RIGHT, XSSFCellStyle.VERTICAL_TOP);
- // Write the output to a file
- FileOutputStream fileOut = new FileOutputStream("xssf-align.xlsx");
- wb.write(fileOut);
- fileOut.close();
- }
- /**
- * Creates a cell and aligns it a certain way.
- *
- * @param wb the workbook
- * @param row the row to create the cell in
- * @param column the column number to create the cell in
- * @param halign the horizontal alignment for the cell.
- */
- private static void createCell(Workbook wb, Row row, short column, short halign, short valign) {
- Cell cell = row.createCell(column);
- cell.setCellValue(new XSSFRichTextString("Align It"));
- CellStyle cellStyle = wb.createCellStyle();
- cellStyle.setAlignment(halign);
- cellStyle.setVerticalAlignment(valign);
- cell.setCellStyle(cellStyle);
- }
发表评论
-
Errors running builder 'JavaScript Validator' on project '......'
2013-02-22 12:14 971在编译java工程时,如果出现 “Errors occur ... -
list,set,map,数组间的相互转换
2012-09-29 13:53 1009list,set,map,数组间的相互转换 ... -
POI学习笔记(三)
2010-06-29 15:14 422614. 自定义颜色 HSSF: ... -
POI学习笔记(二)
2010-06-29 15:12 49987. 设置单元格的边框 Java代码 ... -
web.xml 配置详解
2010-05-27 11:07 1090在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似 ... -
如何调用hh.exe或者别的命令行的方式,在已经打开了的chm中,定位到指定页面?
2010-05-25 11:55 2122hh.exe mk:@MSITStore:F:\\buyord ... -
如何用一个按钮打开chm的帮助文件
2010-05-25 10:26 2082如果是在windows下运行的话,可以用Runtime的get ... -
java文件或jsp、xml文件注释
2010-01-13 16:29 1793Java用Shift+Ctrl+C(单行注释)没问题,Jsp页 ... -
对Java Serializable(序列化)的理解和总结
2010-01-12 09:05 10301、序列化是干什么的? ... -
基本数据类型转换
2010-01-06 09:42 1018int -> String int i=12345;S ... -
配置JDK
2009-12-18 15:07 1352下载好的JDK是一个可执 ... -
一个java处理JSON格式数据的通用类
2009-12-10 16:50 1201一个java处理JSON格式数据的通用类 进入需要在项目中用 ... -
JAVA注解
2009-12-04 08:33 2666java 注解 可以使用在类,方法,全局变量,局部变量作用: ... -
java对象排序
2009-11-18 09:46 1292Java集合对象排序测试 Java API针对集合类型排 ... -
java中对于复杂对象排序的模型及其实现
2009-11-18 09:41 1125排序是编程中经常要碰到的问题,如果只是一般的数据库数据那么我们 ... -
java中对于复杂对象排序的模型及其实现
2009-10-23 15:26 1414排序是编程中经常要碰到的问题,如果只是一般的数据库数据那么我们 ...
相关推荐
POI学习笔记第二版更详细的POI学习笔记第二版更详细的
POI学习笔记 POI中文教程(简单的)
POI的学习笔记,总结的还行。希望大家会喜欢
百度地图学习笔记,有代码。图层 定位 地理编码 poi搜索
OA (ssh) 基本实现(poi 生成 Excel , struts2动态下载 mysql数据库文件) 学习笔记(含源代码) 借鉴 风中叶 老师的视频,写的文章,代码比较详实。 说了很多我的看法,和思考,做了充分的日志
[摘要]:本文通过整理与VC维相关的重要概念,对分(Dichotomy)、增长函数(Growth Function)、打散(Shatter)、Break Poi
Spring Boot 学习笔记 基础篇 #提升篇 15.SpringBoot之集成Shiro 16.SpringBoot之使用mybatis-generator自动生成代码 17.SpringBoot之使用lombok编码 18.SpringBoot之初始化数据 19.SpringBoot之使用POI开发Excel...
java学习过程中的随手笔记,包括常见的文件选择和保存窗口的实现,poi的excel处理
非常值得学习的资源,结合笔记更快上手。 有poi报表生成,访问分析等技术, 非常值得学习的资源,结合笔记更快上手。 有poi报表生成,访问分析等技术,