Poi解决excel.word,powerpoint的读写问题

?? Poi主要用来解决excel.word,powerpoint的读写问题,官方网站是http://jakarta.apache.org/poi/,下载地址:http://apache.justdn.org/jakarta/poi/,常用开发包poi-bin-3.2-FINAL-xxx.zip;开发excel所用包是:poi-x.x-FINAL-xxxx.jar;开发word所用包:poi-3.2-FINAL-20081019.jar,poi-scratchpad-x.x-FINAL-xxxx.jar;

一.Excel开发实例

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.io.FileInputStream;
import java.io.InputStream;

public class CreateExcel {
?public static void main(String[] args)throws IOException{
??? HSSFWorkbook wb = new HSSFWorkbook();
??? HSSFSheet sheet = wb.createSheet(“new sheet”);
??? // Create a row and put some cells in it. Rows are 0 based.
??? HSSFRow row = sheet.createRow(0);
??? // Create a cell and put a date value in it. The first cell is not styled as
?// a date.
??? HSSFCell 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.
??? HSSFCellStyle cellStyle = wb.createCellStyle();
??? cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat(“m/d/yy h:mm”));
??? cell = row.createCell(1);
??? cell.setCellValue(new Date());
??? cell.setCellStyle(cellStyle);
??? // HSSFCellStyle cellStyle = wb.createCellStyle(); // 建立新的cell样式
??? HSSFCellStyle cellStyle1 = wb.createCellStyle();
??? HSSFDataFormat format? =? wb.createDataFormat();
??? cellStyle1.setDataFormat(wb.createDataFormat().getFormat(“#,##0.00”));? // 设置cell样式为定制的浮点数格式
??? row.createCell(2).setCellValue(new HSSFRichTextString(“hello”) );
??? row.createCell(3).setCellValue(true);
??? HSSFCell cell1 =? row.createCell(4);
??? cell1.setCellStyle(cellStyle1);? // 设置该cell浮点数的显示格式
??? cell1.setCellValue(444123.4678910);
??? // Write the output to a file
??? FileOutputStream fileOut = new FileOutputStream(“d:\workbook.xls”);
??? wb.write(fileOut);
??? fileOut.close();
??? // Create a POIFSFileSystem from an InputStream. Normally the stream is read
?// until EOF. The stream is always closed.
??? HSSFWorkbook workbook = new? HSSFWorkbook(new POIFSFileSystem(new FileInputStream(“d:\workbook.xls”)));// 创建对Excel工作簿文件的引用
??? for?? (int numSheets= 0;numSheets< workbook.getNumberOfSheets();numSheets++)?? {
????? if?? (null != workbook.getSheetAt(numSheets)){
??????? HSSFSheet aSheet = workbook.getSheetAt(numSheets);// 获得一个sheet
??????? for (int rowNumOfSheet = 0; rowNumOfSheet <= aSheet.getLastRowNum(); rowNumOfSheet++) {
??????? ?if (null != aSheet.getRow(rowNumOfSheet)) {
??????? ??HSSFRow aRow = aSheet.getRow(rowNumOfSheet); // 获得一个行
??????? ??for (int cellNumOfRow = 0; cellNumOfRow <= aRow.getLastCellNum(); cellNumOfRow++) {
??????? ???if (null != aRow.getCell(cellNumOfRow)) {
??????? ????HSSFCell aCell = aRow.getCell(cellNumOfRow);
??????? ????if(aCell.getCellType()==HSSFCell.CELL_TYPE_STRING) //get the cells type (numeric, formula or string)
??????? ?????????? System.out.println(aCell.getRichStringCellValue());
//??????? ????if(aCell.getCellType()==HSSFCell.CELL_TYPE_NUMERIC)
//??????? ?????System.out.println(aCell.getNumericCellValue());
??????? ???}?
??????? ??}
??????? ?}
??????? }
????? }
??? }
?}
}

附:
servlet导出xls:
response.setContentType(“application/vnd.ms-excel;charset=gb2312”);??
response.addHeader(“Content-Disposition”, “attachment;filename=test.xls”);??
ServletOutputStream out = response.getOutputStream();?
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(new File(“F:/xx.xls”)));
//wb write operate…..
wb.write(out);
out.flush();??
out.close();??
中文乱码问题处理:
cell.setEncoding(HSSFCell.ENCODING_UTF_16);

二.Word开发实例

(1)读操作

import java.io.FileInputStream;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;

public class WordReader {
?public static void main(String args[]) throws Exception {
??// 1:
??HWPFDocument doc = new HWPFDocument(
????new FileInputStream(“d:\hello.doc”));
??Range r = doc.getRange(); // 取得word文档的范围
??int lenParagraph = r.numParagraphs();// 取得段落数
??// Paragraph p;
??for (int x = 0; x < lenParagraph; x++) {
???Paragraph p = r.getParagraph(x);
???String text = p.text();
???System.out.println(text);
??}
??// 2:
??String text = null;
??// 创建WordExtractor
??WordExtractor extractor = new WordExtractor(new FileInputStream(
????”d:\hello.doc”));// (new FileInputStream(“e:\text.doc”));
??// 对DOC文件进行提取
??text = extractor.getText();
??System.out.println(text);
?}
?// POIFSFileSystem fsys = new POIFSFileSystem(new
?// FileInputStream(“d:\hello.doc”));
?// DocumentEntry headerProps = (DocumentEntry)
?// fsys.getRoot().getEntry(“WordDocument”);
?// DocumentInputStream din = fsys.createDocumentInputStream(“WordDocument”);
?// din.read(headerProps);
?// din.close();

}

(2)写操作

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class WordWriter {
?public static void main(String args[]) throws Exception {
??byte[] a = new String(“大家好!欢迎使用poi word功能!”).getBytes();
??ByteArrayInputStream bs = new ByteArrayInputStream(a);
??//以下两句代码不能省略,否则输出的是乱码
??//This is the main class of the POIFS system; it manages the entire life cycle of the filesystem.
??POIFSFileSystem fs = new POIFSFileSystem();
??//This interface defines methods specific to Directory objects managed by a Filesystem instance
??DirectoryEntry directory = fs.getRoot();
??//This interface defines methods specific to Document objects managed by a Filesystem instance.
??DocumentEntry de = directory.createDocument(“WordDocument”, bs);
??//System.out.println(de.getName()+de.getSize()+a.length);
??FileOutputStream fos = new FileOutputStream(“e:\text.doc”);
??fs.writeFilesystem(fos);
??bs.close();
??fos.flush();
??fos.close();
?? }
}

三.Powerpoint开发常用类

org.apache.poi.hslf.HSLFSlideShow;
org.apache.poi.hslf.model.TextRun;
org.apache.poi.hslf.model.Slide;
org.apache.poi.hslf.usermodel.SlideShow;

四.附录

  Java中,已经有很多对于Word、Excel的开源的解决方案,其中比较出色的是Apache的Jakata项目的POI子项目;当然了还有Jacob,jacob 是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁。使用Jacob自带的DLL动态链接库,并通过JNI的方式实现了在Java平台上对COM程序的调用。Jacob下载的地址为:http://sourceforge.net/project/showfiles.php?group_id=109543&package_id=118368

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部