asp.net 生成word[转]

 

代码
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/

posted @ 2010-03-19 09:32  痴呆先生、  阅读(443)  评论(1编辑  收藏  举报