本文目錄一覽:
eclipse-java讀取ppt
java中讀取ppt的實現方法如下:
public class ReadFileUtils {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
ReadFileUtils rf = new ReadFileUtils();
String s = “”;
// s = rf.readTXT(“E:/itsm文檔的後綴名分析報告2.txt”);
// s = rf.readPDF(“E:/memcached全面剖析.pdf”);
// s = rf.readEXCEL(“E:/副本工作量及成本模板.xls”);
// s = rf.readEXCEL2007(“E:/功能點估算方案.xlsx”);
// s = rf.readWORD(“E:/pms中文.doc”);
// s = rf.readWORD2007(“E:/功能點估算方法.docx”);
//s = rf.readPPT(“E:/精細化管理信息系統項目彙報v1.0.ppt”);
s = rf.readPPT2007(“e:/精細化管理信息系統項目彙報v1.0.pptx”);
System.out.println(s);
}
// 讀取ppt
public String readPPT(String file) throws IOException {
StringBuilder sb = new StringBuilder();
SlideShow ppt = new SlideShow(new HSLFSlideShow(file));
Slide[] slides = ppt.getSlides();
//提取文本信息
for (Slide each : slides) {
TextRun[] textRuns = each.getTextRuns();
for (int i=0 ;i textRuns.length; i++ ) {
RichTextRun[] richTextRuns = textRuns.getRichTextRuns();
for (int j = 0; j richTextRuns.length; j++) {
sb.append(richTextRuns[j].getText());
}
sb.append(“\n”);
}
sb.append(“\n”);
}
return sb.toString();
}
// 讀取pptx
public String readPPT2007(String file) throws IOException, XmlException, OpenXML4JException {
return new XSLFPowerPointExtractor(POIXMLDocument.openPackage(file)).getText();
}
// 讀取xls文件
public String readEXCEL(String file) throws IOException {
StringBuilder content = new StringBuilder();
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(file));// 創建對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 (short cellNumOfRow = 0; cellNumOfRow = aRow
.getLastCellNum(); cellNumOfRow++) {
if (null != aRow.getCell(cellNumOfRow)) {
HSSFCell aCell = aRow.getCell(cellNumOfRow);// 獲得列值
if (this.convertCell(aCell).length() 0) {
content.append(this.convertCell(aCell));
}
}
content.append(“\n”);
}
}
}
}
}
return content.toString();
}
// 讀取xlsx文件
public String readEXCEL2007(String file) throws IOException {
StringBuilder content = new StringBuilder();
XSSFWorkbook workbook = new XSSFWorkbook(file);
for (int numSheets = 0; numSheets workbook.getNumberOfSheets(); numSheets++) {
if (null != workbook.getSheetAt(numSheets)) {
XSSFSheet aSheet = workbook.getSheetAt(numSheets);// 獲得一個sheet
for (int rowNumOfSheet = 0; rowNumOfSheet = aSheet
.getLastRowNum(); rowNumOfSheet++) {
if (null != aSheet.getRow(rowNumOfSheet)) {
XSSFRow aRow = aSheet.getRow(rowNumOfSheet); // 獲得一個行
for (short cellNumOfRow = 0; cellNumOfRow = aRow
.getLastCellNum(); cellNumOfRow++) {
if (null != aRow.getCell(cellNumOfRow)) {
XSSFCell aCell = aRow.getCell(cellNumOfRow);// 獲得列值
if (this.convertCell(aCell).length() 0) {
content.append(this.convertCell(aCell));
}
}
content.append(“\n”);
}
}
}
}
}
return content.toString();
}
private String convertCell(Cell cell) {
NumberFormat formater = NumberFormat.getInstance();
formater.setGroupingUsed(false);
String cellValue = “”;
if (cell == null) {
return cellValue;
}
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC:
cellValue = formater.format(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_STRING:
cellValue = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BLANK:
cellValue = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
cellValue = Boolean.valueOf(cell.getBooleanCellValue()).toString();
break;
case HSSFCell.CELL_TYPE_ERROR:
cellValue = String.valueOf(cell.getErrorCellValue());
break;
default:
cellValue = “”;
}
return cellValue.trim();
}
// 讀取pdf文件
public String readPDF(String file) throws IOException {
String result = null;
FileInputStream is = null;
PDDocument document = null;
try {
is = new FileInputStream(file);
PDFParser parser = new PDFParser(is);
parser.parse();
document = parser.getPDDocument();
PDFTextStripper stripper = new PDFTextStripper();
result = stripper.getText(document);
} finally {
if (is != null) {
is.close();
}
if (document != null) {
document.close();
}
}
return result;
}
// 讀取doc文件
public String readWORD(String file) throws Exception {
String returnStr = “”;
try {
WordExtractor wordExtractor = new WordExtractor(new FileInputStream(new File(file)));
returnStr = wordExtractor.getText();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return returnStr;
}
// 讀取docx文件
public String readWORD2007(String file) throws Exception {
return new XWPFWordExtractor(POIXMLDocument.openPackage(file)).getText();
}
// 讀取txt文件
public String readTXT(String file) throws IOException {
String encoding = ReadFileUtils.get_charset(new File(file));
if (encoding.equalsIgnoreCase(“GBK”)) {
return FileUtils.readFileToString(new File(file), “gbk”);
} else {
return FileUtils.readFileToString(new File(file), “utf8”);
}
}
private static String get_charset(File file) throws IOException {
String charset = “GBK”;
byte[] first3Bytes = new byte[3];
BufferedInputStream bis = null;
try {
boolean checked = false;
bis = new BufferedInputStream(new FileInputStream(file));
bis.mark(0);
int read = bis.read(first3Bytes, 0, 3);
if (read == -1)
return charset;
if (first3Bytes[0] == (byte) 0xFF first3Bytes[1] == (byte) 0xFE) {
charset = “UTF-16LE”;
checked = true;
} else if (first3Bytes[0] == (byte) 0xFE
first3Bytes[1] == (byte) 0xFF) {
charset = “UTF-16BE”;
checked = true;
} else if (first3Bytes[0] == (byte) 0xEF
first3Bytes[1] == (byte) 0xBB
first3Bytes[2] == (byte) 0xBF) {
charset = “UTF-8”;
checked = true;
}
bis.reset();
if (!checked) {
// int len = 0;
int loc = 0;
while ((read = bis.read()) != -1) {
loc++;
if (read = 0xF0)
break;
if (0x80 = read read = 0xBF) // 單獨出現BF以下的,也算是GBK
break;
if (0xC0 = read read = 0xDF) {
read = bis.read();
if (0x80 = read read = 0xBF) // 雙字節 (0xC0 – 0xDF)
// (0x80
// – 0xBF),也可能在GB編碼內
continue;
else
break;
} else if (0xE0 = read read = 0xEF) {// 也有可能出錯,但是幾率較小
read = bis.read();
if (0x80 = read read = 0xBF) {
read = bis.read();
if (0x80 = read read = 0xBF) {
charset = “UTF-8”;
break;
} else
break;
} else
break;
}
}
// System.out.println( loc + ” ” + Integer.toHexString( read )
// );
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null) {
bis.close();
}
}
return charset;
}
}
java 怎麼把ppt轉html
ppt保存為html文件的方法:
用c#可以輕鬆實現轉換:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using PPT = Microsoft.Office.Interop.PowerPoint;
using System.Reflection;
namespace WritePptDemo
{
class Program
{
static void Main(string[] args)
{
string path; //文件路徑變量
PPT.Application pptApp; //Excel應用程序變量
PPT.Presentation pptDoc; //Excel文檔變量
PPT.Presentation pptDoctmp;
path = @”C:\MyPPT.ppt”; //路徑
pptApp = new PPT.ApplicationClass(); //初始化
//如果已存在,則刪除
if (File.Exists((string)path))
{
File.Delete((string)path);
}
//由於使用的是COM庫,因此有許多變量需要用Nothing代替
Object Nothing = Missing.Value;
pptDoc = pptApp.Presentations.Add(Microsoft.Office.Core.MsoTriState.msoFalse);
pptDoc.Slides.Add(1, Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutText);
string text = “示例文本”;
foreach (PPT.Slide slide in pptDoc.Slides)
{
foreach (PPT.Shape shape in slide.Shapes)
{
shape.TextFrame.TextRange.InsertAfter(text);
}
}
//WdSaveFormat為Excel文檔的保存格式
PPT.PpSaveAsFileType format = PPT.PpSaveAsFileType.ppSaveAsDefault;
//將excelDoc文檔對象的內容保存為XLSX文檔
pptDoc.SaveAs(path, format, Microsoft.Office.Core.MsoTriState.msoFalse);
//關閉excelDoc文檔對象
pptDoc.Close();
//關閉excelApp組件對象
pptApp.Quit();
Console.WriteLine(path + ” 創建完畢!”);
Console.ReadLine();
string pathHtml = @”c:\MyPPT”;
PPT.Application pa = new PPT.ApplicationClass();
pptDoctmp = pa.Presentations.Open(path, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);
PPT.PpSaveAsFileType formatTmp = PPT.PpSaveAsFileType.ppSaveAsHTML;
pptDoctmp.SaveAs(pathHtml, formatTmp, Microsoft.Office.Core.MsoTriState.msoFalse);
pptDoctmp.Close();
pa.Quit();
Console.WriteLine(pathHtml + ” 創建完畢!”);
}
}
}
這樣在創建完畢後就可以在c盤找到MyPPT了。
java導出ppt太慢
java導出ppt幻燈片可採用API的方法來執行,在java程序中導入spire.presentation.jar,然後執行如下代碼:
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
public class TEST {
public static void main(String[] args)throws Exception{
//創建PPT文檔(默認包含一頁空白幻燈片)
Presentation presentation = new Presentation();
//添加新矩形框到第一個幻燈片
Rectangle rec = new Rectangle((int) presentation.getSlideSize().getSize().getWidth() / 2 – 250, 80, 500, 150);
IAutoShape shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, rec);
shape.getShapeStyle().getLineColor().setColor(Color.white);
shape.getFill().setFillType(FillFormatType.NONE);
//添加文字到矩形框
shape.appendTextFrame(“你好 世界!”);
//設置文字的字體和樣式
PortionEx textRange = shape.getTextFrame().getTextRange();
textRange.getFill().setFillType(FillFormatType.SOLID);
textRange.getFill().getSolidColor().setColor(Color.blue);
textRange.setFontHeight(66);
textRange.setLatinFont(new TextFont(“宋體”));
//保存文檔
presentation.saveToFile(“helloWorld.pptx”, FileFormat.PPTX_2013);
presentation.dispose();
}
}
java 用POI轉換PPT的問題
Shape[] sh = slide[i].getShapes();
如果這頁裡面有Picture元素,應該是可以得到的。我用poi寫出了一個帶圖的ppt,然後用instanceof測試過,這是沒問題的。
可能因為你需要讀取的ppt不夠規範,比如一個人加圖一個習慣,有的人喜歡直接放圖,有的人喜歡把圖放在某個容器上之類的。
還有可能出問題的地方就是確定有底圖的頁面沒用母板?
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/152167.html