论坛首页 Java企业应用论坛

用Java读写多种格式的电子文档的方案

浏览 2907 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-04-25  
OO
  最近项目遇到一个问题,要用Java来对不同格式的电子文档(如:word excel pdf等)进行读写操作,实现数据的导入和导出功能,对于用Java来操作MS的方案有几种,譬如POI、JACOB等,但是否有一种好点的方法来统一对不同格式电子文档进行操作呢?
   发表时间:2008-04-25  
对文件本身的操作就使用poi,jxl等开源项目,如果导入导出的格式不会经常发生变化,将“定义”和“执行”分开,例如在xml中定义导入导出的所有参数格式:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE project SYSTEM "D:\eclipse\workspace\gds\src\dtsMap.dtd">
<project name="database conversion">
	<source-definition>
		<!--
		<source type="db" name="stock" url="jdbc:sybase:Tds:10.16.120.44:5000/pbzq" driver="com.sybase.jdbc2.jdbc.SybDriver" username="sa" password="888888" className="com.gds.util.dts.DatabaseDefine">
			<set-property property="description" value="Example Data Source Configuration"/>
		</source>
		<source type="db" name="found" url="jdbc:sybase:Tds:10.16.120.44:5000/pbzq" driver="com.sybase.jdbc2.jdbc.SybDriver" username="sa" password="888888" className="">
			<set-property property="description" value="Example Data Source Configuration"/>
		</source>
	-->
		<!--
	 定义"源"位置,可以是数据库、ftp服务器、email服务期、共享文件夹等形式
	-->
		<source type="ftp" name="ftpsource" url="ip:port/dir" driver="" username="username" password="passowrd" className="com.gds.util.dts.FtpSource">
			<set-property property="description" value="Example Data Source Configuration"/>
		</source>
	</source-definition>
	<dts-package>
		<package name="db2db" type="db2db" source="stock" destination="found" translator="com.gds.util.dts.DatabaseTranslator" className="">
			<set-property property="description" value="Example Data Source Configuration"/>
			<table-map name="" source="select * from zhk" destination="cashaccount">
				<field-map source="fzjzh" destination="CASHACCOUNTID"/>
				<field-map source="fkhrq" destination="OPENACCOUNTDATE"/>
				<field-map source="fqkfs" destination="DRAWTYPE"/>
				<field-map source="substring(fzjzh,1,2)" destination="OFFICEID"/>
				<field-map source="" destination="CASHORIGIN"/>
				<set-property property="description" value="Example Data Source Configuration"/>
			</table-map>
			<table-map source="select * from tskhsfblb,zhzl where tskhsfblb.fzjzh = zhzl.fzjzh " destination="shareholderrebate">
				<field-map source="zhzl.fgddm" destination="SHAREHOLDER"/>
				<field-map source="convert(char(1),tskhsfblb.fscdm)+convert(char(2),tskhsfblb.fgplb)" destination="SHARETYPE"/>
				<field-map source="tskhsfblb.fssfl" destination="REBATE"/>
				<set-property property="description" value="Example Data Source Configuration"/>
			</table-map>
		</package>
		<package name="db2excel" type="db2excel" source="stock" destination="ftpsource" translator="com.gds.util.dts.Database2ExcelTranslator" className="">
			<set-property property="description" value="Example Data Source Configuration"/>
			<table-map name="" source="select * from zhk" destination="testWorkbook.testSheet">
				<field-map source="fzjzh" destination="0,0"/>
				<field-map source="fkhrq" destination="0,1"/>
				<field-map source="fqkfs" destination="2,3"/>
				<field-map source="substring(fzjzh,1,2)" destination="4,8"/>
				<field-map source="test" destination="3,5"/>
			</table-map>
		</package>
		<package name="excel2db" type="excel2db" source="stock" destination="stock" translator="com.gds.util.dts.Excel2DatabaseTranslator" className="">
			<set-property property="description" value="Example Data Source Configuration"/>
			<table-map name="" source="testWorkbook.testSheet" destination="cashaccount">
				<field-map source="0,0" destination="fzjzh"/>
				<field-map source="0,1" destination="fkhrq"/>
				<field-map source="2,3" destination="fqkfs"/>
				<field-map source="4,8" destination="fzjzh"/>
				<field-map source="3,5" destination="test"/>
			</table-map>
		</package>
	</dts-package>
</project>
0 请登录后投票
   发表时间:2008-04-29  
taelons 写道
对文件本身的操作就使用poi,jxl等开源项目,如果导入导出的格式不会经常发生变化,将“定义”和“执行”分开,例如在xml中定义导入导出的所有参数格式:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE project SYSTEM "D:\eclipse\workspace\gds\src\dtsMap.dtd">
<project name="database conversion">
	<source-definition>
		<!--
		<source type="db" name="stock" url="jdbc:sybase:Tds:10.16.120.44:5000/pbzq" driver="com.sybase.jdbc2.jdbc.SybDriver" username="sa" password="888888" className="com.gds.util.dts.DatabaseDefine">
			<set-property property="description" value="Example Data Source Configuration"/>
		</source>
		<source type="db" name="found" url="jdbc:sybase:Tds:10.16.120.44:5000/pbzq" driver="com.sybase.jdbc2.jdbc.SybDriver" username="sa" password="888888" className="">
			<set-property property="description" value="Example Data Source Configuration"/>
		</source>
	-->
		<!--
	 定义"源"位置,可以是数据库、ftp服务器、email服务期、共享文件夹等形式
	-->
		<source type="ftp" name="ftpsource" url="ip:port/dir" driver="" username="username" password="passowrd" className="com.gds.util.dts.FtpSource">
			<set-property property="description" value="Example Data Source Configuration"/>
		</source>
	</source-definition>
	<dts-package>
		<package name="db2db" type="db2db" source="stock" destination="found" translator="com.gds.util.dts.DatabaseTranslator" className="">
			<set-property property="description" value="Example Data Source Configuration"/>
			<table-map name="" source="select * from zhk" destination="cashaccount">
				<field-map source="fzjzh" destination="CASHACCOUNTID"/>
				<field-map source="fkhrq" destination="OPENACCOUNTDATE"/>
				<field-map source="fqkfs" destination="DRAWTYPE"/>
				<field-map source="substring(fzjzh,1,2)" destination="OFFICEID"/>
				<field-map source="" destination="CASHORIGIN"/>
				<set-property property="description" value="Example Data Source Configuration"/>
			</table-map>
			<table-map source="select * from tskhsfblb,zhzl where tskhsfblb.fzjzh = zhzl.fzjzh " destination="shareholderrebate">
				<field-map source="zhzl.fgddm" destination="SHAREHOLDER"/>
				<field-map source="convert(char(1),tskhsfblb.fscdm)+convert(char(2),tskhsfblb.fgplb)" destination="SHARETYPE"/>
				<field-map source="tskhsfblb.fssfl" destination="REBATE"/>
				<set-property property="description" value="Example Data Source Configuration"/>
			</table-map>
		</package>
		<package name="db2excel" type="db2excel" source="stock" destination="ftpsource" translator="com.gds.util.dts.Database2ExcelTranslator" className="">
			<set-property property="description" value="Example Data Source Configuration"/>
			<table-map name="" source="select * from zhk" destination="testWorkbook.testSheet">
				<field-map source="fzjzh" destination="0,0"/>
				<field-map source="fkhrq" destination="0,1"/>
				<field-map source="fqkfs" destination="2,3"/>
				<field-map source="substring(fzjzh,1,2)" destination="4,8"/>
				<field-map source="test" destination="3,5"/>
			</table-map>
		</package>
		<package name="excel2db" type="excel2db" source="stock" destination="stock" translator="com.gds.util.dts.Excel2DatabaseTranslator" className="">
			<set-property property="description" value="Example Data Source Configuration"/>
			<table-map name="" source="testWorkbook.testSheet" destination="cashaccount">
				<field-map source="0,0" destination="fzjzh"/>
				<field-map source="0,1" destination="fkhrq"/>
				<field-map source="2,3" destination="fqkfs"/>
				<field-map source="4,8" destination="fzjzh"/>
				<field-map source="3,5" destination="test"/>
			</table-map>
		</package>
	</dts-package>
</project>

楼上的老兄,我还是有点迷糊,能不能给个源码过来
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics