1)若是是建立新的PPT文檔,直接使用SlideShow和Slide類就能夠,其中SlideShow表示PPT文檔,Slide表示某一張幻燈片
以下代碼建立空的PPT文檔:html
1 SlideShow ppt = new SlideShow(); 2 Slide[] slides = ppt.getSlides(); 3 assertTrue(slides.length == 0); 4 savePPTFile(ppt); 5 6 private void savePPTFile(SlideShow ppt) throws Exception{ 7 FileOutputStream out = new FileOutputStream("ppt測試.ppt"); 8 ppt.write(out); 9 out.close(); 10 }
2)設置母版,這樣後續的新建幻燈片都將使用母版的字體,背景等設置java
1 SlideShow ppt = new SlideShow(); 2 //設置幻燈片大小 3 ppt.setPageSize(new Dimension(760,600)); 4 SlideMaster master = ppt.getSlidesMasters()[0]; 5 //設置母板背景,支持多種圖片格式 6 int picIndex = ppt.addPicture(new File("background.png"), Picture.PNG); 7 Picture background = new Picture(picIndex); 8 //設置圖片位置 9 background.setAnchor(new java.awt.Rectangle(0, 0, ppt.getPageSize().width 10 , ppt.getPageSize().height)); 11 master.addShape(background);
3)建立幻燈片並插入文本web
1 SlideShow ppt = new SlideShow(); 2 Slide newSlide = ppt.createSlide(); 3 4 //添加幻燈片標題 5 TextBox title = newSlide.addTitle(); 6 RichTextRun titleRun = title.getTextRun().getRichTextRuns()[0]; 7 titleRun.setFontColor(Color.RED); 8 title.setText("ppt測試"); 9 10 //添加文本框 11 TextBox txt = new TextBox(); 12 RichTextRun richTextRun = txt.getTextRun().getRichTextRuns()[0]; 13 richTextRun.setFontColor(Color.BLUE); 14 //setText參數字符串能夠包含回車、換行符,可是最後一行不能以\r\n結尾,不然設置的格式沒有效果(v3.5) 15 richTextRun.setText("這裏能夠換行\r\n第二行文本"); 16 txt.setAnchor(new java.awt.Rectangle(50,150,400,400)); 17 newSlide.addShape(txt); 18 19 savePPTFile(ppt);
4)插入圖片,支持多種格式sql
1 SlideShow ppt = new SlideShow(); 2 Slide newSlide = ppt.createSlide(); 3 int picIndex = ppt.addPicture(new File("圖片.jpg"), Picture.JPEG); 4 Picture jpg = new Picture(picIndex); 5 6 //set image position in the slide 7 jpg.setAnchor(new java.awt.Rectangle(360, 200, 280, 260)); 8 9 newSlide.addShape(jpg); 10 savePPTFile(ppt);
5)插入表格(v3.5)apache
1 SlideShow ppt = new SlideShow(); 2 Slide slide = ppt.createSlide(); 3 4 String[][] datas = { 5 {"序號", "姓名","年齡"}, 6 {"1", "張三","30"}, 7 {"2", "李四","27"}, 8 }; 9 10 //create a table of 3 rows and 3 columns 11 Table table = new Table(3, 3); 12 13 for (int i = 0; i < datas.length; i++) { 14 for (int j = 0; j < datas[i].length; j++) { 15 TableCell cell = table.getCell(i, j); 16 17 RichTextRun rt = cell.getTextRun().getRichTextRuns()[0]; 18 rt.setFontName("宋體"); 19 rt.setFontSize(12); 20 21 cell.setVerticalAlignment(TextBox.AnchorMiddle); 22 cell.setHorizontalAlignment(TextBox.AlignCenter); 23 cell.setText(datas[i][j]); 24 25 if(i == 0){//首行背景設置爲灰色 26 cell.setFillColor(Color.GRAY); 27 } 28 } 29 } 30 31 Line border = table.createBorder(); 32 border.setLineColor(Color.black); 33 border.setLineWidth(2.0); 34 table.setAllBorders(border); 35 36 slide.addShape(table); 37 table.moveTo(160,260); 38 savePPTFile(ppt);
6)若是是讀取已存在的PPT文檔則還要用到HSLFSlideShow,下面代碼將PPT文件導出爲圖片(png)格式,若是幻燈片上有中文字符則這些字符的字體須要修改成支持中文的字體(宋體等),不然導出的圖片的中文字符不能正常顯示瀏覽器
1 SlideShow ppt = new SlideShow(new HSLFSlideShow("PPT測試.ppt")); 2 Dimension pgsize = ppt.getPageSize(); 3 Slide[] slide = ppt.getSlides(); 4 5 for (int i = 0; i < slide.length; i++) { 6 BufferedImage img = new BufferedImage(pgsize.width, pgsize.height 7 , BufferedImage.TYPE_INT_RGB); 8 Graphics2D graphics = img.createGraphics(); 9 //clear the drawing area 10 graphics.setPaint(Color.white); 11 graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height)); 12 13 //render 14 slide[i].draw(graphics); 15 16 FileOutputStream out = new FileOutputStream("slide-" + (i+1) + ".png"); 17 javax.imageio.ImageIO.write(img, "png", out); 18 out.close();
7)提取PPT文檔信息app
1 SlideShow ppt = new SlideShow(new HSLFSlideShow("PPT測試.ppt")); 2 Slide[] slides = ppt.getSlides(); 3 //提取文本信息 4 for (Slide each : slides) { 5 System.out.println(each.getTitle()) ; 6 TextRun[] textRuns = each.getTextRuns(); 7 for (int i=0 ;i< textRuns.length; i++ ) { 8 System.out.println(textRuns[i].getText()); 9 RichTextRun[] richTextRuns = textRuns[i].getRichTextRuns(); 10 for (int j = 0; j < richTextRuns.length; j++) { 11 System.out.println(richTextRuns[j].getText()); 12 } 13 } 14 } 15 //提取全部JPEG圖片 16 PictureData[] picDatas = ppt.getPictureData(); 17 for (int i=0;i<picDatas.length;i++) { 18 if(picDatas[i].getType() == Picture.JPEG){ 19 FileOutputStream out = new FileOutputStream("jpg_" + i + ".jpg"); 20 ppt.write(out); 21 out.close(); 22 } 23 }
8)設置PPT文檔摘要信息(文檔點擊鼠標右鍵查看屬性)ide
1 HSLFSlideShow hslf = HSLFSlideShow.create(); 2 DocumentSummaryInformation dsi= hslf.getDocumentSummaryInformation(); 3 SummaryInformation si= hslf.getSummaryInformation(); 4 5 dsi.setCompany("yourCompany"); 6 dsi.setCategory("ppt測試"); 7 si.setAuthor("yourName"); 8 si.setTitle("標題"); 9 10 SlideShow ppt = new SlideShow(hslf); 11 savePPTFile(ppt);
1)若是是建立新的PPT文檔,直接使用SlideShow和Slide類就能夠,其中SlideShow表示PPT文檔,Slide表示某一張幻燈片
以下代碼建立空的PPT文檔:函數
2)設置母版,這樣後續的新建幻燈片都將使用母版的字體,背景等設置post
3)建立幻燈片並插入文本
4)插入圖片,支持多種格式
5)插入表格(v3.5)
6)若是是讀取已存在的PPT文檔則還要用到HSLFSlideShow,下面代碼將PPT文件導出爲圖片(png)格式,若是幻燈片上有中文字符則這些字符的字體須要修改成支持中文的字體(宋體等),不然導出的圖片的中文字符不能正常顯示
7)提取PPT文檔信息
8)設置PPT文檔摘要信息(文檔點擊鼠標右鍵查看屬性)
try {
// 獲取PPT文件
String pptModelPath =ConfigReadUtil.getInstance().getConfigItem("ppt_path"); //獲取路徑
FileInputStream is = new FileInputStream(pptModelPath+"2.pptx"); //讀pptx存放的路徑
XMLSlideShow ppt = new XMLSlideShow(is);
is.close();
for (XSLFSlide slide : ppt.getSlides()) {
// 獲取每一張幻燈片中的shape
for (XSLFShape shape : slide.getShapes()) {
Rectangle2D anchor = shape.getAnchor();
if (shape instanceof XSLFTextShape) {
XSLFTextShape txShape = (XSLFTextShape) shape;
if (txShape.getText().contains("{time}")) {
// 替換文字內容
txShape.setText(txShape.getText().replace(
"{time}", time1));
}else if (txShape.getText().contains("{eight}")) {
// 替換文字內容
txShape.setText(txShape.getText().replace(
"{eight}", finalTime));
}else if (txShape.getText().contains("{pic}")) { //替換圖片
byte[] pictureData = IOUtils.toByteArray(new FileInputStream("C:\\Users\\gls\\Desktop\\1.jpg"));
int idx = ppt.addPicture(pictureData,XSLFPictureData.PICTURE_TYPE_PNG);
XSLFPictureShape pic = slide.createPicture(idx);
// 設置XSLFPictureShape的位置信息
pic.setAnchor(anchor);
// 移除XSLFTextShape
slide.removeShape(txShape);
}
else if (txShape.getText().contains("{today}")) {
// 替換文字內容
txShape.setText(txShape.getText().replace(
"{today}", firstTime));
} else if (txShape.getText().contains("{tomorrow}")) {
// 替換文字內容
txShape.setText(txShape.getText().replace(
"{tomorrow}", secondTime));
}else if (txShape.getText().contains("{the day after tomorrow}")) {
// 替換文字內容
txShape.setText(txShape.getText().replace(
"{the day after tomorrow}", thirdTime));
}
else if (txShape.getText().contains("{dwfirstRain}")) {
// 替換文字內容
txShape.setText(txShape.getText().replace(
"{dwfirstRain}", dwfirstRain));
} else if (txShape.getText().contains("{psfirstRain}")) {
txShape.setText(txShape.getText().replace(
"{psfirstRain}", psfirstRain));
} else if (txShape.getText().contains("{dwsecondRain}")) {
txShape.setText(txShape.getText().replace(
"{dwsecondRain}",dwsecondRain));
} else if (txShape.getText().contains("{pssecondRain}")) {
txShape.setText(txShape.getText().replace(
"{pssecondRain}", pssecondRain));
} else if (txShape.getText().contains("{dwthirdRain}")) {
txShape.setText(txShape.getText().replace(
"{dwthirdRain}",dwthirdRain));
} else if (txShape.getText().contains("{psthirdRain}")) {
txShape.setText(txShape.getText().replace(
"{psthirdRain}",psthirdRain));
} else if (txShape.getText().contains("{dwFirstphenomena}")) {
txShape.setText(txShape.getText().replace("{dwFirstphenomena}",
dwFirstphenomena));
} else if (txShape.getText().contains("{psFirstphenomena}")) {
txShape.setText(txShape.getText().replace("{psFirstphenomena}",
psFirstphenomena));
} else if (txShape.getText().contains("{dwSecondphenomena}")) {
txShape.setText(txShape.getText().replace("{dwSecondphenomena}",
dwSecondphenomena));
} else if (txShape.getText().contains("{psSecondphenomena}")) {
txShape.setText(txShape.getText().replace("{psSecondphenomena}",
psSecondphenomena));
} else if (txShape.getText().contains("{dwThirdphenomena}")) {
txShape.setText(txShape.getText().replace("{dwThirdphenomena}",
dwThirdphenomena));
} else if (txShape.getText().contains("{psThirdphenomena}")) {
txShape.setText(txShape.getText().replace("{psThirdphenomena}",
psThirdphenomena));
} else if (txShape.getText().contains("{dwfirstMaxTemp}")) {
txShape.setText(txShape.getText().replace("{dwfirstMaxTemp}",
dwfirstMaxTemp));
} else if (txShape.getText().contains("{psfirstMaxTemp}")) {
txShape.setText(txShape.getText().replace("{psfirstMaxTemp}",
psfirstMaxTemp));
} else if (txShape.getText().contains("{dwsecondMaxTemp}")) {
txShape.setText(txShape.getText().replace(
"{dwsecondMaxTemp}", dwsecondMaxTemp));
}else if (txShape.getText().contains("{pssecondMaxTemp}")) {
txShape.setText(txShape.getText().replace(
"{pssecondMaxTemp}", pssecondMaxTemp));
}else if (txShape.getText().contains("{dwthirdMaxTemp}")) {
txShape.setText(txShape.getText().replace(
"{dwthirdMaxTemp}", dwthirdMaxTemp));
}else if (txShape.getText().contains("{psthirdMaxTemp}")) {
txShape.setText(txShape.getText().replace(
"{psthirdMaxTemp}", psthirdMaxTemp));
}else if (txShape.getText().contains("{dwfirstMinTemp}")) {
txShape.setText(txShape.getText().replace(
"{dwfirstMinTemp}", dwfirstMinTemp));
}else if (txShape.getText().contains("{psfirstMinTemp}")) {
txShape.setText(txShape.getText().replace(
"{psfirstMinTemp}", psfirstMinTemp));
}else if (txShape.getText().contains("{dwsecondtMinTemp}")) {
txShape.setText(txShape.getText().replace(
"{dwsecondtMinTemp}", dwsecondtMinTemp));
}else if (txShape.getText().contains("{pssecondMinTemp}")) {
txShape.setText(txShape.getText().replace(
"{pssecondMinTemp}", pssecondMinTemp));
}else if (txShape.getText().contains("{dwthirdMinTemp}")) {
txShape.setText(txShape.getText().replace(
"{dwthirdMinTemp}", dwthirdMinTemp));
}else if (txShape.getText().contains("{psthirdMinTemp}")) {
txShape.setText(txShape.getText().replace(
"{psthirdMinTemp}", psthirdMinTemp));
}else if (txShape.getText().contains("{dwWindDAndV}")) {
txShape.setText(txShape.getText().replace(
"{dwWindDAndV}", dwWindDAndV));
}else if (txShape.getText().contains("{psWindDAndV}")) {
txShape.setText(txShape.getText().replace(
"{psWindDAndV}", psWindDAndV));
}else if (txShape.getText().contains("{dwSeaWindDAndV}")) {
txShape.setText(txShape.getText().replace(
"{dwSeaWindDAndV}", dwSeaWindDAndV));
}else if (txShape.getText().contains("{psSeaWindDAndV}")) {
txShape.setText(txShape.getText().replace(
"{psSeaWindDAndV}", psSeaWindDAndV));
}else if (txShape.getText().contains("{zyPSMaxTemp}")) {
txShape.setText(txShape.getText().replace(
"{zyPSMaxTemp}", zyPSMaxTemp));
}else if (txShape.getText().contains("{zyPSMinTemp}")) {
txShape.setText(txShape.getText().replace(
"{zyPSMinTemp}", zyPSMinTemp));
}else if (txShape.getText().contains("{zyPSPhoenomena}")) {
txShape.setText(txShape.getText().replace(
"{zyPSPhoenomena}", zyPSPhoenomena));
}else if (txShape.getText().contains("{zyPSWindD}")) {
txShape.setText(txShape.getText().replace(
"{zyPSWindD}", zyPSWindD));
}else if (txShape.getText().contains("{zyPSWindV}")) {
txShape.setText(txShape.getText().replace(
"{zyPSWindV}", zyPSWindV));
}else if (txShape.getText().contains("{zyDWMaxTemp}")) {
txShape.setText(txShape.getText().replace(
"{zyDWMaxTemp}", zyDWMaxTemp));
}else if (txShape.getText().contains("{zyDWMinTemp}")) {
txShape.setText(txShape.getText().replace(
"{zyDWMinTemp}", zyDWMinTemp));
}else if (txShape.getText().contains("{zyDWPhoenomena}")) {
txShape.setText(txShape.getText().replace(
"{zyDWPhoenomena}", zyDWPhoenomena));
}else if (txShape.getText().contains("{zyDWWindD}")) {
txShape.setText(txShape.getText().replace(
"{zyDWWindD}", zyDWWindD));
}else if (txShape.getText().contains("{zyDWWindV}")) {
txShape.setText(txShape.getText().replace(
"{zyDWWindV}", zyDWWindV));
}
} else if (shape instanceof XSLFPictureShape) {
XSLFPictureShape pShape = (XSLFPictureShape) shape;
XSLFPictureData pData = pShape.getPictureData();
} else {
System.out.println("Process me: " + shape.getClass());
}
}
}
Object user = ServletActionContext.getRequest().getSession()
.getAttribute(ResourceUtil.getSessionInfoName());
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
String fileTime = sdf.format(date);
String time= s.format(date);
String filePath =ConfigReadUtil.getInstance().getConfigItem("FileSavePath");
// String filePath = "D:\\Conference";
filePath = filePath.replace("\\", "\\\\");
File dir = new File(filePath);
if(!dir.exists()){
dir.mkdirs();
}
String path = dir.getPath();
path = path.replace("\\", "\\\\");
String fileName = "天氣會商.pptx";
String fileType = "pptx";
WeatherConference c = new WeatherConference();
c.setFileName(time+fileName);
c.setFileType(fileType);
c.setFileTime(fileTime);
c.setFileUploader(user.toString());
c.setFilePath(path+"\\\\"+time+"天氣會商製做"+".pptx");
conferenceManager.add(c);
FileOutputStream out = new FileOutputStream(path+"\\"+time+"天氣會商製做"+".pptx");
ppt.write(out);
out.close();
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。
到apache 官方網站下載POI 的jar 包
一個Excel 文檔稱爲工做簿(worksheet),一個工做簿包含多個工做表(sheet),
每一個工做表看起來像一張二維表格,由不少行(row)組成,每行由多個單元格組成(cell).
下面是POI HSSF API 中的類與Excel 結構的對應關係:
新建java項目,導入jar文件
建立的xls 文件用excel 打開的時候報錯,由於這個 Excel 文件結構不完整。
xlsx 文件是 office2007 文件格式,這種文件格式是基於 xml 的,因此須要在項目中加
入xml 文件解析的jar 包。如圖,jar包在 poi 發行包中可以找到
一樣道理,由於xlsx 文件信息不完整,因此用Excel 打開的時候出現錯誤
在前面建立的工做簿(WorkBook)的基礎之上建立工做表
建立結果:
執行後的結果:
能夠看到日期有一些問題。代碼中使用的是」new Date()」,須要作一些轉換。
這個建立好的格式能夠反覆使用。
HSSFCellStyle 中定義了一些對齊方式
爲了方便操做,定義一個建立單元格的方法。
調用它建立單元格:
更多單元格,樣式屬性方法請參閱發行文檔 API
先設置好單元格,而後調用工做表的 addMergedRegion 方法合併單元格
結果:
Ok,大功告成!!
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 slidejpg.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 columnsTable 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(); } }
|
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")); } }
Microsoft Excel 版本中,一個單元格可包含多達 32,767 個字符。可是,若是某單元格中包含的字符多於 1,024 個,則應遵循如下規則:
大約第 1,024 個字符以後的字符不顯示在單元格中;不過,在編輯或選擇該單元格時,它們會出如今編輯欄中。
儘管不會顯示大約第 1,024 個字符以後的字符,但能夠使用工做表函數(例如 RIGHT 和 MID 函數)和宏命令(例如「字符」屬性)檢測並操做這些字符。
若是複製包含 1,024 個以上字符的單元格,而後將其粘貼到另外一單元格中,則會將全部字符粘貼到新的單元格中。不過,大約第 1,024 個字符以後的字符不會在目標單元格中顯示。
沒法打印單元格中大約第 1,024 個字符以後的字符,且這些字符也不會在打印預覽中顯示。
注意 :增長工做錶行高和列寬,或修改系統的顯示設置後,能夠看到的字符數將多於 1,024 個。
要了解此問題,請按照下列步驟操做:
在新工做表的單元格 A1 中,鍵入如下公式:
=REPT("w",1024)&"xyz"
選中單元格 A1,單擊「格式」菜單上的「單元格」。
單擊「對齊」選項卡。單擊以選中「自動換行」複選框,而後單擊「肯定」。
在「格式」菜單上,指向「列」,而後單擊「最適合的列寬」。
請注意,您只能在單元格 A1 中看到「w」字符,該單元格結尾處的「xyz」字符不會顯示。
在單元格 A2 和 A3 中鍵入如下公式:
A2:=RIGHT(A1,3)
A3:=LEN(A1)
單元格 A2 中的公式返回結果「xyz」,即單元格最右側的三個字符。單元格 A3 中包含數字 1027,即單元格 A1 中的字符數。
將單元格 A1 中的公式更改成:
=REPT("w",1023)&"xyz"
您能夠看到 1,023 個「w」字符及其後的「x」字符。由於存在 1,024 個字符的限制,因此不會顯示「yz」字符。單元格 A3 中的公式如今顯示 1026,即單元格 A1 的長度。