「java解析ppt」java解析json报文
本篇文章给大家谈谈java解析ppt,以及java解析json报文对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
如何用JAVA实现在指定窗口内打开PPT
Apache的poi是为java写的解析office文件的库,本身有解析ppt的功能,官方网址是,是其ppt读取组件。
我大致看了一下,它会把ppt中的文本解析成RichTextRun对象,大概是html格式的富文本,至于图片貌似要另行获取。总的来说能满足你的要求。
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 poi 操作ppt,该怎么解决
解析PPT文件中的图片
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.Picture;
import org.apache.poi.hslf.usermodel.PictureData;
import org.apache.poi.hslf.usermodel.SlideShow;
public class OutputPicture {
// 图片默认存放路径
public final static String path = "F:\\ppt\";
public static void main(String[] args) throws Exception {
// 加载PPT
HSLFSlideShow _hslf = new HSLFSlideShow("F:\\Downloads\\myPPT.ppt");
SlideShow _slideShow = new SlideShow(_hslf);
// 获取PPT文件中的图片数据
PictureData[] _pictures = _slideShow.getPictureData();
// 循环读取图片数据
for (int i = 0; i _pictures.length; i++) {
StringBuilder fileName = new StringBuilder(path);
PictureData pic_data = _pictures[i];
fileName.append(i);
// 设置格式
switch (pic_data.getType()) {
case Picture.JPEG:
fileName.append(".jpg");
break;
case Picture.PNG:
fileName.append(".png");
break;
default:
fileName.append(".data");
}
// 输出文件
FileOutputStream fileOut = new FileOutputStream(new File(fileName.toString()));
fileOut.write(pic_data.getData());
fileOut.close();
}
}
}
在PPT文件中加入外部图片
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
import org.apache.poi.hslf.model.Picture;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;
public class InputPicture {
public static String path = "F:\\images\\myImage.png";
public static String OUTPUT = "F:\\ppt\\myppt.ppt";
public static void main(String[] args) throws Exception {
if(args.length != 0){
path = args[0];
}
// 构建PPT
SlideShow _slideShow = new SlideShow();
// 创建幻灯片
Slide _slide = _slideShow.createSlide();
// 设置图片类型
int pic_type = -1;
if(path.indexOf(".png") != -1){
pic_type = Picture.PNG;
}else{
pic_type = Picture.JPEG;
}
File file = new File(path);
BufferedImage image = ImageIO.read(file);
// 新置入图片索引位置
int newIndex = _slideShow.addPicture(file, pic_type);
// 根据索引位置 , 创建图片对象
Picture _picture = new Picture(newIndex);
// 设置图片显示位置
_picture.setAnchor(new Rectangle(100,100,image.getWidth(),image.getHeight()));
// 将图片放入幻灯片
_slide.addShape(_picture);
// 输出PPT文件
_slideShow.write(new FileOutputStream(new File(OUTPUT)));
}
}
操作文本对象
import java.awt.Color;
import java.awt.Rectangle;
import java.io.FileOutputStream;
import org.apache.poi.hslf.model.AutoShape;
import org.apache.poi.hslf.model.Line;
import org.apache.poi.hslf.model.ShapeTypes;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.TextBox;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.usermodel.SlideShow;
public class InputTextRun {
public static void main(String[] args) throws Exception{
SlideShow _slideShow = new SlideShow();
Slide slide = _slideShow.createSlide();
// 创建并置入简单文本
TextBox _text = new TextBox();
TextRun _textRun = _text.createTextRun();
_textRun.setRawText("杜磊米");
_text.setAnchor(new Rectangle(10,10,100,100));
// 创建并置入带有样式的文本
AutoShape _autoShape = new AutoShape(ShapeTypes.Rectangle); //设置形状
TextRun _autoText = _autoShape.createTextRun();
_autoText.setRawText("杜磊米");
_autoShape.setAnchor(new Rectangle(200,200,100,100));
_autoShape.setFillColor(new Color(170,215,255));
_autoShape.setLineWidth(5.0);
_autoShape.setLineStyle(Line.LINE_DOUBLE);
// AutoShape 对象可以设置多个不同样式文本
TextRun _autoText2 = _autoShape.createTextRun();
RichTextRun _richText = _autoText2.appendText("杜");
_richText.setFontColor(new Color(255,255,255));
RichTextRun _richText2 = _autoText2.appendText("磊米");
_richText2.setFontColor(new Color(255,0,0));
_richText2.setFontSize(12);
// 将文本对象置入幻灯片
slide.addShape(_text);
slide.addShape(_autoShape);
// 输出文件
_slideShow.write(new FileOutputStream("F:\\ppt\\text.ppt"));
}
}
设置各类文件属性
import java.awt.Dimension;
import java.io.FileOutputStream;
import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;
public class PPTProperty {
public static void main(String [] args)throws Exception{
HSLFSlideShow hslf = HSLFSlideShow.create();
SlideShow _slideShow = new SlideShow(hslf);
// 设置页面大小
_slideShow.setPageSize(new Dimension(400,600));
// 设置后创建出相应大小的幻灯片
Slide slide = _slideShow.createSlide();
DocumentSummaryInformation doc = hslf.getDocumentSummaryInformation();
SummaryInformation info = hslf.getSummaryInformation();
doc.setCompany("secret");
info.setAuthor("杜磊米");
info.setTitle("nothing");
// 输出文件
_slideShow.write(new FileOutputStream("F:\\ppt\\demo.ppt"));
// 完成后, 找到文件 , 右键属性查看:)
}
}
关于java解析ppt和java解析json报文的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。