POI操作Microsoft Office(Word、Excel、PowerPoint)全面了解

POI 与 Microsoft Office

1. POI 简介 

     POI 是 Apache 下的 Jakata 项目的一个子项目,主要用于提供 java 操作 Microsoft

Office 办公套件如 Excel,Word,Powerpoint 等文件的 API.

     微软的Office 办公软件在企业的日常办公中占据着重要的地位,人们已经非常熟悉

Office 的使用。在我们开发的应用系统中,常常需要将数据导出到 Excel 文件中,或者

Word 文件中进行打印。比如移动的话费查询系统中就提供了将话费清单导入到 excel 表

格中的功能。这样在web 应用中,我们在浏览器中看到的数据可以被导出到 Excel 中了。

  • Excel 文件: xls 格式文件对应 POI API 为 HSSF 。 xlsx 格式为 office 2007 的文件格式,POI 中对应的API 为XSSF
  • Word 文件:doc 格式文件对应的 POI API 为 HWPF。 docx 格式为 XWPF
  • powerPoint 文件:ppt 格式对应的 POI API 为 HSLF。 pptx 格式为 XSLF
  • outlook :对应的 API 为 HSMF
  • Visio: 对应的 API 为 HDGF
  • Publisher : 对应的 API 为 HPBF

下面主要介绍如何操作Excel。

2. 下载 POI

     到apache 官方网站下载POI 的jar 包

POI操作Microsoft Office(Word、Excel、PowerPoint)全面了解_第1张图片

POI操作Microsoft Office(Word、Excel、PowerPoint)全面了解_第2张图片

3. Excel 文件的结构

         一个Excel 文档称为工作簿(worksheet),一个工作簿包含多个工作表(sheet),

每个工作表看起来像一张二维表格,由很多行(row)组成,每行由多个单元格组成(cell).

下面是POI HSSF API 中的类与Excel 结构的对应关系:

POI操作Microsoft Office(Word、Excel、PowerPoint)全面了解_第3张图片

4. 创建空的 Excel 文件

      新建java项目,导入jar文件

POI操作Microsoft Office(Word、Excel、PowerPoint)全面了解_第4张图片

4.1 创建空的 xls 文件

POI操作Microsoft Office(Word、Excel、PowerPoint)全面了解_第5张图片

      创建的xls 文件用excel 打开的时候报错,因为这个 Excel 文件结构不完整。


 

4.2 创建空的 xlsx 文件

     xlsx 文件是 office2007 文件格式,这种文件格式是基于 xml 的,所以需要在项目中加

入xml 文件解析的jar 包。如图,jar包在 poi 发行包中能够找到

POI操作Microsoft Office(Word、Excel、PowerPoint)全面了解_第6张图片

POI操作Microsoft Office(Word、Excel、PowerPoint)全面了解_第7张图片

   同样道理,因为xlsx 文件信息不完整,所以用Excel 打开的时候出现错误

5. 创建工作表

      在前面创建的工作簿(WorkBook)的基础之上创建工作表

POI操作Microsoft Office(Word、Excel、PowerPoint)全面了解_第8张图片

    创建结果:

POI操作Microsoft Office(Word、Excel、PowerPoint)全面了解_第9张图片

6. 创建行数据

POI操作Microsoft Office(Word、Excel、PowerPoint)全面了解_第10张图片

执行后的结果:

POI操作Microsoft Office(Word、Excel、PowerPoint)全面了解_第11张图片

可以看到日期有一些问题。代码中使用的是”new Date()”,需要做一些转换。

POI操作Microsoft Office(Word、Excel、PowerPoint)全面了解_第12张图片

这个创建好的格式可以反复使用。

7. 单元格对其方式


 

HSSFCellStyle 中定义了一些对齐方式

为了方便操作,定义一个创建单元格的方法。

POI操作Microsoft Office(Word、Excel、PowerPoint)全面了解_第13张图片

调用它创建单元格:

POI操作Microsoft Office(Word、Excel、PowerPoint)全面了解_第14张图片

更多单元格,样式属性方法请参阅发行文档 API

8. 合并单元格

先设置好单元格,然后调用工作表的 addMergedRegion 方法合并单元格

POI操作Microsoft Office(Word、Excel、PowerPoint)全面了解_第15张图片

结果:

POI操作Microsoft Office(Word、Excel、PowerPoint)全面了解_第16张图片

                 Ok,大功告成!!

 

使用POI操作PPT文档

1)如果是创建新的PPT文档,直接使用SlideShow和Slide类就可以,其中SlideShow表示PPT文档,Slide表示某一张幻灯片
如下代码创建空的PPT文档:

SlideShow ppt = new SlideShow();
Slide[] slides = ppt.getSlides();
assertTrue(slides.length == 0);
savePPTFile(ppt);
 
private void savePPTFile(SlideShow ppt) throws Exception{
         FileOutputStream out = new FileOutputStream(\"ppt测试.ppt\");
 ppt.write(out);
 out.close();
}

2)设置母版,这样后续的新建幻灯片都将使用母版的字体,背景等设置

SlideShow ppt = new SlideShow();
//设置幻灯片大小
ppt.setPageSize(new Dimension(760,600));
SlideMaster master = ppt.getSlidesMasters()[0];       
//设置母板背景,支持多种图片格式
int picIndex = ppt.addPicture(new File(\"background.png\"), Picture.PNG);
Picture background = new Picture(picIndex);
//设置图片位置
background.setAnchor(new java.awt.Rectangle(0, 0, ppt.getPageSize().width
                                               , ppt.getPageSize().height));
master.addShape(background);

3)创建幻灯片并插入文本

SlideShow ppt = new SlideShow();
Slide newSlide = ppt.createSlide();
 
//添加幻灯片标题
TextBox title = newSlide.addTitle();
RichTextRun titleRun = title.getTextRun().getRichTextRuns()[0];
titleRun.setFontColor(Color.RED);
title.setText(\"ppt测试\");
 
//添加文本框
TextBox txt = new TextBox();
RichTextRun richTextRun = txt.getTextRun().getRichTextRuns()[0];
richTextRun.setFontColor(Color.BLUE);  
//setText参数字符串可以包含回车、换行符,但是最后一行不能以\\r\\n结尾,否则设置的格式没有效果(v3.5)
richTextRun.setText(\"这里可以换行\\r\\n第二行文本\");    
txt.setAnchor(new java.awt.Rectangle(50,150,400,400));
newSlide.addShape(txt);
 
savePPTFile(ppt);

4)插入图片,支持多种格式

SlideShow ppt = new SlideShow();
Slide newSlide = ppt.createSlide();
int picIndex = ppt.addPicture(new File(\"图片.jpg\"), Picture.JPEG);
Picture jpg = new Picture(picIndex);
 
//set image position in the slide
jpg.setAnchor(new java.awt.Rectangle(360, 200, 280, 260));
 
newSlide.addShape(jpg);
savePPTFile(ppt);

5)插入表格(v3.5)

SlideShow ppt = new SlideShow();
Slide slide = ppt.createSlide();
 
String[][] datas = {
    {\"序号\", \"姓名\",\"年龄\"},
    {\"1\", \"张三\",\"30\"},
    {\"2\", \"李四\",\"27\"},
};
 
//create a table of 3 rows and 3 columns
Table table = new Table(3, 3);
 
for (int i = 0; i < datas.length; i++) {
    for (int j = 0; j < datas[i].length; j++) {
        TableCell cell = table.getCell(i, j);
 
        RichTextRun rt = cell.getTextRun().getRichTextRuns()[0];
        rt.setFontName(\"宋体\");
        rt.setFontSize(12);
 
        cell.setVerticalAlignment(TextBox.AnchorMiddle);
        cell.setHorizontalAlignment(TextBox.AlignCenter);
        cell.setText(datas[i][j]);
 
        if(i == 0){//首行背景设置为灰色
             cell.setFillColor(Color.GRAY);
        }               
    }
}
 
Line border = table.createBorder();
border.setLineColor(Color.black);
border.setLineWidth(2.0);
table.setAllBorders(border); 
 
slide.addShape(table);
table.moveTo(160,260);
savePPTFile(ppt);

6)如果是读取已存在的PPT文档则还要用到HSLFSlideShow,下面代码将PPT文件导出为图片(png)格式,如果幻灯片上有中文字符则这些字符的字体需要修改为支持中文的字体(宋体等),否则导出的图片的中文字符不能正常显示

SlideShow ppt = new SlideShow(new HSLFSlideShow(\"PPT测试.ppt\"));
Dimension pgsize = ppt.getPageSize();
Slide[] slide = ppt.getSlides();
 
for (int i = 0; i < slide.length; i++) {
    BufferedImage img = new BufferedImage(pgsize.width, pgsize.height
                                               , BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = img.createGraphics();
    //clear the drawing area
    graphics.setPaint(Color.white);
    graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
 
    //render
    slide[i].draw(graphics);
 
    FileOutputStream out = new FileOutputStream(\"slide-\"  + (i+1) + \".png\");
    javax.imageio.ImageIO.write(img, \"png\", out);
    out.close();
}

7)提取PPT文档信息

SlideShow ppt = new SlideShow(new HSLFSlideShow(\"PPT测试.ppt\"));
Slide[] slides = ppt.getSlides();
//提取文本信息   
for (Slide each : slides) {
    System.out.println(each.getTitle()) ;
    TextRun[] textRuns = each.getTextRuns();
    for (int i=0 ;i< textRuns.length; i++ ) {
        System.out.println(textRuns[i].getText());
        RichTextRun[] richTextRuns = textRuns[i].getRichTextRuns();
        for (int j = 0; j < richTextRuns.length; j++) {
            System.out.println(richTextRuns[j].getText());
        }
    }
}
//提取所有JPEG图片
PictureData[] picDatas = ppt.getPictureData();
for (int i=0;i<picDatas.length;i++) {
    if(picDatas[i].getType() == Picture.JPEG){
        FileOutputStream out = new FileOutputStream(\"jpg_\" + i + \".jpg\");
        ppt.write(out);
        out.close();
    }
}

8)设置PPT文档摘要信息(文档点击鼠标右键查看属性)

HSLFSlideShow hslf = HSLFSlideShow.create();
DocumentSummaryInformation dsi= hslf.getDocumentSummaryInformation();   
SummaryInformation si= hslf.getSummaryInformation();   
 
dsi.setCompany(\"yourCompany\");  
dsi.setCategory(\"ppt测试\");   
si.setAuthor(\"yourName\");   
si.setTitle(\"标题\");  
 
SlideShow ppt = new SlideShow(hslf);
savePPTFile(ppt);

 

POI处理Word、Excel、PowerPoint 简单例子

第一:下载POI,在http://jakarta.apache.org/poi/中,下载poi-bin-3.5-beta4-20081128.zip,解压后把jar包引入项目工程。

 

第二:处理Word(Word.java)

import org.apache.poi.hwpf.extractor.WordExtractor;

import java.io.File;

import java.io.InputStream;

 

publicclass Word {

    publicstaticvoid main(String[] args)throws Exception {

       System.out.println(getContent("c://11.doc"));

    }

 

    publicstatic String getContent(String s)throws Exception {

       returngetContent(new java.io.FileInputStream(s));

    }

 

    publicstatic String getContent(File f)throws Exception {

       returngetContent(new java.io.FileInputStream(f));

    }

 

    publicstatic String getContent(InputStream is)throws Exception {

       String bodyText = null;

       WordExtractor ex = new WordExtractor(is);

       bodyText = ex.getText();

       return bodyText;

    }

}

 

 

第三:处理Excel(Excel.java)

import org.apache.poi.hssf.usermodel.HSSFDateUtil;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFCell;

import java.io.File;

import java.io.InputStream;

import java.text.SimpleDateFormat;

import java.util.Date;

 

publicclassExcel {

    publicstaticvoid main(String[] args)throws Exception {

       System.out.println(getContent("c://22.xls"));

    }

 

    publicstatic String getContent(String s)throws Exception {

       returngetContent(new java.io.FileInputStream(s));

    }

 

    publicstatic String getContent(File f)throws Exception {

       returngetContent(new java.io.FileInputStream(f));

    }

 

    publicstatic String getContent(InputStream is)throws Exception {

       StringBuffer content = new StringBuffer();

       HSSFWorkbook workbook = new HSSFWorkbook(is);

       for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) {

           HSSFSheet aSheet = workbook.getSheetAt(numSheets);//获得一个sheet

           content.append("/n");

           if (null == aSheet) {

              continue;

           }

           for (int rowNum = 0; rowNum <= aSheet.getLastRowNum(); rowNum++) {

              content.append("/n");

              HSSFRow aRow = aSheet.getRow(rowNum);

              if (null == aRow) {

                  continue;

              }

              for (short cellNum = 0; cellNum <= aRow.getLastCellNum(); cellNum++) {

 

                  HSSFCell aCell = aRow.getCell(cellNum);

                  if (null == aCell) {

                     continue;

                  }

                  if (aCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {

                     content.append(aCell.getRichStringCellValue()

                            .getString());

                  } elseif (aCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {

                     boolean b = HSSFDateUtil.isCellDateFormatted(aCell);

                     if (b) {

                         Date date = aCell.getDateCellValue();

                         SimpleDateFormat df =new SimpleDateFormat(

                                "yyyy-MM-dd");

                         content.append(df.format(date));

                     }

                  }

              }

           }

       }

       return content.toString();

    }

}

 

 

第四:处理PowerPoint(PowerPoint.java)

import java.io.File;

import java.io.InputStream;

import org.apache.poi.hslf.HSLFSlideShow;

import org.apache.poi.hslf.model.TextRun;

import org.apache.poi.hslf.model.Slide;

import org.apache.poi.hslf.usermodel.SlideShow;

 

publicclassPowerPoint {

    publicstaticvoid main(String[] args)throws Exception {

       System.out.println(getContent("c://33.ppt"));

    }

 

    publicstatic String getContent(String s)throws Exception {

       returngetContent(new java.io.FileInputStream(s));

    }

 

    publicstatic String getContent(File f)throws Exception {

       returngetContent(new java.io.FileInputStream(f));

    }

 

    publicstatic String getContent(InputStream is)throws Exception {

       StringBuffer content = new StringBuffer("");

       SlideShow ss = new SlideShow(new HSLFSlideShow(is));

       Slide[] slides = ss.getSlides();

       for (int i = 0; i < slides.length; i++) {

           TextRun[] t = slides[i].getTextRuns();

           for (int j = 0; j < t.length; j++) {

              content.append(t[j].getText());

           }

           content.append(slides[i].getTitle());

       }

       return content.toString();

    }

}

 

 

 

java 读取 2003 ppt 和 2007 pptx

import java.io.IOException;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.xslf.XSLFSlideShow;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.drawingml.x2006.main.CTRegularTextRun;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph;
import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;
import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlide;

public class PptReader {

    /**
     * @param args
     */
    public static String getTextFromPPT2003(String path) {

        StringBuffer content = new StringBuffer("");
        try {

            SlideShow ss = new SlideShow(new HSLFSlideShow(path));// path为文件的全路径名称,建立SlideShow
            Slide[] slides = ss.getSlides();// 获得每一张幻灯片
            for (int i = 0; i < slides.length; i++) {
                TextRun[] t = slides[i].getTextRuns();// 为了取得幻灯片的文字内容,建立TextRun
                for (int j = 0; j < t.length; j++) {
                    content.append(t[j].getText());// 这里会将文字内容加到content中去
                }
                content.append(slides[i].getTitle());
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return content.toString();

    }

    public static String getTextFromPPT2007(String path) {
        XSLFSlideShow slideShow;
        String reusltString=null;
        try {
            slideShow = new XSLFSlideShow(path);
            XMLSlideShow xmlSlideShow = new XMLSlideShow(slideShow);
            XSLFSlide[] slides = xmlSlideShow.getSlides();
            StringBuilder sb = new StringBuilder();
            for (XSLFSlide slide : slides) {
                CTSlide rawSlide = slide._getCTSlide();
                CTGroupShape gs = rawSlide.getCSld().getSpTree();
                CTShape[] shapes = gs.getSpArray();
                for (CTShape shape : shapes) {
                    CTTextBody tb = shape.getTxBody();
                    if (null == tb)
                        continue;
                    CTTextParagraph[] paras = tb.getPArray();
                    for (CTTextParagraph textParagraph : paras) {
                        CTRegularTextRun[] textRuns = textParagraph.getRArray();
                        for (CTRegularTextRun textRun : textRuns) {
                            sb.append(textRun.getT());
                        }
                    }
                }
            }
        reusltString=sb.toString();
        } catch (OpenXML4JException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (XmlException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return reusltString;
    }

    public static void main(String[] args) {
        System.out.println(PptReader.getTextFromPPT2003("c:/test.ppt"));
        System.out.println(PptReader.getTextFromPPT2007("c:/test.pptx"));
    }

}

 

 

 

excel 单元格字符数限制(from microsoft support)

 

Microsoft Excel 版本中,一个单元格可包含多达 32,767 个字符。但是,如果某单元格中包含的字符多于 1,024 个,则应遵循以下规则:

  • 大约第 1,024 个字符之后的字符不显示在单元格中;不过,在编辑或选择该单元格时,它们会出现在编辑栏中。
  • 尽管不会显示大约第 1,024 个字符之后的字符,但可以使用工作表函数(例如 RIGHT 和 MID 函数)和宏命令(例如“字符”属性)检测并操作这些字符。
  • 如果复制包含 1,024 个以上字符的单元格,然后将其粘贴到另一单元格中,则会将所有字符粘贴到新的单元格中。不过,大约第 1,024 个字符之后的字符不会在目标单元格中显示。
  • 无法打印单元格中大约第 1,024 个字符之后的字符,且这些字符也不会在打印预览中显示。

注意 :增加工作表行高和列宽,或修改系统的显示设置后,可以看到的字符数将多于 1,024 个。


示例

要了解此问题,请按照下列步骤操作:

  1. 在新工作表的单元格 A1 中,键入以下公式:
    =REPT("w",1024)&"xyz"
  2. 选中单元格 A1,单击“格式”菜单上的“单元格”。
  3. 单击“对齐”选项卡。单击以选中“自动换行”复选框,然后单击“确定”。
  4. 在“格式”菜单上,指向“列”,然后单击“最适合的列宽”。

    请注意,您只能在单元格 A1 中看到“w”字符,该单元格结尾处的“xyz”字符不会显示。
  5. 在单元格 A2 和 A3 中键入以下公式:
    A2:=RIGHT(A1,3)
    A3:=LEN(A1)
    单元格 A2 中的公式返回结果“xyz”,即单元格最右侧的三个字符。单元格 A3 中包含数字 1027,即单元格 A1 中的字符数。
  6. 将单元格 A1 中的公式更改为:
    =REPT("w",1023)&"xyz"

您可以看到 1,023 个“w”字符及其后的“x”字符。因为存在 1,024 个字符的限制,所以不会显示“yz”字符。单元格 A3 中的公式现在显示 1026,即单元格 A1 的长度。

 

Arguments in a function: 30
Length of formula contents: 1,024 characters
Nested levels of functions: 7 (although can be extended using nested-nested functions)
Number of available worksheet functions: 329 (and a few more in VBA)
Decimal precision: 15
Largest number allowed to be typed into a cell: 1.00E+308
Largest allowed positive number: 1.79769313486231E308
Smallest allowed negative number: 0.00E-01
Smallest allowed positive number: 2.23E-308
Largest allowed negative number: -1.00E-307
Iterations: 32,767
Worksheet arrays: Limited by available memory. Also, arrays cannot refer to entire columns. For example, an array cannot refer to the entire column C:C or to the range C1:C65536. However, an array can refer to the range C1: D65535 because the range is one row short of the maximum worksheet size and does not include the entire C or D column.

Selected ranges 2,048

Charts linked to a worksheet: Limited by available memory
Worksheets referred to by a chart: 255
Data series in one chart: 255
Data points in a data series for 2-D charts: 32,000
Data points in a data series for 3-D charts: 4,000
Data points for all data series in one chart: 256,000
Line styles: 8
Line weights: 4
Area patterns (screen display): 18
Total area pattern and color combinations (color display): 56,448
Pattern and color combinations (color printer): 56,448 (the actual number depends on your printer and its software)
Page fields in a PivotChart report: 256 (may be limited by available memory)
Data fields in a PivotChart report: 256
Calculated item formulas in a PivotChart report: Limited by available memory

Feature Maximum limit
Open workbooks : Limited by available memory and system resources
Worksheet size : 65,536 rows by 256 columns
Column width : 255 characters
Row height : 409 points
Length of cell contents (text) : 32,767 characters. Only 1,024 display in a cell; all 32,767 display in the formula bar.
Sheets in a workbook : Limited by available memory (default is 3 sheets)
Colors in a workbook : 56
Cell styles in a workbook : 4,000
Named views in a workbook : Limited by available memory
Custom number formats : Limited by available memory
Names in a workbook : Limited by available memory
Windows in a workbook : Limited only by system resources
Panes in a window : 4
Linked sheets : Limited by available memory
Scenarios : Limited by available memory; a summary report shows only the first 251 scenarios
Changing cells in a scenario : 32
Adjustable cells in Solver : 200
Custom functions : Limited by available memory
Zoom range : 10 percent to 400 percent
Reports : Limited by available memory
Sort references : 3 in a single sort; unlimited when using sequential sorts
Undo levels : 16
Fields in a data form : 32
Custom toolbars in a workbook : Limited by available memory
Custom toolbar buttons : Limited by available memory

 

 

POI讀取Excel2003中的圖片

转自:http://blog.csdn.net/Dean_Deng/article/details/6749418
//poi讀取Excel中的圖片
public class ReadExcelPicture{
 public ReadExcelPicture(){
 }
 public Map readPicture(String excelPath)throws InvalidFormatException, IOException {

   FileInputStream fis = new FileInputStream(excelPath);
   HSSFWorkbook workbook = (HSSFWorkbook) WorkbookFactory.create(fis);
   List pictures = workbook.getAllPictures();
   //假設讀取的Excel工作薄中的第一張表
   HSSFSheet sheet = workbook.getSheet("0");

   Map map = new HashMap();
   for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) {
    HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor();
    int rowIndex = anchor.getRow1();
    if (shape instanceof HSSFPicture) {
     int rowmark = rowIndex;
     picture = (HSSFPicture) shape;
     int pictureIndex = picture.getPictureIndex() - 1;
     pictureData = pictures.get(pictureIndex);
     map.put(rowmark, pictureData);
    }
  }
  return map;
 }
 public static void main(String args[]){

  String excelPath = "D:\\Excel\\test.xls";
  String savePicturePath = "D:\\images\\";
  ReadExcelPicture readExcelPicture = new ReadExcelPicture();
  Map map = readExcelPicture.readPicture(excelPath);
  //傳入一個你需要的Excel圖片行索引,必須確保該Excel行索引中有圖片,
  //而且是在已經讀取的Excel行索引范圍內,我傳入的是1
  HSSFPictureData pictureData = map.get(1);
  //獲取包含圖片格式的文件字符串
  String ext = pictureData.suggestFileExtension();
  //代表圖片信息的字節數據
  byte[] data = pictureData.getData();
  //根據圖片格式將圖片寫出到磁盤
  if (ext.equals("jpeg")) {
   FileOutputStream out = new FileOutputStream(savePicturePath+"a.jpg");
   out.write(data);
   out.close();
  }
  if (ext.equals("png")) {
   FileOutputStream out = new FileOutputStream(savePicturePath+"a.png");
   out.write(data);
   out.close();
  }
 }
}

 

java POI实现向Excel2003中插入图片

转自:http://blog.csdn.net/chenssy/article/details/20524563
做Web开发免不了要与Excel打交道。今天老大给我一个任务-导出Excel。开始想的还是蛮简单的,无非就是查找,构建Excel,response下载即可。但是有一点不同,就是要加入图片,就是这个加入图片搞了好久。同时网络上确实没有发现比较好的资料,所以写这篇博文记录之,供自己和博友们查询,参考。

       在POI中有HSSFPatriarch对象,该对象为画图的顶级管理器,它的createPicture(anchor, pictureIndex)方法就能够在Excel插入一张图片。所以要在Excel中插入图片,三步就可以搞定。一、获取HSSFPatriarch对象,二、new HSSFClientAnchor对象,三、调用createPicture方法即可。实现倒是非常容易实现,如果想把它做好还是有点儿难度的。这里我们先插入一张图片:

public class ExcelImageTest {
    public static void main(String[] args) {
         FileOutputStream fileOut = null;  
         BufferedImage bufferImg = null;  
        //先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray 
        try {
            ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();  
            bufferImg = ImageIO.read(new File("F:/图片/照片/无名氏/小昭11.jpg"));  
            ImageIO.write(bufferImg, "jpg", byteArrayOut);
           
            HSSFWorkbook wb = new HSSFWorkbook();  
            HSSFSheet sheet1 = wb.createSheet("test picture"); 
            //画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
            HSSFPatriarch patriarch = sheet1.createDrawingPatriarch();  
            //anchor主要用于设置图片的属性
            HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 255, 255,(short) 1, 1, (short) 5, 8);  
            anchor.setAnchorType(3);  
            //插入图片 
            patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
            fileOut = new FileOutputStream("D:/测试Excel.xls");  
            // 写入excel文件  
             wb.write(fileOut);  
             System.out.println("----Excle文件已生成------");
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(fileOut != null){
                 try {
                    fileOut.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

 如下为执行后的结果:

POI操作Microsoft Office(Word、Excel、PowerPoint)全面了解_第17张图片

      至于为什么会是这样的结果,主要是因为HSSFClientAnchor(0, 0, 255, 255,(short) 1, 1, (short) 5, 8)这个构造函数造成的,下面我就来解释这个构造函数:HSSFClientAnchor(int dx1,int dy1,int dx2,int dy2,short col1,int row1,short col2, int row2);各个参数的含义如下:

      dx1:the x coordinate within the first cell。

      dy1:the y coordinate within the first cell。

      dx2:the x coordinate within the second cell。

      dy2:the y coordinate within the second cell。

      col1:the column (0 based) of the first cell。

      row1:the row (0 based) of the first cell。

      col2:the column (0 based) of the second cell。

      row2:the row (0 based) of the second cell。

      这里dx1、dy1定义了该图片在开始cell的起始位置,dx2、dy2定义了在终cell的结束位置。col1、row1定义了开始cell、col2、row2定义了结束cell。

POI操作Microsoft Office(Word、Excel、PowerPoint)全面了解_第18张图片

      下面是有两个不同的构造函数所创建的,从这幅图中我们可以清晰看到上面八个参数的含义和不同之处。

POI操作Microsoft Office(Word、Excel、PowerPoint)全面了解_第19张图片

      上面是插入一张图片,那么实现插入多张图片呢?其实很简单,构造多个不同的HSSFClientAnchor对象,控制好那八个参数,如下:

HSSFClientAnchor anchor1 = new HSSFClientAnchor(0, 0, 1023,100,(short) 1, 1, (short)5, 8); 
            HSSFClientAnchor anchor2 = new HSSFClientAnchor(0, 0, 1023,100,(short) 1, 9, (short)5, 16); 
            
            //插入图片  
            patriarch.createPicture(anchor1, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG)); 
            patriarch.createPicture(anchor2, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));

      其余代码一样,得到如下结果:

POI操作Microsoft Office(Word、Excel、PowerPoint)全面了解_第20张图片

      下篇我将提供一个Excel生成的通用模板,支持自定样式、标题、写入图片等!!

 

利用POI获取Excel2003/2007中图片和图片位置

转自:http://blog.csdn.net/delongcpp/article/details/8833995 
第三方JAR包(apache下载POI即可):


poi-3.9-20121203.jar
dom4j-1.6.1.jar
poi-ooxml-3.9-20121203.jar
poi-ooxml-schemas-3.9-20121203.jar
poi-scratchpad-3.9-20121203.jar
stax-api-1.0.1.jar
xmlbeans-2.3.0.jar
测试代码:


package com;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPicture;
import org.apache.poi.hssf.usermodel.HSSFPictureData;
import org.apache.poi.hssf.usermodel.HSSFShape;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.PictureData;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFPicture;
import org.apache.poi.xssf.usermodel.XSSFShape;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTMarker;


/**
 * @since 2013-04-22
 
 * @author Gerrard
 
 * 获取excel中 图片,并得到图片位置,支持03 07 多sheet
 
 */
public class GetImgFromExcel {


    /**
     * @param args
     * @throws IOException 
     * @throws InvalidFormatException 
     */
    public static void main(String[] args) throws InvalidFormatException, IOException {
        
        // 创建文件
        File file = new File("model/test.xls");
        
        // 创建流
        InputStream input = new FileInputStream(file);
        
        // 获取文件后缀名
        String fileExt =  file.getName().substring(file.getName().lastIndexOf(".") + 1);
        
        // 创建Workbook
        Workbook wb = null;
        
        // 创建sheet
        Sheet sheet = null;


        //根据后缀判断excel 2003 or 2007+
        if (fileExt.equals("xls")) {
            wb = (HSSFWorkbook) WorkbookFactory.create(input);
        } else {
            wb = new XSSFWorkbook(input);
        }
        
        //获取excel sheet总数
        int sheetNumbers = wb.getNumberOfSheets();
        
        // sheet list
        List> sheetList = new ArrayList>();
        
        // 循环sheet
        for (int i = 0; i < sheetNumbers; i++) {
            
            sheet = wb.getSheetAt(i);
            // map等待存储excel图片
            Map sheetIndexPicMap; 
            
            // 判断用07还是03的方法获取图片
            if (fileExt.equals("xls")) {
                sheetIndexPicMap = getSheetPictrues03(i, (HSSFSheet) sheet, (HSSFWorkbook) wb);
            } else {
                sheetIndexPicMap = getSheetPictrues07(i, (XSSFSheet) sheet, (XSSFWorkbook) wb);
            }
            // 将当前sheet图片map存入list
            sheetList.add(sheetIndexPicMap);
        }
        
        printImg(sheetList);


    }
    
    /**
     * 获取Excel2003图片
     * @param sheetNum 当前sheet编号
     * @param sheet 当前sheet对象
     * @param workbook 工作簿对象
     * @return Map key:图片单元格索引(0_1_1)String,value:图片流PictureData
     * @throws IOException
     */
    public static Map getSheetPictrues03(int sheetNum,
            HSSFSheet sheet, HSSFWorkbook workbook) {


        Map sheetIndexPicMap = new HashMap();
        List pictures = workbook.getAllPictures();
        if (pictures.size() != 0) {
            for (HSSFShape shape : sheet.getDrawingPatriarch().getChildren()) {
                HSSFClientAnchor anchor = (HSSFClientAnchor) shape.getAnchor();
                if (shape instanceof HSSFPicture) {
                    HSSFPicture pic = (HSSFPicture) shape;
                    int pictureIndex = pic.getPictureIndex() - 1;
                    HSSFPictureData picData = pictures.get(pictureIndex);
                    String picIndex = String.valueOf(sheetNum) + "_"
                            + String.valueOf(anchor.getRow1()) + "_"
                            + String.valueOf(anchor.getCol1());
                    sheetIndexPicMap.put(picIndex, picData);
                }
            }
            return sheetIndexPicMap;
        } else {
            return null;
        }
    }


    /**
     * 获取Excel2007图片
     * @param sheetNum 当前sheet编号
     * @param sheet 当前sheet对象
     * @param workbook 工作簿对象
     * @return Map key:图片单元格索引(0_1_1)String,value:图片流PictureData
     */
    public static Map getSheetPictrues07(int sheetNum,
            XSSFSheet sheet, XSSFWorkbook workbook) {
        Map sheetIndexPicMap = new HashMap();


        for (POIXMLDocumentPart dr : sheet.getRelations()) {
            if (dr instanceof XSSFDrawing) {
                XSSFDrawing drawing = (XSSFDrawing) dr;
                List shapes = drawing.getShapes();
                for (XSSFShape shape : shapes) {
                    XSSFPicture pic = (XSSFPicture) shape;
                    XSSFClientAnchor anchor = pic.getPreferredSize();
                    CTMarker ctMarker = anchor.getFrom();
                    String picIndex = String.valueOf(sheetNum) + "_"
                            + ctMarker.getRow() + "_" + ctMarker.getCol();
                    sheetIndexPicMap.put(picIndex, pic.getPictureData());
                }
            }
        }


        return sheetIndexPicMap;
    }

 

注意:POI3.9/3.10 EXCEL2007读取图片方法有差异

POI3.9
for (POIXMLDocumentPart dr : sheet.getRelations()) {
            if (dr instanceof XSSFDrawing) {
                XSSFDrawing drawing = (XSSFDrawing) dr;
                List shapes = drawing.getShapes();
                for (XSSFShape shape : shapes) {
                    XSSFPicture pic = (XSSFPicture) shape;
                    //anchor可以得到图片的起始位置.
                    XSSFClientAnchor anchor = pic.getPreferredSize();
                    CTMarker ctMarker = anchor.getFrom();
                    String picIndex = String.valueOf(sheetNum) + "_"
                            + ctMarker.getRow() + "_" + ctMarker.getCol();
                    sheetIndexPicMap.put(picIndex, pic.getPictureData());
                }
            }else if(dr instanceof XSSFVMLDrawing){//emf图片会进入这个分支
                XSSFVMLDrawing drawing = (XSSFVMLDrawing) dr;
                List shapes = drawing.getRelations();
                for (POIXMLDocumentPart shape : shapes) {
                    if(shape instanceof XSSFPictureData){
                        XSSFPictureData xpd = (XSSFPictureData)shape;
                        //可以得到护展名,但没办法得到图片的起始位置.
                        xpd.suggestFileExtension();
                    }
                                      
                }

            }

        }

POI3.10
for (POIXMLDocumentPart dr : sheet.getRelations()) {
            if (dr instanceof XSSFDrawing) {
                XSSFDrawing drawing = (XSSFDrawing) dr;
                //List shapes = drawing.getShapes();//此方法3.9版本出现空指针.
                List shapes = drawing.getRelations();
                for (POIXMLDocumentPart shape : shapes) {
                    if(shape instanceof XSSFPictureData){
                        XSSFPictureData xpd = (XSSFPictureData)shape;
                        //可以得到护展名,但没办法得到图片的起始位置.
                        xpd.suggestFileExtension();
                    }
                                      
                }
            }

 }

 

    
    public static void printImg(List> sheetList) throws IOException {
        
        for (Map map : sheetList) {
            Object key[] = map.keySet().toArray();
            for (int i = 0; i < map.size(); i++) {
                // 获取图片流
                PictureData pic = map.get(key[i]);
                // 获取图片索引
                String picName = key[i].toString();
                // 获取图片格式
                String ext = pic.suggestFileExtension();
                
                byte[] data = pic.getData();
                
                FileOutputStream out = new FileOutputStream("D:\\pic" + picName + "." + ext);
                out.write(data);
                out.close();
            }
        }
        
    }
  
}





POI插入EXCEL2007图片



转自:http://blog.csdn.net/ggcaozheng/article/details/8135890
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;


import javax.imageio.ImageIO;


import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class TestPOI {
    public static void main(String[] args) {
        FileOutputStream fileOut = null;
        BufferedImage bufferImg = null;
        BufferedImage bufferImg1 = null;
        try {


            // 先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
            ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
            ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream();
            // 读入两幅图片,作比较,看效果
            bufferImg = ImageIO.read(new File("d:/1.jpg"));
            bufferImg1 = ImageIO.read(new File("d:/2.jpg"));
            
            // static boolean write(RenderedImage im, String formatName, OutputStream output)
            // 使用支持给定格式的任意 ImageWriter 将一个图像写入 OutputStream。
            ImageIO.write(bufferImg, "jpg", byteArrayOut);
            ImageIO.write(bufferImg1, "jpg", byteArrayOut1);


            // 创建一个工作薄
            Workbook wb = new XSSFWorkbook();
            Sheet sheet1 = wb.createSheet("new sheet");
            
            
            Drawing patriarch = sheet1.createDrawingPatriarch();
            ClientAnchor anchor = new XSSFClientAnchor(0, 0, 512, 255, (short) 1, 1, (short) 10, 20);
            ClientAnchor anchor1 = new XSSFClientAnchor(0, 0, 512, 255, (short) 2, 30, (short) 10, 60);
            anchor1.setAnchorType(2);
            // 插入图片
            patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
            patriarch.createPicture(anchor1, wb.addPicture(byteArrayOut1.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));


            // 创建一个Excle文档
            fileOut = new FileOutputStream("d:/workbook.xlsx");
            // 将图片写入Excle文件
            wb.write(fileOut);
            fileOut.close();


        } catch (IOException io) {
            io.printStackTrace();
            System.out.println("io erorr : " + io.getMessage());
        } finally {
            if (fileOut != null) {


                try {
                    fileOut.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
 

你可能感兴趣的:(POI操作Microsoft Office(Word、Excel、PowerPoint)全面了解)