---
title: "把excle，word转换成html页面显示"
author: "aazham"
date: "2012-01-11T11:08:54Z"
tags: []
categories: ["Web前端"]
slug: "excle-word-html"
draft: false
lastmod: "2012-01-20T10:57:08Z"
id: 1340909
url: "https://aazham.iteye.com/blog/1340909"
---

项目开发过程中，需求涉及到了各种文档转换为HTML或者网页易显示格式，现在将实现方式整理如下： 
**一、使用Jacob转换Word,Excel为HTML** 

“JACOB一个Java-COM中间件.通过这个组件你可以在Java应用程序中调用COM组件和Win32 libraries。”

首先下载Jacob包，JDK1.5以上需要使用Jacob1.9版本（JDK1.6尚未测试），与先前的Jacob1.7差别不大

1、将压缩包解压后，Jacob.jar添加到Libraries中；

2、将Jacob.dll放至“WINDOWS\SYSTEM32”下面。

需要注意的是： 
【使用IDE启动Web服务器时，系统读取不到Jacob.dll，例如用MyEclipse启动Tomcat，就需要将dll文件copy到MyEclipse安装目录的“jre\bin”下面。 
一般系统没有加载到Jacob.dll文件时，报错信息为：“java.lang.UnsatisfiedLinkError: no jacob in java.library.path”】 

新建类：

pdftohtml-0.39-win32.tar.gz
下载jacob.rar，解压出jacob.dll和jacob.jar

然后：
1) 把jacob.dll在 C:\Program Files\Java\jdk1.5.0_08\bin、C:\Program Files\Java\jdk1.5.0_08\jre\bin、C:\WINDOWS\system32    目录下各.放一份
2) 把jacob.jar放入 项目的lib包下，并且在“java构建路径”中也要加载此jar包。.
3) 运行项目即可编译通过.

注：
    1)这样配置后不需要再配置环境变量（如：path,classpath）了.

4、解压调试

1) 先将xpdf-3.02pl2-win32.zip解压，解压后的内容可根据需要进行删减，如果只需要转换为txt格式，其他的exe文件可以删除，只保留pdftotext.exe，以此类推；

2) 然后将xpdf-chinese-simplified.tar.gz解压到刚才xpdf-3.02pl2-win32.zip的解压目录；

3) 将pdftohtml-0.39-win32.tar.gz解压，pdftohtml.exe解压到xpdf-3.02pl2-win32.zip的解压目录；

4) 目录结构：

 +---

           |-------各种转换用到的exe文件

           |

           |-------xpdfrc

           |

           +------

                                      |

                                      |

                                      +-------很多转换时需要用到的字符文件


xpdfrc：此文件是用来声明转换字符集对应路径的文件


5) 修改xpdfrc文件(文件原名为sample-xpdfrc)

修改文件内容为：




Txt代码 ![](/images/icon_copy.gif&quot; alt=&quot;复制代码) ![](/images/icon_star.png&quot; class=&quot;star&quot; alt=&quot;收藏代码)![](/images/spinner.gif&quot; style=&quot;display: none;&quot; class=&quot;spinner&quot; alt=&quot;)



#----- begin Chinese Simplified support package  

cidToUnicode    Adobe-GB1       xpdf-chinese-simplified\Adobe-GB1.cidToUnicode  

unicodeMap      ISO-2022-CN     xpdf-chinese-simplified\ISO-2022-CN.unicodeMap  

unicodeMap      EUC-CN          xpdf-chinese-simplified\EUC-CN.unicodeMap  

unicodeMap  GBK    xpdf-chinese-simplified\GBK.unicodeMap  

cMapDir         Adobe-GB1       xpdf-chinese-simplified\CMap  

toUnicodeDir                    xpdf-chinese-simplified\CMap  

fontDir  C:\WINDOWS\Fonts  

displayCIDFontTT Adobe-GB1 C:\WINDOWS\Fonts\simhei.ttf  

#----- end Chinese Simplified support package  



```&quot;TXT&quot;
#----- begin Chinese Simplified support package
cidToUnicode    Adobe-GB1       xpdf-chinese-simplified\Adobe-GB1.cidToUnicode
unicodeMap      ISO-2022-CN     xpdf-chinese-simplified\ISO-2022-CN.unicodeMap
unicodeMap      EUC-CN          xpdf-chinese-simplified\EUC-CN.unicodeMap
unicodeMap  GBK    xpdf-chinese-simplified\GBK.unicodeMap
cMapDir         Adobe-GB1       xpdf-chinese-simplified\CMap
toUnicodeDir                    xpdf-chinese-simplified\CMap
fontDir  C:\WINDOWS\Fonts
displayCIDFontTT Adobe-GB1 C:\WINDOWS\Fonts\simhei.ttf
#----- end Chinese Simplified support package
```


6) 创建bat文件pdftohtml.bat（放置的路径不能包含空格）

内容为：




Txt代码 ![](/images/icon_copy.gif&quot; alt=&quot;复制代码) ![](/images/icon_star.png&quot; class=&quot;star&quot; alt=&quot;收藏代码)![](/images/spinner.gif&quot; style=&quot;display: none;&quot; class=&quot;spinner&quot; alt=&quot;)



@echo off  

set folderPath=%1  

set filePath=%2  

cd /d %folderPath%  

pdftohtml -enc GBK %filePath%  

exit  



```&quot;TXT&quot;
@echo off
set folderPath=%1
set filePath=%2
cd /d %folderPath%
pdftohtml -enc GBK %filePath%
exit
```


7) 创建类




Java代码 ![](/images/icon_copy.gif&quot; alt=&quot;复制代码) ![](/images/icon_star.png&quot; class=&quot;star&quot; alt=&quot;收藏代码)![](/images/spinner.gif&quot; style=&quot;display: none;&quot; class=&quot;spinner&quot; alt=&quot;)



**public** **class** ConvertPdf  

{  

    **private** **static** String INPUT_PATH;  

    **private** **static** String PROJECT_PATH;  

      

    **public** **static** **void** convertToHtml(String file, String project)  

    {  

        INPUT_PATH = file;  

        PROJECT_PATH = project;  

        **if**(checkContentType()==0)  

        {  

            toHtml();  

        }  

    }  

      

    **private** **static** **int** checkContentType()  

    {  

        String type = INPUT_PATH.substring(INPUT_PATH.lastIndexOf(&quot;.&quot;) + 1, INPUT_PATH.length())  

                .toLowerCase();  

        **if** (type.equals(&quot;pdf&quot;))  

            **return** 0;  

        **else**  

            **return** 9;  

    }  

      

    **private** **static** **void** toHtml()  

    {  

        **if**(**new** File(INPUT_PATH).isFile())  

        {  

            **try**  

            {  

                String cmd = &quot;cmd /c start X:\\pdftohtml.bat \&quot;&quot; + PROJECT_PATH + &quot;\&quot; \&quot;&quot; + INPUT_PATH + &quot;\&quot;&quot;;  

                Runtime.getRuntime().exec(cmd);  

            }  

            **catch** (IOException e)  

            {  

                e.printStackTrace();  

            }  

        }  

    }  

      

}  



```&quot;java&quot;
public class ConvertPdf
{
	private static String INPUT_PATH;
	private static String PROJECT_PATH;
	
	public static void convertToHtml(String file, String project)
	{
		INPUT_PATH = file;
		PROJECT_PATH = project;
		if(checkContentType()==0)
		{
			toHtml();
		}
	}
	
	private static int checkContentType()
	{
		String type = INPUT_PATH.substring(INPUT_PATH.lastIndexOf(&quot;.&quot;) + 1, INPUT_PATH.length())
				.toLowerCase();
		if (type.equals(&quot;pdf&quot;))
			return 0;
		else
			return 9;
	}
	
	private static void toHtml()
	{
		if(new File(INPUT_PATH).isFile())
		{
			try
			{
				String cmd = &quot;cmd /c start X:\\pdftohtml.bat \&quot;&quot; + PROJECT_PATH + &quot;\&quot; \&quot;&quot; + INPUT_PATH + &quot;\&quot;&quot;;
				Runtime.getRuntime().exec(cmd);
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}
	
}
```

String cmd = &quot;....&quot;;此处代码是调用创建的bat文件进行转换 


8) 测试转换





Java代码 ![](/images/icon_copy.gif&quot; alt=&quot;复制代码) ![](/images/icon_star.png&quot; class=&quot;star&quot; alt=&quot;收藏代码)![](/images/spinner.gif&quot; style=&quot;display: none;&quot; class=&quot;spinner&quot; alt=&quot;)



**public** **static** **void** main(String args)  

{  

    ConvertPdf.convertToHtml(&quot;C:\\test.pdf&quot;, &quot;X:\\xpdf&quot;);  

}  



```&quot;java&quot;
public static void main(String[] args)
{
	ConvertPdf.convertToHtml(&quot;C:\\test.pdf&quot;, &quot;X:\\xpdf&quot;);
}
```



以上就是整理的内容，后续还会添加视频转换为FLV格式，如果需要的话:)

说的不是很详细，碰到问题的朋友可以自己努力解决一下:)