c#操作word表格彪悍实例.docx

上传人:b****5 文档编号:12176845 上传时间:2023-04-17 格式:DOCX 页数:26 大小:20.46KB
下载 相关 举报
c#操作word表格彪悍实例.docx_第1页
第1页 / 共26页
c#操作word表格彪悍实例.docx_第2页
第2页 / 共26页
c#操作word表格彪悍实例.docx_第3页
第3页 / 共26页
c#操作word表格彪悍实例.docx_第4页
第4页 / 共26页
c#操作word表格彪悍实例.docx_第5页
第5页 / 共26页
点击查看更多>>
下载资源
资源描述

c#操作word表格彪悍实例.docx

《c#操作word表格彪悍实例.docx》由会员分享,可在线阅读,更多相关《c#操作word表格彪悍实例.docx(26页珍藏版)》请在冰豆网上搜索。

c#操作word表格彪悍实例.docx

c#操作word表格彪悍实例

1.using System; 

2.using System.Collections; 

3.using System.ComponentModel; 

4.using System.Data; 

5.using System.Drawing; 

6.using System.Web; 

7.using System.Web.SessionState; 

8.using System.Web.UI; 

9.using System.Web.UI.WebControls; 

10.using System.Web.UI.HtmlControls; 

11.using System.Configuration; 

12.using System.IO; 

13. 

14.using System.Reflection; 

15.using System.Runtime.InteropServices ; 

16.using System.Threading; 

17. //C#操作Word表格

18.public void MakeMyTable(DataTable DT,string strFilePath) 

19.  { 

20.    

21.   string strEnd   = this.txtEnd.Text.Trim().ToString(); 

22.   string strStart = this.txtStart.Text.Trim().ToString(); 

23.//生成文档分页中的起始和终止页 

24.   string strSign = "("+strStart + "-" + strEnd + ")"; 

25. 

26.   //杀掉所有word进程以保证速度 

27.   //KillWordProcess(); 

28. 

29.   object Nothing = System.Reflection.Missing.Value;  

30.   object missing = System.Reflection.Missing.Value;  

31.   object filename= strFilePath;   

32. 

33.   Word.Application wordApp=new Word.ApplicationClass();  

34.   Word.Document wordDoc=wordApp.Documents.Add( 

35.ref Nothing,ref Nothing,ref Nothing,ref Nothing);  

36. //C#操作Word表格

37.   try 

38.   { 

39.    //生成过程中屏蔽返回按扭,不允许中途停止 

40.    Button2.Enabled = false; 

41.    #region 生成文档 

42.    //设置文档宽度 

43.    wordApp.Selection.PageSetup.LeftMargin  = 

44. wordApp.CentimetersToPoints(float.Parse("2")); 

45.    wordApp.ActiveWindow.ActivePane.HorizontalPercentScrolled = 11 ; 

46.    wordApp.Selection.PageSetup.RightMargin = 

47. wordApp.CentimetersToPoints(float.Parse("2")); 

48. 

49.    Object start = Type.Missing; 

50.    Object end = Type.Missing; 

51.    Object unit = Type.Missing; 

52.    Object count = Type.Missing; 

53.    wordDoc.Range(ref start, ref end). 

54.Delete(ref unit, ref count); 

55. 

56. //C#操作Word表格

57.    object rng = Type.Missing; 

58.    string strInfo = this.txtNameCh. 

59.Text.ToString()+"明细表"+strSign+"\r\n"; 

60.    start = 0; 

61.    end = 0; 

62.    wordDoc.Range(ref start, ref end). 

63.InsertBefore(strInfo); 

64.    wordDoc.Range(ref start, ref end). 

65.Font.Name = "Verdana";  

66.    wordDoc.Range(ref start, ref end). 

67.Font.Size = 20; 

68.    wordDoc.Range(ref start, ref end). 

69.ParagraphFormat.Alignment = Word.WdParagraphAlignment. 

70.wdAlignParagraphCenter; 

71. 

72.    start = 8; 

73.    end = strInfo.Length; 

74.    wordDoc.Range(ref start, ref end). 

75.InsertParagraphAfter();//插入回车 

76. 

77.    if(DT.Rows.Count>0) 

78.    { 

79.//C#操作Word表格之存在数据项 

80.//添加一个表格 

81.object missingValue = Type.Missing;  

82.object location = strInfo.Length;  

83.//注:

若location超过已有字符的长度将会出错。

一定要比"明细表"串多一个字符 

84.Word.Range rng2 = wordDoc.Range(ref location, ref location); 

85. 

86.wordDoc.Tables.Add(rng2, 13, 6, ref missingValue, ref missingValue);  

87.wordDoc.Tables.Item

(1).Rows.HeightRule =  

88.Word.WdRowHeightRule.wdRowHeightAtLeast; 

89.wordDoc.Tables.Item

(1).Rows.Height = wordApp. 

90.CentimetersToPoints(float.Parse("0.8")); 

91.wordDoc.Tables.Item

(1).Range.Font.Size = 10; 

92.wordDoc.Tables.Item

(1).Range.Font.Name = "宋体"; 

93.wordDoc.Tables.Item

(1).Range.ParagraphFormat.Alignment = 

94. Word.WdParagraphAlignment.wdAlignParagraphCenter; 

95.wordDoc.Tables.Item

(1).Range.Cells.VerticalAlignment = 

96. Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter; 

97.//C#操作Word表格之设置表格样式 

98.wordDoc.Tables.Item

(1).Borders. 

99.Item(Word.WdBorderType.wdBorderLeft).LineStyle = 

100. Word.WdLineStyle.wdLineStyleSingle; 

101.wordDoc.Tables.Item

(1).Borders. 

102.Item(Word.WdBorderType.wdBorderLeft).LineWidth = 

103. Word.WdLineWidth.wdLineWidth050pt; 

104.wordDoc.Tables.Item

(1).Borders. 

105.Item(Word.WdBorderType.wdBorderLeft).Color =  

106.Word.WdColor.wdColorAutomatic; 

107. 

108.wordDoc.Tables.Item

(1).Borders. 

109.Item(Word.WdBorderType.wdBorderTop).LineStyle =  

110.Word.WdLineStyle.wdLineStyleSingle; 

111.wordDoc.Tables.Item

(1).Borders. 

112.Item(Word.WdBorderType.wdBorderTop).LineWidth =  

113.Word.WdLineWidth.wdLineWidth050pt; 

114.wordDoc.Tables.Item

(1).Borders. 

115.Item(Word.WdBorderType.wdBorderTop).Color = 

116. Word.WdColor.wdColorAutomatic; 

117. 

118.wordDoc.Tables.Item

(1).Borders. 

119.Item(Word.WdBorderType.wdBorderBottom).LineStyle = 

120. Word.WdLineStyle.wdLineStyleSingle; 

121.wordDoc.Tables.Item

(1).Borders. 

122.Item(Word.WdBorderType.wdBorderBottom).LineWidth = 

123. Word.WdLineWidth.wdLineWidth050pt; 

124.wordDoc.Tables.Item

(1).Borders. 

125.Item(Word.WdBorderType.wdBorderBottom).Color = 

126. Word.WdColor.wdColorAutomatic; 

127. 

128.wordDoc.Tables.Item

(1).Borders. 

129.Item(Word.WdBorderType.wdBorderHorizontal).LineStyle =  

130.Word.WdLineStyle.wdLineStyleSingle; 

131.wordDoc.Tables.Item

(1).Borders. 

132.Item(Word.WdBorderType.wdBorderHorizontal).LineWidth =  

133.Word.WdLineWidth.wdLineWidth050pt; 

134.wordDoc.Tables.Item

(1).Borders. 

135.Item(Word.WdBorderType.wdBorderHorizontal).Color =  

136.Word.WdColor.wdColorAutomatic; 

137. 

138.wordDoc.Tables.Item

(1).Borders. 

139.Item(Word.WdBorderType.wdBorderVertical).LineStyle =  

140.Word.WdLineStyle.wdLineStyleSingle; 

141.wordDoc.Tables.Item

(1).Borders. 

142.Item(Word.WdBorderType.wdBorderVertical).LineWidth =  

143.Word.WdLineWidth.wdLineWidth050pt; 

144.wordDoc.Tables.Item

(1).Borders. 

145.Item(Word.WdBorderType.wdBorderVertical).Color =  

146.Word.WdColor.wdColorAutomatic; 

147. 

148.wordDoc.Tables.Item

(1).Borders. 

149.Item(Word.WdBorderType.wdBorderRight).LineStyle =  

150.Word.WdLineStyle.wdLineStyleSingle; 

151.wordDoc.Tables.Item

(1).Borders. 

152.Item(Word.WdBorderType.wdBorderRight).LineWidth =  

153.Word.WdLineWidth.wdLineWidth050pt; 

154.wordDoc.Tables.Item

(1).Borders. 

155.Item(Word.WdBorderType.wdBorderRight).Color =  

156.Word.WdColor.wdColorAutomatic; 

157. //C#操作Word表格

158.//wordDoc.Tables.Item(k).Borders. 

159.Item(Word.WdBorderType.wdBorderDiagonalDown). 

160.LineStyle = Word.WdLineStyle.wdLineStyleSingle; 

161.//wordDoc.Tables.Item(k).Borders. 

162.Item(Word.WdBorderType.wdBorderDiagonalDown). 

163.LineWidth = Word.WdLineWidth.wdLineWidth050pt; 

164.//wordDoc.Tables.Item(k).Borders. 

165.Item(Word.WdBorderType.wdBorderDiagonalDown). 

166.Color = Word.WdColor.wdColorAutomatic; 

167.//第一行显示 

168.wordDoc.Tables.Item

(1).Cell(1,2). 

169.Merge(wordDoc.Tables.Item

(1).Cell(1,3)); 

170.wordDoc.Tables.Item

(1).Cell(1,4). 

171.Merge(wordDoc.Tables.Item

(1).Cell(1,5)); 

172. 

173.//第二行显示 

174.wordDoc.Tables.Item

(1).Cell(2,5). 

175.Merge(wordDoc.Tables.Item

(1).Cell(2,6)); 

176.wordDoc.Tables.Item

(1).Cell(1,4). 

177.Merge(wordDoc.Tables.Item

(1).Cell(2,5)); 

178. 

179.#region 插入数据行 

180.wordDoc.Tables.Item

(1).Cell(1, 1).Range.Text = "cell11";  

181.//wordDoc.Tables.Item(k).Cell(1, 2).Range.Text = 

182. DT.Rows[i]["cell11"].ToString(); 

183. 

184.////****************** 

185.wordDoc.Tables.Item

(1).Cell(1, 3).Range.Text = "cell13";  

186.//wordDoc.Tables.Item(k).Cell(1, 4).Range.Text = 

187. DT.Rows[i]["cell13"].ToString(); 

188. 

189.////****************** 

190.wordDoc.Tables.Item

(1).Cell(2, 1).Range.Text = "cell21";  

191.//wordDoc.Tables.Item(k).Cell(2, 2).Range.Text =  

192.DT.Rows[i]["cell21"].ToString(); 

193. 

194.////****************** 

195.wordDoc.Tables.Item

(1).Cell(2, 3).Range.Text = "cell23";  

196.//wordDoc.Tables.Item(k).Cell(2, 4).Range.Text = 

197. DT.Rows[i]["cell23"].ToString(); 

198. 

199.#endregion 

200. //C#操作Word表格

201.#region 第三行显示 

202.wordDoc.Tables.Item

(1).Cell(3,2). 

203.Merge(wordDoc.Tables.Item

(1).Cell(3,3)); 

204.wordDoc.Tables.Item

(1).Cell(3,2). 

205.Merge(wordDoc.Tables.Item

(1).Cell(3,3)); 

206. 

207.////****************** 

208.wordDoc.Tables.Item

(1).Cell(3, 1).Range.Text = "cell31";  

209. 

210.////****************** 

211.wordDoc.Tables.Item

(1).Cell(3, 3).Range.Text = "cell33";  

212. 

213.#endregion 

214. 

215.#region 第五行显示 

216.wordDoc.Tables.Item

(1).Cell(5,2). 

217.Merge(wordDoc.Tables.Item

(1).Cell(5,3)); 

218.wordDoc.Tables.Item

(1).Cell(5,2). 

219.Merge(wordDoc.Tables.Item

(1).Cell(5,3)); 

220.wordDoc.Tables.Item

(1).Cell(5,2). 

221.Merge(wordDoc.Tables.Item

(1).Cell(5,3)); 

222.wordDoc.Tables.Item

(1).Cell(5,2). 

223.Merge(wordDoc.Tables.Item

(1).Cell(5,3)); 

224.#endregion 

225. 

226.#region  第四行显示 

227. 

228.////****************** 

229.wordDoc.Tables.Item

(1).Cell(4, 1).Range.Text = "cell41"; 

230. 

231.////****************** 

232.wordDoc.Tables.Item

(1).Cell(4, 3).Range.Text = "cell43";  

233. 

234.////****************** 

235.wordDoc.Tables.Item

(1).Cell(4, 5).Range.Text = "cell45";  

236. 

237.#endregion 

238. //C#操作Word表格

239.#region 第六行显示 

240.wordDoc.Tables.Item

(1).Cell(6,2). 

241.Merge(wordDoc.Tables.Item

(1).Cell(6,3)); 

242.wordDoc.Tables.Item

(1).Cell(6,2). 

243.Merge(wordDoc.Tables.Item

(1).Cell(6,3)); 

244.wordDoc.Tables.Item

(1).Cell(6,2). 

245.Merge(wordDoc.Tables.Item

(1).Cell(6,3)); 

246.wordDoc.Tables.Item

(1).Cell(6,2). 

247.Merge(wordDoc.Tables.Item

(1).Cell(6,3)); 

248. 

249. 

250.////****************** 

251.wordDoc.Tables.Item

(1).Cell(5, 1). 

252.Range.Text = "cell51"; 

253. 

254.wordDoc.Tables.Item

(1).Cell(5, 2). 

255.Range.ParagraphFormat.Alignment =  

256.Word.WdParagraphAlignment.wdAlignParagraphLeft; 

257. 

258.  

259. 

260.////****************** 

261.wordDoc.Tables.Item

(1).Cell(6, 1).Range.Text = "cdll61"; 

262. 

263.wordDoc.Tables.Item

(1).Cell(6, 2). 

264.Range.ParagraphFormat.Align

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 其它

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1