

下载一个poi库http://poi.apache.org/download.html
主要的api
//读取word例子可以导出图片
package org.evilbinary.word;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.OutputStream;
importjava.util.ArrayList;
importjava.util.List;
importorg.apache.poi.hwpf.HWPFDocument;
importorg.apache.poi.hwpf.extractor.WordExtractor;
importorg.apache.poi.hwpf.model.PAPBinTable;
importorg.apache.poi.hwpf.model.PAPX;
importorg.apache.poi.hwpf.model.PicturesTable;
importorg.apache.poi.hwpf.model.TextPiece;
importorg.apache.poi.hwpf.usermodel.CharacterRun;
importorg.apache.poi.hwpf.usermodel.Paragraph;
importorg.apache.poi.hwpf.usermodel.Picture;
importorg.apache.poi.hwpf.usermodel.Range;
importorg.apache.poi.hwpf.usermodel.Section;
publicclass Main {
privatestatic final boolean DEBUG=true;
publicstatic void wordTest() {
try{
FileInputStreamfis = new FileInputStream("D:\\Test\\word\\2.doc");
HWPFDocumentdoc = new HWPFDocument(fis);
PicturesTablepictuesTable = doc.getPicturesTable();
List<Picture>allPics = pictuesTable.getAllPictures();
for(Picture p : allPics) {
System.out.println("size:"+ p.getSize() + " name:"
+p.suggestFullFileName() + " ext:"
+p.suggestFileExtension());
FileOutputStreamfos = new FileOutputStream("D:\\Test\\word\\"
+p.suggestFullFileName());
fos.write(p.getContent());
fos.close();
}
Ranger = doc.getRange();
System.out.println("Sectionsize:" + r.numSections());
StringBuildersb = new StringBuilder();
intpicPos = 0;
for(int i = 0; i < r.numSections(); i++) {
Sections = r.getSection(i);
System.out.println("Paragraphssize:" + s.numParagraphs());
for(int j = 0; j < s.numParagraphs(); j++) {
Paragraphp = s.getParagraph(j);
for(int z = 0; z < p.numCharacterRuns(); z++) {
CharacterRunrun = p.getCharacterRun(z);
if(run.getPicOffset() <= 0) {
sb.append(run.text());
//System.out.print(text);
}else {
sb.append("picoffset:"+ run.getPicOffset()
+" len:" + run.text().length());
//System.out.println("picoffset:"+run.getPicOffset()+"
//len:"+ run.text().length());
if(allPics.size() > picPos) {
Picturepic = allPics.get(picPos);
if(pic != null) {
//System.out.println("size:"+pic.getSize()+"
//name:"+pic.suggestFullFileName()+"
//ext:"+pic.suggestFileExtension());
sb.append("size:"+ pic.getSize()
+" name:"
+pic.suggestFullFileName()
+" ext:"
+pic.suggestFileExtension());
picPos++;
}
}
}
}
}
}
System.out.println(sb);
fis.close();
}catch (FileNotFoundException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param args
*/
publicstatic void main(String[] args) {
wordTest();
}
//读取excel的例子
packageorg.evilbinary.excel;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.util.Iterator;
importorg.apache.poi.ss.format.CellFormatResult;
importorg.apache.poi.hssf.usermodel.HSSFWorkbook;
importorg.apache.poi.hssf.util.CellReference;
importorg.apache.poi.openxml4j.exceptions.InvalidFormatException;
importorg.apache.poi.ss.format.CellFormat;
importorg.apache.poi.ss.usermodel.*;
importorg.apache.poi.xssf.usermodel.XSSFWorkbook;
publicclassMain {
/**
*@paramargs
*/
publicstaticvoidexcelTest() {
Workbook[]wbs = newWorkbook[] { newHSSFWorkbook() };
for(inti = 0; i < wbs.length;i++) {
Workbookwb = wbs[i];
CreationHelpercreateHelper = wb.getCreationHelper();
//create a new sheet
Sheets = wb.createSheet();
//declare a row object reference
//Row r = null;
//// declare a cell object reference
//Cell c = null;
//create 2 cell styles
CellStylecs = wb.createCellStyle();
CellStylecs2 = wb.createCellStyle();
DataFormatdf = wb.createDataFormat();
//create 2 fonts objects
Fontf = wb.createFont();
Fontf2 = wb.createFont();
//Set font 1 to 12 point type, blue and bold
f.setFontHeightInPoints((short)12);
f.setColor(IndexedColors.RED.getIndex());
f.setBoldweight(Font.BOLDWEIGHT_BOLD);
//Set font 2 to 10 point type, red and bold
f2.setFontHeightInPoints((short)10);
f2.setColor(IndexedColors.RED.getIndex());
f2.setBoldweight(Font.BOLDWEIGHT_BOLD);
//Set cell style and formatting
cs.setFont(f);
cs.setDataFormat(df.getFormat("#,##0.0"));
//Set the other cell style and formatting
cs2.setBorderBottom(cs2.BORDER_THIN);
cs2.setDataFormat(df.getFormat("text"));
cs2.setFont(f2);
//Define a few rows
for(intrownum = 0; rownum < 30; rownum++) {
Rowr = s.createRow(rownum);
for(intcellnum = 0; cellnum < 10; cellnum += 2) {
Cellc = r.createCell(cellnum);
Cellc2 = r.createCell(cellnum + 1);
c.setCellValue((double)rownum + (cellnum / 10));
c2.setCellValue(createHelper.createRichTextString("Hello!"
+cellnum));
}
}
//Save
Stringfilename = "workbook.xls";
if(wb instanceofXSSFWorkbook) {
filename= filename + "x";
}
FileOutputStreamout;
try{
out= newFileOutputStream(filename);
wb.write(out);
out.close();
System.out.println("done.");
}catch(FileNotFoundException e) {
//TODOAuto-generated catch block
e.printStackTrace();
}catch(IOException e) {
//TODOAuto-generated catch block
e.printStackTrace();
}
}
}
publicstaticvoidreadExcel() {
FileworkbookFile = newFile("workbook.xls");
try{
FileInputStreamfis = newFileInputStream(workbookFile);
Workbookworkbook = WorkbookFactory.create(fis);
fis.close();
intn = workbook.getNumberOfSheets();
for(inti = 0; i < n; i++) {
System.out.println(workbook.getSheetName(i));
Sheetsheet = workbook.getSheetAt(i);
Iterator<Row>rows = sheet.rowIterator();
CellStyle style = null;
while(rows.hasNext()) {
Rowrow = rows.next();
for(Cell cell : row) {
style= cell.getCellStyle();
CellFormatcf = CellFormat.getInstance(style.getDataFormatString());
CellFormatResultcellResult = cf.apply(cell);
System.out.print(""+cellResult.text);
}
System.out.println();
}
}
}catch(InvalidFormatException e) {
//TODOAuto-generated catch block
e.printStackTrace();
}catch(IOException e) {
//TODOAuto-generated catch block
e.printStackTrace();
}
}
publicstaticvoidmain(String[] args) {
//TODOAuto-generated method stub
//excelTest();
readExcel();
}
}