poi读取ppt的例子
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poi读取ppt的例子相关的知识,希望对你有一定的参考价值。
poi读取ppt的例子,不只是读取ppt里面的文字,还要又文字的样式,布局,图片等!网上的资料大都是只读取文字,它的样式,布局都读不出来!解决再追加200分
“jerry_shen_sjf”这位兄台给的答案,上面那点是给输出了ppt中的图片和输出了ppt中的文字,并没有得到我想要的结果,下面那些都是往ppt中加东西了,我想要的结果是把ppt中的一张一张幻灯片读出来,读出来的格式是‘.jpg .gif .bmp等‘格式的文件。以图片的形式读出
http://poi.apache.org/slideshow/quick-guide.html
文档格式
http://poi.apache.org/slideshow/ppt-file-format.html
操作Shape的API
http://poi.apache.org/slideshow/how-to-shapes.html
How to work with pictures
Currently, HSLF API supports the following types of pictures:
* Windows Metafiles (WMF)
* Enhanced Metafiles (EMF)
* JPEG Interchange Format
* Portable Network Graphics (PNG)
* Macintosh PICT
SlideShow ppt = new SlideShow(new HSLFSlideShow("slideshow.ppt"));
//extract all pictures contained in the presentation
PictureData[] pdata = ppt.getPictureData();
for (int i = 0; i < pdata.length; i++)
PictureData pict = pdata[i];
// picture data
byte[] data = pict.getData();
int type = pict.getType();
String ext;
switch (type)
case Picture.JPEG: ext=".jpg"; break;
case Picture.PNG: ext=".png"; break;
case Picture.WMF: ext=".wmf"; break;
case Picture.EMF: ext=".emf"; break;
case Picture.PICT: ext=".pict"; break;
default: continue;
FileOutputStream out = new FileOutputStream("pict_"+i + ext);
out.write(data);
out.close();
// add a new picture to this slideshow and insert it in a new slide
int idx = ppt.addPicture(new File("clock.jpg"), Picture.JPEG);
Picture pict = new Picture(idx);
//set image position in the slide
pict.setAnchor(new java.awt.Rectangle(100, 100, 300, 200));
Slide slide = ppt.createSlide();
slide.addShape(pict);
//now retrieve pictures containes in the first slide and save them on disk
slide = ppt.getSlides()[0];
Shape[] sh = slide.getShapes();
for (int i = 0; i < sh.length; i++)
if (sh[i] instanceof Picture)
Picture pict = (Picture)sh[i];
PictureData pictData = pict.getPictureData();
byte[] data = pictData.getData();
int type = pictData.getType();
if (type == Picture.JPEG)
FileOutputStream out = new FileOutputStream("slide0_"+i+".jpg");
out.write(data);
out.close();
else if (type == Picture.PNG)
FileOutputStream out = new FileOutputStream("slide0_"+i+".png");
out.write(data);
out.close();
FileOutputStream out = new FileOutputStream("slideshow.ppt");
ppt.write(out);
out.close();
How to set slide title
SlideShow ppt = new SlideShow();
Slide slide = ppt.createSlide();
TextBox title = slide.addTitle();
title.setText("Hello, World!");
//save changes
FileOutputStream out = new FileOutputStream("slideshow.ppt");
ppt.write(out);
out.close();
Below is the equivalent code in PowerPoint VBA:
Set myDocument = ActivePresentation.Slides(1)
myDocument.Shapes.AddTitle.TextFrame.TextRange.Text = "Hello, World!"
How to modify background of a slide master
SlideShow ppt = new SlideShow();
SlideMaster master = ppt.getSlidesMasters()[0];
Fill fill = master.getBackground().getFill();
int idx = ppt.addPicture(new File("background.png"), Picture.PNG);
fill.setFillType(Fill.FILL_PICTURE);
fill.setPictureData(idx);
How to modify background of a slide
SlideShow ppt = new SlideShow();
Slide slide = ppt.createSlide();
//This slide has its own background.
//Without this line it will use master's background.
slide.setFollowMasterBackground(false);
Fill fill = slide.getBackground().getFill();
int idx = ppt.addPicture(new File("background.png"), Picture.PNG);
fill.setFillType(Fill.FILL_PATTERN);
fill.setPictureData(idx);
How to modify background of a shape
SlideShow ppt = new SlideShow();
Slide slide = ppt.createSlide();
Shape shape = new AutoShape(ShapeTypes.Rectangle);
shape.setAnchor(new java.awt.Rectangle(100, 100, 200, 200));
Fill fill = shape.getFill();
fill.setFillType(Fill.FILL_SHADE);
fill.setBackgroundColor(Color.red);
fill.setForegroundColor(Color.green);
slide.addShape(shape);
How to create bulleted lists
SlideShow ppt = new SlideShow();
Slide slide = ppt.createSlide();
TextBox shape = new TextBox();
RichTextRun rt = shape.getTextRun().getRichTextRuns()[0];
shape.setText(
"January\r" +
"February\r" +
"March\r" +
"April");
rt.setFontSize(42);
rt.setBullet(true);
rt.setBulletOffset(0); //bullet offset
rt.setTextOffset(50); //text offset (should be greater than bullet offset)
rt.setBulletChar('\u263A'); //bullet character
slide.addShape(shape);
shape.setAnchor(new java.awt.Rectangle(50, 50, 500, 300)); //position of the text box in the slide
slide.addShape(shape);
FileOutputStream out = new FileOutputStream("bullets.ppt");
ppt.write(out);
out.close();
How to read hyperlinks from a slide show
FileInputStream is = new FileInputStream("slideshow.ppt");
SlideShow ppt = new SlideShow(is);
is.close();
Slide[] slide = ppt.getSlides();
for (int j = 0; j < slide.length; j++)
//read hyperlinks from the text runs
TextRun[] txt = slide[j].getTextRuns();
for (int k = 0; k < txt.length; k++)
String text = txt[k].getText();
Hyperlink[] links = txt[k].getHyperlinks();
if(links != null) for (int l = 0; l < links.length; l++)
Hyperlink link = links[l];
String title = link.getTitle();
String address = link.getAddress();
String substring = text.substring(link.getStartIndex(), link.getEndIndex()-1); //in ppt end index is inclusive
//in PowerPoint you can assign a hyperlink to a shape without text,
//for example to a Line object. The code below demonstrates how to
//read such hyperlinks
Shape[] sh = slide[j].getShapes();
for (int k = 0; k < sh.length; k++)
Hyperlink link = sh[k].getHyperlink();
if(link != null)
String title = link.getTitle();
String address = link.getAddress();
How to create tables
//table data
String[][] data =
"INPUT FILE", "NUMBER OF RECORDS",
"Item File", "11,559",
"Vendor File", "300",
"Purchase History File", "10,000",
"Total # of requisitions", "10,200,038"
;
SlideShow ppt = new SlideShow();
Slide slide = ppt.createSlide();
//create a table of 5 rows and 2 columns
Table table = new Table(5, 2);
for (int i = 0; i < data.length; i++)
for (int j = 0; j < data[i].length; j++)
TableCell cell = table.getCell(i, j);
cell.setText(data[i][j]);
RichTextRun rt = cell.getTextRun().getRichTextRuns()[0];
rt.setFontName("Arial");
rt.setFontSize(10);
cell.setVerticalAlignment(TextBox.AnchorMiddle);
cell.setHorizontalAlignment(TextBox.AlignCenter);
//set table borders
Line border = table.createBorder();
border.setLineColor(Color.black);
border.setLineWidth(1.0);
table.setAllBorders(border);
//set width of the 1st column
table.setColumnWidth(0, 300);
//set width of the 2nd column
table.setColumnWidth(1, 150);
slide.addShape(table);
table.moveTo(100, 100);
FileOutputStream out = new FileOutputStream("hslf-table.ppt");
ppt.write(out);
out.close();
How to remove shapes from a slide
Shape[] shape = slide.getShapes();
for (int i = 0; i < shape.length; i++)
//remove the shape
boolean ok = slide.removeShape(shape[i]);
if(ok)
//the shape was removed. Do something.
How to retrieve embedded OLE objects
Shape[] shape = slide.getShapes();
for (int i = 0; i < shape.length; i++)
if (shape[i] instanceof OLEShape)
OLEShape ole = (OLEShape) shape[i];
ObjectData data = ole.getObjectData();
String name = ole.getInstanceName();
if ("Worksheet".equals(name))
HSSFWorkbook wb = new HSSFWorkbook(data.getData());
else if ("Document".equals(name))
HWPFDocument doc = new HWPFDocument(data.getData());
How to retrieve embedded sounds
FileInputStream is = new FileInputStream(args[0]);
SlideShow ppt = new SlideShow(is);
is.close();
SoundData[] sound = ppt.getSoundData();
for (int i = 0; i < sound.length; i++)
//save *WAV sounds on disk
if(sound[i].getSoundType().equals(".WAV"))
FileOutputStream out = new FileOutputStream(sound[i].getSoundName());
out.write(sound[i].getData());
out.close();
How to create shapes of arbitrary geometry
SlideShow ppt = new SlideShow();
Slide slide = ppt.createSlide();
java.awt.geom.GeneralPath path = new java.awt.geom.GeneralPath();
path.moveTo(100, 100);
path.lineTo(200, 100);
path.curveTo(50, 45, 134, 22, 78, 133);
path.curveTo(10, 45, 134, 56, 78, 100);
path.lineTo(100, 200);
path.closePath();
Freeform shape = new Freeform();
shape.setPath(path);
slide.addShape(shape); 参考技术A edrt
参考资料:rt
参考技术B 这个问题很复杂的,等我试下。。。我不知道poi是什么意思呢。java poi 写入Excel后读取公式值问题
我用poi 向excel中写入了一个值 之后也重置了公式
cell.setCellFormula(cell.getCellFormula);
但是读取出来的值是0.0 直接打开excel文件后公式才能正确计算
如果要换成jxl才能做到的话麻烦各个例子谢谢;
poi 用的 3.7
cell.setCellFormula("A1-B1-C1"); cell.setCellValue(123);
改公式和改值两种都试了 取值用的 cell.getNumbericCellValue();
读出来都是0.0 公式不能正常计算
2
3 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
4 import org.apache.poi.hssf.usermodel.HSSFSheet;
5 import org.apache.poi.hssf.usermodel.HSSFRow;
6 import org.apache.poi.hssf.usermodel.HSSFCell;
7
8 import java.io.*;
9
10 public class FormulaToString
11
12 /**
13 * @param args
14 */
15 public void fileInput() throws IOException
16
17 HSSFWorkbook hw = new HSSFWorkbook(new FileInputStream(
18 "d:/My Documents/Desktop/poi.xls"));
19 HSSFSheet hsheet = hw.getSheet("poi test");
20 HSSFRow hrow = hsheet.getRow(0);
21 HSSFCell hcell = hrow.getCell(0);
22 String cellValue = this.getCellValue(hcell);
23 System.out.println(cellValue);
24
25
26
27 public String getCellValue(HSSFCell cell)
28 String value = null;
29 if (cell != null)
30 switch (cell.getCellType())
31 case HSSFCell.CELL_TYPE_FORMULA:
32 // cell.getCellFormula();
33 try
34 value = String.valueOf(cell.getNumericCellValue());
35 catch (IllegalStateException e)
36 value = String.valueOf(cell.getRichStringCellValue());
37
38 break;
39 case HSSFCell.CELL_TYPE_NUMERIC:
40 value = String.valueOf(cell.getNumericCellValue());
41 break;
42 case HSSFCell.CELL_TYPE_STRING:
43 value = String.valueOf(cell.getRichStringCellValue());
44 break;
45
46
47
48 return value;
49
50
51 public static void main(String[] args)
52 try
53 // TODO Auto-generated method stub
54 FormulaToString fts = new FormulaToString();
55 fts.fileInput();
56 catch (IOException e)
57 e.printStackTrace();
58
59
60
61 参考技术B 你好,我之前一直在研究poi和jxl,应该可以帮助你
“但是读取出来的值是0.0”,你说的这句话,是不是说,你用poi写了excel后,再读取excel时发现那个值是0.0?
“直接打开excel文件后公式才能正确计算”,这句话是不是说,直接打开excel文件后,双击那个你设置公式了但取值为0.0的单元格后,它又能用公式计算出正确的结果?追问
是的 我用poi写入excel后 并且重新设置了公式
再读取那个值就是0.0 (没有重设置公式也是0.0)
直接打开excel文件后 可以看到那个单元格的是显示的正确结果 不需要双击
但是不保存的话 再用poi读取出来的值还是0.0
我明白了,完整过程是这样,你看对不对:
用poi在内存中创建了一个workbook(自始至终没有写成文件),然后在某个单元格设置了公式,然后直接读取这个workbook发现,那个单元格取值并没有按照公式来
这个我还没有试过呢,我一般读写都会通过已保存的文件来处理,我现在试试,马上告诉你结果
---------------------------------------------------------------------------------------------------------
你好,试过了,不保存的话得到的结果一直是0.0,看来非得保存后再读取。
或者可能的话,你既然知道公式,就当然知道是哪些值的计算结果,直接在内存中计算出来,不一定非得通过excel公式,也省下了保存excel再读取的麻烦
不是这样
目前的操作是直接修改的现有文件(后面也会直接生成文件,我暂时还没去试),
比如C1=A1+B1 A1=10 B1=20 直接读取的话C1=30
然后我用poi修改了A1的值变成20 再取读取C1发现值没有变成40而是0.0
修改值 和 修改 公式我都试过了 都不能得到正确答案
因为公式是不固定的,我把公式存在数据库中 这样只要在前台修改数据那就可以直接得到想要的值了
麻烦你 再看下 谢谢
“然后我用poi修改了A1的值变成20 再取读取C1”
这个过程,有没有保存文件?在写入excel时直接获值只能是0.0。
所以如果用poi修改了任何内容,都必须先保存写成文件,否则直接从内存中获值不会是想要的。
另外,我刚才试了下jxl,不保存文件直接在内存中修改再读取也不会得到想要的值,必须先保存文件
写入数据后就 workbook.write(fileout); fileout.flush();fileout.close();
然后另一个类 新建workbook来调用这个文件 操作都是java完成的
直接用excel来打开文件可以看到值已经写进去了 但是读出来的话是0.0
你算说对了,我试验的结果一样,真是bug,我解决了告诉你
本回答被提问者采纳 参考技术C sheet.setForceFormulaRecalculation(true);可以这样 强制使用公式 不需要保存! 参考技术D 我今天也遇到了这个问题,弄了半天,看API才找到了答案。在你写入Excel之前,刷新一下这个工作表的公式就OK了。
HSSFFormulaEvaluator.evaluateAllFormulaCells(HSSFWorkbook workbook);
以上是关于poi读取ppt的例子的主要内容,如果未能解决你的问题,请参考以下文章
java读取txt/pdf/xls/xlsx/doc/docx/ppt/pptx