- 浏览: 83873 次
- 性别:
- 来自: 上海
最新评论
-
abc382410124:
不错呀 学习了
String.getBytes()和new String() -
lovekang89:
学习了,看起来不错
如何有效防止Java程序源码被人偷窥?
项目中经常要解析和生成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();
- <SPAN style="FONT-SIZE: medium">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();</SPAN>
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();
- <SPAN style="FONT-SIZE: medium">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();
- </SPAN>
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();
- }
- <SPAN style="FONT-SIZE: medium">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();
- }</SPAN>
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();
- }
- <SPAN style="FONT-SIZE: medium">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();
- }</SPAN>
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();
- }
- <SPAN style="FONT-SIZE: medium">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();
- }</SPAN>
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);
- }
相关推荐
POI学习笔记第二版更详细的POI学习笔记第二版更详细的
"POI学习笔记" POI(Apache POI)是一款流行的Java库,用于处理Microsoft Office文件格式,包括Excel、Word、PowerPoint等。POI提供了一个简洁和灵活的API,允许开发者轻松地读取、写入和操作Office文件。 POI的...
### POI学习笔记知识点解析 #### 一、POI简介 Apache POI是一个开源的Java API,用于处理Microsoft Office格式的文件,包括Excel (.xls, .xlsx), PowerPoint (.ppt, .pptx) 和 Word (.doc, .docx)。POI提供了一套...
Apache POI 是一个开源项目,专门用于处理 Microsoft Office 格式文档,如 .doc、.xls 和 .ppt 文件。在 Java 开发环境中,POI 提供了一套丰富的 API,使得开发者能够读取、写入以及操作这些文件。下面将详细介绍 ...
POI导出POI导出POI导出POI导出POI导出POI导出POI导出POI导出POI导出POI导出POI导出POI导出POI导出POI导出POI导出POI导出POI导出POI导出POI导出POI导出POI导出POI导出POI导出POI导出POI导出POI导出POI导出POI导出POI...
Apache POI是一个开源的Java库,专门用于读写Microsoft Office格式的文件,特别是Excel、Word和PowerPoint文档。在提供的描述中,我们看到的路径实际上是在指示Apache POI项目中的不同模块和示例代码的位置。 1. **...
对于更深入的学习和了解 POI 的使用方法,可以参考以下资源: - **官方文档**:[http://jakarta.apache.org/poi/](http://jakarta.apache.org/poi/) - **HSSF 快速指南**:...
在地理信息系统中,一个POI可以是一栋房子、 一个商铺、一个邮筒、一个公交站等。 高德POI又名高德兴趣点,来源于高德地图,高德是中国领先的数字地图内容、导航和位置服务解决方 案提供商。拥有导航电子地图...
Apache POI 是一个开源项目,专门用于处理Microsoft Office格式的文件,如Excel(.xlsx、.xls)、Word(.doc、.docx)和PowerPoint(.ppt、.pptx)。这个压缩包包含了POI项目中所有必要的jar包,总计十二个,确保了...
Apache POI 是一个Java库,专门用于读写Microsoft Office格式的文件,特别是Excel。在描述中提到的HSSF是POI中的一个接口,用于处理MSExcel的对象,这意味着它允许开发者在Java环境中创建、修改和读取Excel文件。...
Apache POI 是一个开源项目,专门用于处理微软的Office文档格式,如Excel、Word和PowerPoint。POI 提供了一套 Java API,使得开发者可以在Java应用程序中读写Microsoft Office格式的文件。标题中的"poi-3.17 poi-...
- POI库的API设计相对复杂,学习曲线较陡峭,需要花时间去熟悉各种类和方法。 - 当处理复杂的公式或图表时,POI可能无法完全模拟Excel的所有特性,因此在某些情况下可能需要使用其他工具或库进行补充。 - 在更新版本...
POI数据是地理位置信息系统(GIS)中的一个重要组成部分,它包含了地点的坐标、名称、类型等信息,广泛用于导航、地图应用、商业分析等领域。 描述中提到的“2005到2020年的科研POI数据”暗示这是一份时间跨度较长...
在地理信息系统中,一个POI可以是一栋房子、一个商铺、一个邮筒、一个公交站等。 高德POI又名高德兴趣点,来源于高德地图,高德是中国领先的数字地图内容、导航和位置服务解决方案提供商。拥有导航电子地图甲级...
poi3.8快速学习指南
POI 3.17是支持Java 6的最后一个版本。下一个版本将是4.0.0,并支持min。Java 8。 发行说明中提供了更改摘要 。更改日志中提供了完整的更改列表。感兴趣的人也应该遵循开发列表 来跟踪进度。 有积分的可以在这儿下载...
Apache POI 是一个开源项目,专门用于处理微软的Office文档格式,如Excel、Word和PowerPoint。在Java编程环境中,Apache POI 提供了API,让开发者能够方便地读取、写入和修改这些文件。标题提到的"poi-3.9、poi-...
在本文中,我们将学习如何将poi导入到eclipse中,并搭建好eclipse环境项目,以便更深一步地学习Java读取Microsoft Office的文件。 首先,需要下载poi的jar包。截至本文发表,poi最新版本是3.0.1,距离上一个Final...
在IT行业中,POI...免费下载的POI数据资源对于学习、研究或开发基于位置的服务非常有帮助,但使用时需要注意版权和合规性问题。通过理解并有效利用这些数据,开发者能够构建出更智能、更贴近用户需求的地图应用。