Skip to content

Commit

Permalink
ms office 转换成html
Browse files Browse the repository at this point in the history
  • Loading branch information
zyqwst committed Sep 20, 2017
1 parent 814600a commit 64cdc24
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 9 deletions.
Binary file modified .DS_Store
Binary file not shown.
Binary file modified WebContent/.DS_Store
Binary file not shown.
Binary file modified WebContent/WEB-INF/.DS_Store
Binary file not shown.
Binary file modified WebContent/WEB-INF/lib/poi-ooxml-3.8-20120326.jar
Binary file not shown.
25 changes: 25 additions & 0 deletions src/com/albert/test/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
*
*/
package com.albert.test;

import org.apache.poi.xwpf.converter.DocxConverter;

/**
* @ClassName: Test
* @Description:
* @author albert
* @date 2017年9月19日 下午4:56:28
*
*/
public class Test {

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
DocxConverter.convert("/Users/albert/Desktop/test.docx", "/Users/albert/Desktop/test.html");
}

}
8 changes: 4 additions & 4 deletions src/org/apache/poi/hwpf/converter/DocConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -806,12 +806,12 @@ protected void processImage(Element parent, CharacterRun cr ){
try {

// TWIPS_PER_INCH/
out = new FileOutputStream(new File(output.concat(File.separator).concat(fileName)));
File f= new File(output.concat(File.separator).concat(fileName));
out = new FileOutputStream(f);
pic.writeImageContent(out);
Element img = htmlDocumentFacade.getDocument().createElement("img");
String[] arr = output.split("/");
String uri = arr[arr.length-3].concat(File.separator).concat(arr[arr.length-2]).concat(File.separator).concat(arr[arr.length-1]).concat(File.separator).concat(fileName);
img.setAttribute("src",uri );
String uri = f.getParent().replace(f.getParentFile().getParentFile().getParentFile().getParentFile().getPath(), "").concat(File.separator).concat(fileName);
img.setAttribute("src",uri.substring(1) );
if(pic.getWidth() > 600)
img.setAttribute("style", "width: 600px;");
Element imgBlock = htmlDocumentFacade.createBlock();
Expand Down
6 changes: 1 addition & 5 deletions src/org/apache/poi/xwpf/converter/DocxConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,13 @@ public DocxConverter( String filePath, String output) throws IOException, Invali

Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
StringUtil.getFilePath("");
{// create image folder
int pos = output.lastIndexOf(".");
imgFolderPath = output.substring(0, pos).concat(File.separator).concat(IMG_FOLDER);

File folder = new File(imgFolderPath);
if(!folder.canRead())
folder.mkdirs();
folder = null;
}
imgPath = StringUtil.getFileName(filePath, false).concat(File.separator).concat(IMG_FOLDER);
imgPath = folder.getParent().replace(folder.getParentFile().getParentFile().getParentFile().getParentFile().getPath(), "").concat(File.separator).concat(IMG_FOLDER);


OPCPackage container = OPCPackage.open(filePath);
Expand Down Expand Up @@ -327,7 +324,6 @@ private void processParagraphMulti(XWPFParagraph paragraph, Element page, boole
}

private void processRun(Element container, XWPFRun r) throws IOException {

List<XWPFPicture> pics = r.getEmbeddedPictures();
if(!pics.isEmpty()){
processImage( container, pics);
Expand Down

0 comments on commit 64cdc24

Please sign in to comment.