Word报表开发磨难记

系统中的报表采用的是C#和Word Automation方案,工作流程如下:

1.      预先创建Word报表模版

2.      动态填充XML数据

3.      生成DOC文档后

4.      借助于PDF995虚拟打印机生成PDF文件

开发环境是Windows XP 英文专业版,Visual Studio 2005 英文专业版,Office 2003英文专业版。

最终用户环境是Windows XP中文专业版,Office XP中文专业版。

由于种种原因导致了开发环境和终端用户环境的不同,经常是调试成功的系统部署后却无法正常运行,几乎全部是兼容性问题,而且很难解决。

起初在数据填充方式上采用了XML映射的方法,直接向WORD传送XML数据,确实是很方便,编码量很少,后来才发现Office XP不支持该功能,后面只能采用硬编码的方式操作Word中的表格,迁移时颇费了一番周折,幸好代码量不是很大,这里比较难处理的是不规则表格的处理,单元格的合并与拆分,一定要耐心调试,现在才发现不用XML绑定或许是正确的选择,估计它是无法完成合并拆分的重任,看来硬编码还是必不可少的。这里贴部分代码:

ContractedBlock.gif ExpandedBlockStart.gif
None.gifint rowIndex = 3;
None.gif            
foreach (XmlNode component in xnl)
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif{
InBlock.gif                document.Tables[
2].Cell(rowIndex, 1).Range.Text = component.SelectSingleNode("ns:Index", manager).InnerText;
InBlock.gif                document.Tables[
2].Cell(rowIndex, 4).Range.Text = component.SelectSingleNode("ns:StockID", manager).InnerText;
InBlock.gif                document.Tables[
2].Cell(rowIndex, 5).Range.Text = component.SelectSingleNode("ns:Description", manager).InnerText;
InBlock.gif                document.Tables[
2].Cell(rowIndex, 6).Range.Text = component.SelectSingleNode("ns:Package", manager).InnerText;
InBlock.gif                document.Tables[
2].Cell(rowIndex, 8).Range.Text = component.SelectSingleNode("ns:QuantityPlanning", manager).InnerText;
InBlock.gif                document.Tables[
2].Cell(rowIndex, 9).Range.Text = component.SelectSingleNode("ns:QuantityTakeOut", manager).InnerText;
InBlock.gif                document.Tables[
2].Cell(rowIndex, 10).Range.Text = component.SelectSingleNode("ns:Remark", manager).InnerText;
InBlock.gif                XmlNodeList itemList 
= component.SelectNodes("ns:Item", manager);
InBlock.gif                
object rows = itemList.Count;
InBlock.gif                
object columns = 1;
InBlock.gif                document.Tables[
2].Cell(rowIndex, 2).Split(ref rows, ref columns);
InBlock.gif                document.Tables[
2].Cell(rowIndex, 3).Split(ref rows, ref columns);
InBlock.gif                document.Tables[
2].Cell(rowIndex, 7).Split(ref rows, ref columns);
InBlock.gif                
foreach (XmlNode item in itemList)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    document.Tables[
2].Cell(rowIndex, 2).Range.Text = item.SelectSingleNode("ns:BOM", manager).InnerText;
InBlock.gif                    document.Tables[
2].Cell(rowIndex, 3).Range.Text = item.SelectSingleNode("ns:QuantityPerPCB", manager).InnerText;
InBlock.gif                    document.Tables[
2].Cell(rowIndex, 7).Range.Text = item.SelectSingleNode("ns:QuantityPlanning", manager).InnerText;
InBlock.gif                    rowIndex
++;
ExpandedSubBlockEnd.gif                }

ExpandedBlockEnd.gif            }

Page X of Y问题,用户环境中创建的PDF文件中的页码有问题,总是出现Page 1 of 1, Page 2 of 1,很明显的错位问题,打开生成的Word文档发现页码正常,手工打印出PDF文件,页码也正常,起初不知道是什么原因,就采用了下面的方法来测试,结果引发了另外的一个问题。

原程序是对DOT模版填充数据完毕后直接调用打印功能生成PDF文档的,这里尝试在DOC文档生成后暂时不打印,而是退出Word程序,休眠一段时间后再重新打开该文档直接打印。结果在用户环境中测试时,发现document.open函数总是抛出异常,google了一下微软有描述该问题,但解决方法似乎很复杂,摸不着头脑,调试了将近半天没有进展,于是尝试新的解决方案。

Google了一下Page X of Y,发现内容还不少,原来Office中对此处理有Bug,网上找到了一个解决方法http://word.mvps.org/FAQs/AppErrors/PageXofY.htm,直接打补丁,下载了Office XP SP3安装测试后,一切OK了。

转载于:https://www.cnblogs.com/swnuwangyun/archive/2007/02/13/649294.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值