asp.net 生成word[转]

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
1 其实Word模板比较好容易实现,其原理就是占位符 + 替代字符串,你可以用占位符(如 " <%字段1%> " )表示字段名,建立一个Word文件,然后用程序操作这个Word,用数据替换占位符,然后另存为新的文件即可。
2
3 这里的难点是在如何处理表格,需要你了解Word的相关API,以下代码仅供参考:
4
5   bool saveChange = false ;
6 // C#操作Word表格操作
7   object missing = System.Reflection.Missing.Value;
8
9   object template = ( object )templateFilePath;
10
11   object filename = ( object )saveFilePath;
12   object isVisible = missing;
13 object readOnly = missing;
14 object breakType = Word.WdBreakType.wdSectionBreakNextPage;
15 object isSaveChange = ( object )saveChange;
16 Word.Document doc = null ;
17
18 // 定义一个Word.Application 对象
19
20 Word.Application WordApp = new Word.ApplicationClass();
21
22 // C#操作Word表格操作之打开文档
23 doc = WordApp.Documents.Open( ref template,
24 ref missing, ref readOnly, ref missing,
25 ref missing, ref missing, ref missing,
26 ref missing, ref missing, ref missing,
27 ref missing, ref isVisible, ref missing,
28 ref missing, ref missing, ref missing);
29
30 // C#操作Word表格操作之设置页眉文本
31
32 WordApp.ActiveWindow.ActivePane.View.SeekView =
33 Word.WdSeekView.wdSeekCurrentPageHeader;
34 WordApp.Selection.WholeStory();
35 WordApp.Selection.TypeText( this .m_titleText );
36 WordApp.ActiveWindow.ActivePane.View.SeekView =
37 Word.WdSeekView.wdSeekMainDocument;
38
39 // 页面设置,设置页面为纵向布局,设置纸张类型为A4纸
40
41 doc.PageSetup.Orientation = Word.WdOrientation.wdOrientLandscape;
42 doc.PageSetup.PageWidth = WordApp.CentimetersToPoints( 29.7F );
43 doc.PageSetup.PageHeight = WordApp.CentimetersToPoints(21F);
44
45 // C#操作Word表格操作之创建表格及设置表格和单元格属性
46
47 object autoFitBehavior = Word.WdAutoFitBehavior.wdAutoFitWindow;
48 doc.Content.Tables.Add(
49 WordApp.Selection.Range, totalCount + 1 ,
50 totalField - keyCount_1, ref missing,
51 ref autoFitBehavior);
52
53 // C#操作Word表格操作之合并单元隔
54
55 doc.Content.Tables[ 1 ].Cell(i + 1 ,j).Select();
56 object moveUnit = Word.WdUnits.wdLine;
57 object moveCount = 1 ;
58 object moveExtend = Word.WdMovementType.wdExtend;
59 WordApp.Selection.MoveUp( ref moveUnit,
60 ref moveCount, ref moveExtend);
61 WordApp.Selection.Cells.Merge();
62 WordApp.Selection.Cells.VerticalAlignment =
63 Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
64
65 doc.Content.Tables[ 1 ].Cell(i + 1 , 1 ).Range.Text = “单元格内容填充”
66
67 // 添加表格行
68
69 doc.Content.Tables[ 0 ].Rows.Add( ref beforeRow);
70
71 // C#操作Word表格操作之添加表格列
72
73 doc.Content.Tables[ 0 ].Columns.Add( ref beforeColumn);
74
75 // 文本居中
76 WordApp.Selection.ParagraphFormat.Alignment =
77 Word.WdParagraphAlignment.wdAlignParagraphCenter;
78 WordApp.Selection.Cells.VerticalAlignment =
79 Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
80
81 // C#操作Word表格操作之选中单元格文字垂直居中
82
83 oDoc.Tables.Add(tableLocation, 3 , 4 , ref oMissing, ref oMissing);
84
85 Word.Table newTable = oDoc.Tables[ 1 ];
86 object beforeRow = newTable.Rows[ 1 ];
87 newTable.Rows.Add( ref beforeRow);
88
89 Word.Cell cell = newTable.Cell( 1 , 1 );
90 cell.Merge(newTable.Cell( 1 , 2 ));

 

以上这些是刚才无意间看到的,虽然现在暂时还没有去接触过,但以后应该会有用,先收藏先。呵呵

在这看到的:http://space.cnblogs.com/question/12771/

转载于:https://www.cnblogs.com/mrguo/archive/2010/03/19/1689523.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值