1. 程式人生 > >Word轉html實現線上預覽

Word轉html實現線上預覽

word轉html,可以同時支援doc和docx兩種格式,非常好用

開發工具:idea
專案管理工具:maven
不多說,直接擼程式碼

1、首先配置pom.xml檔案,具體配置如下
在這裡插入圖片描述
2、工具類的開發

/**
 * WORD轉HTML docx格式
 * POI版本: 3.10-FINAL
 * */

import java.io.*;
import java.util.List;
import org.apache.poi.xwpf.usermodel.IBodyElement;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFPicture;
import org.apache.poi.xwpf.usermodel.XWPFPictureData;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

public class Docx2Html {
	/**
	 * 解析DOC
	 * 
	 * @param fileName 檔名
	 * @param  isAllHtml 全部為HMTL
	 * @param  tmpImgDir 臨時目錄,不包含檔名
	 * @param  tmpImgUrl 臨時連結,圖片 連結不包含檔名,通常這個引數可以傳一個相對路徑
	 * @throws Exception
	 */
	public static String analysisDocument(String fileName,boolean isAllHtml,String tmpImgDir,String tmpImgUrl) throws Exception{ 
		InputStream in = new FileInputStream(new File(fileName));
		return analysisDocument(in, isAllHtml, tmpImgDir, tmpImgUrl);
	}
	/**
	 * 解析DOC
	 */
	public static String analysisDocument(InputStream in,boolean isAllHtml,String tmpImgDir,String tmpImgUrl) throws Exception {
		XWPFDocument doc = new XWPFDocument(in);
		StringBuffer buffer=new StringBuffer();
		List<IBodyElement> eles= doc.getBodyElements();
		for(IBodyElement el:eles){
			String name=el.getElementType().name();
			if(name.equals("表格")){//表格
				XWPFTable table=(XWPFTable)el;
				buffer.append(analysisTable(table));
			}else {//文字
				XWPFParagraph graph=(XWPFParagraph)el;
				List<XWPFRun> runs=graph.getRuns();
				ParagraphAlignment alignment=graph.getAlignment();
				String align="left";
				if(alignment.equals(ParagraphAlignment.CENTER)){
					align="center";
				}else if(alignment.equals(ParagraphAlignment.RIGHT)){
					align="right";
				}
				buffer.append("<div style='text-align:").append(align);
				
				StringBuffer pBuffer=new StringBuffer();
				int index=0;
				for(XWPFRun run:runs){
					index++;
					List<XWPFPicture> pics=run.getEmbeddedPictures();
					if(null!=pics){
						for(XWPFPicture pic:pics){
							index++;
							XWPFPictureData pictureData=pic.getPictureData();
							byte[] picBytes=pictureData.getData();
							String picName="dzpic_"+System.currentTimeMillis()+"_"+index+".jpg";
							File file=new File(tmpImgDir+"/"+picName);
							OutputStream os =new FileOutputStream(file);
							os.write(picBytes);
							os.close();
							pBuffer.append("<img src='").append(tmpImgUrl).append(picName).append("'border=0'/>");
						}
					}
					String text=run.getText(run.getTextPosition());
					if(null!=text){
						String color=null==run.getColor()?"000":run.getColor();
						int fontsize=run.getFontSize()==-1?15:run.getFontSize();
						pBuffer.append("<span style='color:#").append(color).append(";font-size:").append(fontsize).append("px;'>");
						pBuffer.append(text).append("</span>");
					}
				}
				buffer.append(";'>").append(pBuffer.toString()).append("</div>");
			}
		}
		return buffer.toString();
	}
	/**
	 * 解析表格
	 * @param tb 表格 物件
	 * @return String
	 * @throws Exception
	 */
	static String analysisTable(XWPFTable tb)
			throws Exception {
		StringBuffer htmlTextTbl = new StringBuffer();
		htmlTextTbl .append("<table border=\"1\" style=\"width:100%;font-size:14px;\" class=\"display table table-striped table-bordered table-hover table-checkable dataTable no-footer\">");
		List<XWPFTableRow> rows=tb.getRows();
		int rowCount=rows.size();
		for (int i = 0; i < rowCount; i++) {
			XWPFTableRow tr = rows.get(i);
			String trCls=(i%2==1)?"odd":"even";
			htmlTextTbl .append("<tr class='").append(trCls).append("'>");
			
			List<XWPFTableCell> cells=tr.getTableCells();
			int cellCount=cells.size();
			for (int j = 0; j < cellCount; j++) {
				XWPFTableCell td = tr.getCell(j);
				List<XWPFParagraph> cellGraphs=  td.getParagraphs();
				for (int k = 0; k < cellGraphs.size(); k++) {
					XWPFParagraph para = cellGraphs.get(k);
					String s =para.getText()==null?"": para.getText().trim();
					if (s == "") {
						s = "&nbsp;";
					}
					if(i==0){
						htmlTextTbl.append("<th class='td-center'>").append(s).append("</th>");
					}else{
						htmlTextTbl.append("<td class='td-center'>").append(s).append("</td>");
					}
					
				}
			}
			htmlTextTbl.append("</tr>");
			
		}
		htmlTextTbl.append("</table>");
		return htmlTextTbl.toString();
	}

	public static void main(String args[]) {
		try {
			String htmlDoc=analysisDocument("d:/uploadtemp/mysql.docx", true, "d:/uploadtemp/","d:/uploadtemp/");
			File file=new File("d:/uploadtemp/text.html");
			if (file.exists()){
				file.createNewFile();
			}
			FileWriter fileWriter=new FileWriter(file);
			BufferedWriter bufferedWriter=new BufferedWriter(fileWriter);
			bufferedWriter.write(htmlDoc,0,htmlDoc.length()-1);
			bufferedWriter.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

/**
 * WORD轉HTML doc格式
 * POI版本: 3.10-FINAL
 * */

import java.io.*;

import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.model.PicturesTable;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Picture;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.hwpf.usermodel.TableCell;
import org.apache.poi.hwpf.usermodel.TableIterator;
import org.apache.poi.hwpf.usermodel.TableRow;

public class Doc2Html {

	private static final short ENTER_ASCII = 13; //回車
	private static final short SPACE_ASCII = 32; // 空格
	private static final short TABULATION_ASCII = 9; // TAB

	public static int beginPosi = 0;
	public static int endPosi = 0;
	public static int beginArray[];
	public static int endArray[];
	public static String htmlTextArray[];
	public static boolean tblExist = false;

	/**
	 * 解析DOC
	 * 
	 * @param fileName 檔名
	 * @param  isAllHtml 全部為HMTL
	 * @param  tmpImgDir 臨時目錄,圖片
	 * @param  tmpImgUrl 臨時連結,圖片
	 * @throws Exception
	 */
	public static String analysisDocument(String fileName,boolean isAllHtml,String tmpImgDir,String tmpImgUrl) throws Exception{ 
		InputStream in = new FileInputStream(new File(fileName));
		return analysisDocument(in, isAllHtml, tmpImgDir, tmpImgUrl);
	}
	
	/**
	 * 解析DOC
	 */
	public static String analysisDocument(InputStream in,boolean isAllHtml,String tmpImgDir,String tmpImgUrl) throws Exception {
		HWPFDocument doc = new HWPFDocument(in);
		Range rangetbl = doc.getRange();
		TableIterator it = new TableIterator(rangetbl);
		int num = 100;

		beginArray = new int[num];
		endArray = new int[num];
		htmlTextArray = new String[num];
		int length = doc.characterLength();
		if(length==0)return "";
		PicturesTable pTable = doc.getPicturesTable();
		SummaryInformation sif = doc.getSummaryInformation();
		String title = "DOC檔案預覽";
		if (null != sif) {
			title = doc.getSummaryInformation().getTitle();
		}
		StringBuffer htmlText=new StringBuffer("");
		if(isAllHtml){
			htmlText.append("<html><head><title>").append(title).append("</title></head><body>");
		}
		if (it.hasNext()) {
			analysisTables(it, rangetbl);
		}		
		
		int cur = 0;
		String tempString = "";
		int index=0;
		for (int i = 0; i < length - 1; i++) {
			Range range = new Range(i, i + 1, doc);
			CharacterRun cr = range.getCharacterRun(0);
			if (tblExist) {
				if (i == beginArray[cur]) {
					htmlText.append(tempString).append(htmlTextArray[cur]);
					tempString = "";
					i = endArray[cur] - 1;
					cur++;
					continue;
				}
			}
			if (pTable.hasPicture(cr)) {
				htmlText.append(tempString);
				String picFileName=analysisPicture(pTable, cr,tmpImgDir,index++);
				htmlText.append("<img src=\"").append(tmpImgUrl.endsWith("/")?tmpImgUrl:(tmpImgUrl+"/")).append(picFileName).append("\" style='width:100%;height:100%;border:0px;'/>");
				tempString = "";
			} else {
				Range range2 = new Range(i + 1, i + 2, doc);
				CharacterRun cr2 = range2.getCharacterRun(0);
				char c = cr.text().charAt(0);
				if (c == ENTER_ASCII) {
					tempString += "<br/>";
				}
				else if (c == SPACE_ASCII)
					tempString += " ";
				else if (c == TABULATION_ASCII)
					tempString += "    ";
				boolean flag = compareCharStyle(cr, cr2);
				if (flag)
					tempString += cr.text();
				else {
					StringBuffer fontStyle1 = new StringBuffer("<span style=\"font-family:");
					fontStyle1.append(cr.getFontName() ).append(";font-size:").append( cr.getFontSize() / 2).append( "pt;");
					if (cr.isBold())
						fontStyle1.append("font-weight:bold;");
					if (cr.isItalic())
						fontStyle1.append("font-style:italic;");
					htmlText.append(fontStyle1.toString()).append("\">") .append(tempString).append(cr.text()).append("</span>");
					fontStyle1.delete(0,fontStyle1.length());
					tempString = "";
				}
			}
		}
		htmlText .append(tempString);
		if(isAllHtml){htmlText .append("</body></html>");}
		return htmlText.toString();
	}

	/**
	 * 解析表格
	 * @param  it
	 * @param  rangetbl DOC段
	 * @throws Exception
	 */
	static void analysisTables(TableIterator it, Range rangetbl)
			throws Exception {
		int counter=0;
		while (it.hasNext()) {
			tblExist = true;
			StringBuffer htmlTextTbl = new StringBuffer();
			Table tb = (Table) it.next();
			beginPosi = tb.getStartOffset();
			endPosi = tb.getEndOffset();
			beginArray[counter] = beginPosi;
			endArray[counter] = endPosi;
			htmlTextTbl .append("<table border=\"1\" style=\"width:100%;font-size:14px;\" class=\"display table table-striped table-bordered table-hover table-checkable dataTable no-footer\">");
			for (int i = 0; i < tb.numRows(); i++) {
				TableRow tr = tb.getRow(i);
				String trCls=(i%2==1)?"odd":"even";
				htmlTextTbl .append("<tr class='").append(trCls).append("'>");
				for (int j = 0; j < tr.numCells(); j++) {
					TableCell td = tr.getCell(j);
					int cellWidth = td.getWidth();
					for (int k = 0; k < td.numParagraphs(); k++) {
						Paragraph para = td.getParagraph(k);
						String s = para.text().toString().trim();
						if (s == "") {
							s = "&nbsp;";
						}
						if(i==0){
							htmlTextTbl.append("<th class='td-center'>").append(s).append("</th>");
						}else{
							htmlTextTbl.append("<td class='td-center'>").append(s).append("</td>");
						}
					}
				}
				htmlTextTbl.append("</tr>");
			}
			htmlTextTbl.append("</table>");
			htmlTextArray[counter++] = htmlTextTbl.toString();
		}
	}

	/**
	 * 圖片解析
	 * 
	 * @param  pTable WORD中的圖片域
	 * @param  cr
	 * @param  path 臨時路徑
	 * @return String 圖片檔名
	 * @throws Exception
	 */
	static String analysisPicture(PicturesTable pTable, CharacterRun cr, String path, int index)
			throws Exception {
		// 圖片物件
		Picture pic = pTable.extractPicture(cr, false);
		// 圖片檔名
		String afileName = "dzpic_"+System.currentTimeMillis()+"_"+index+".jpg";
		OutputStream out = new FileOutputStream(new File((path.endsWith("/")?path:(path+ "/")) + afileName));
		pic.writeImageContent(out);
		out.close();
		return afileName;
	}
	/**
	 * 切換文字樣式
	 * @param  cr1
	 * @param  cr2
	 * @return boolean
	 * */
	static boolean compareCharStyle(CharacterRun cr1, CharacterRun cr2) {
		boolean flag = false;
		if (cr1.isBold() == cr2.isBold() && cr1.isItalic() == cr2.isItalic()
				&& cr1.getFontName().equals(cr2.getFontName())
				&& cr1.getFontSize() == cr2.getFontSize()) {
			flag = true;
		}
		return flag;
	}
	/**
	 * 測試
	 * */
	public static void main(String args[]) {
		try {
			String htmlDoc=analysisDocument("d:/uploadtemp/監造流程.doc", true, "d:/uploadtemp/","d:/uploadtemp/");
			File file=new File("d:/uploadtemp/監造流程.html");
			if (file.exists()){
				file.createNewFile();
			}
			FileWriter fileWriter=new FileWriter(file);
			BufferedWriter bufferedWriter=new BufferedWriter(fileWriter);
			bufferedWriter.write(htmlDoc,0,htmlDoc.length()-1);
			bufferedWriter.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
/**
 * 分析文件的格式是doc還是docx
 * */
public class AnaliysisDom {

    public static String analiysisDom(String path,String filename,HttpServletRequest request){
        String uri=null;
        String realPath=null;
        String inputpath=path+filename;
        String outputname=(filename.substring(0,filename.lastIndexOf('.')))+".html";
        String outputpath=path+outputname;
        File imgfile=new File(path+filename.substring(0,filename.lastIndexOf('.')));
        String string=imgfile.getPath();
        if (!imgfile.exists()){
            imgfile.mkdirs();
        }
        File outputFile = new File(outputpath);
        if (outputFile.exists()){

        }else {
            if (inputpath.endsWith(".doc") || inputpath.endsWith(".DOC")){
                try {
                    String s = Doc2Html.analysisDocument(inputpath, true, imgfile.getPath(), imgfile.getPath());
                    File file=new File(outputpath);
                    if (file.exists()){
                        file.createNewFile();
                    }
                    FileWriter fileWriter=new FileWriter(file);
                    BufferedWriter bufferedWriter=new BufferedWriter(fileWriter);
                    bufferedWriter.write(s,0,s.length()-1);
                    bufferedWriter.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (inputpath.endsWith(".docx") || inputpath.endsWith("DOCX")){
                try {
                    String s = Docx2Html.analysisDocument(inputpath, true, imgfile.getPath()+"/", imgfile.getPath()+"/");
                    File file=new File(outputpath);
                    if (file.exists()){
                        file.createNewFile();
                    }
                    FileWriter fileWriter=new FileWriter(file);
                    BufferedWriter bufferedWriter=new BufferedWriter(fileWriter);
                    bufferedWriter.write(s,0,s.length()-1);
                    bufferedWriter.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        realPath = request.getSession().getServletContext().getRealPath(outputname);
        System.out.println(realPath);
        uri=request.getScheme() + "://" + request.getServerName() + ":" +
                request.getServerPort() +"/"+ request.getContextPath() +outputname;
        return uri;
    }
}

controller層程式碼例項

    @RequestMapping("/readFile")
    @ResponseBody
    public String readFile(String templateattachpath,HttpServletRequest request){
       String uri="";
        try {
            uri=AnaliysisDom.analiysisDom(path,templateattachpath,request);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return uri;
    }

這樣word轉html就轉換成功了,超簡單,開擼吧!!!!