在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1 public class WordOperate 2 { 3 4 #region 新建Word文档 5 /// <summary> 6 /// 动态生成Word文档并填充内容 7 /// </summary> 8 /// <param name="dir">文档目录</param> 9 /// <param name="fileName">文档名</param> 10 /// <returns>返回自定义信息</returns> 11 public static bool CreateWordFile(string dir, string fileName) 12 { 13 try 14 { 15 Object oMissing = System.Reflection.Missing.Value; 16 17 if (!Directory.Exists(dir)) 18 { 19 //创建文件所在目录 20 Directory.CreateDirectory(dir); 21 } 22 //创建Word文档(Microsoft.Office.Interop.Word) 23 Microsoft.Office.Interop.Word._Application WordApp = new Application(); 24 WordApp.Visible = true; 25 Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Add( 26 ref oMissing, ref oMissing, ref oMissing, ref oMissing); 27 28 //保存 29 object filename = dir + fileName; 30 WordDoc.SaveAs(ref filename, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 31 ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 32 ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); 33 WordDoc.Close(ref oMissing, ref oMissing, ref oMissing); 34 WordApp.Quit(ref oMissing, ref oMissing, ref oMissing); 35 return true; 36 } 37 catch (Exception e) 38 { 39 Console.WriteLine(e.Message); 40 Console.WriteLine(e.StackTrace); 41 return false; 42 } 43 } 44 45 #endregion 新建Word文档 46 47 #region 给word文档添加页眉页脚 48 /// <summary> 49 /// 给word文档添加页眉 50 /// </summary> 51 /// <param name="filePath">文件名</param> 52 /// <returns></returns> 53 public static bool AddPageHeaderFooter(string filePath) 54 { 55 try 56 { 57 Object oMissing = System.Reflection.Missing.Value; 58 Microsoft.Office.Interop.Word._Application WordApp = new Application(); 59 WordApp.Visible = true; 60 object filename = filePath; 61 Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Open(ref filename, ref oMissing, 62 ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 63 ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); 64 65 ////添加页眉方法一: 66 //WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView; 67 //WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader; 68 //WordApp.ActiveWindow.ActivePane.Selection.InsertAfter( "**公司" );//页眉内容 69 70 ////添加页眉方法二: 71 if (WordApp.ActiveWindow.ActivePane.View.Type == WdViewType.wdNormalView || 72 WordApp.ActiveWindow.ActivePane.View.Type == WdViewType.wdOutlineView) 73 { 74 WordApp.ActiveWindow.ActivePane.View.Type = WdViewType.wdPrintView; 75 } 76 WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekCurrentPageHeader; 77 WordApp.Selection.HeaderFooter.LinkToPrevious = false; 78 WordApp.Selection.HeaderFooter.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; 79 WordApp.Selection.HeaderFooter.Range.Text = "页眉内容"; 80 81 WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekCurrentPageFooter; 82 WordApp.Selection.HeaderFooter.LinkToPrevious = false; 83 WordApp.Selection.HeaderFooter.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; 84 WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("页脚内容"); 85 86 //跳出页眉页脚设置 87 WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument; 88 89 //保存 90 WordDoc.Save(); 91 WordDoc.Close(ref oMissing, ref oMissing, ref oMissing); 92 WordApp.Quit(ref oMissing, ref oMissing, ref oMissing); 93 return true; 94 } 95 catch (Exception e) 96 { 97 Console.WriteLine(e.Message); 98 Console.WriteLine(e.StackTrace); 99 return false; 100 } 101 } 102 #endregion 给word文档添加页眉页脚 103 104 #region 设置文档格式并添加文本内容 105 /// <summary> 106 /// 设置文档格式并添加文本内容 107 /// </summary> 108 /// <param name="filePath">文件名</param> 109 /// <returns></returns> 110 public static bool AddContent(string filePath) 111 { 112 try 113 { 114 Object oMissing = System.Reflection.Missing.Value; 115 Microsoft.Office.Interop.Word._Application WordApp = new Application(); 116 WordApp.Visible = true; 117 object filename = filePath; 118 Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Open(ref filename, ref oMissing, 119 ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 120 ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); 121 122 //设置居左 123 WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft; 124 125 //设置文档的行间距 126 WordApp.Selection.ParagraphFormat.LineSpacing = 15f; 127 //插入段落 128 //WordApp.Selection.TypeParagraph(); 129 Microsoft.Office.Interop.Word.Paragraph para; 130 para = WordDoc.Content.Paragraphs.Add(ref oMissing); 131 //正常格式 132 para.Range.Text = "This is paragraph 1"; 133 //para.Range.Font.Bold = 2; 134 //para.Range.Font.Color = WdColor.wdColorRed; 135 //para.Range.Font.Italic = 2; 136 para.Range.InsertParagraphAfter(); 137 138 para.Range.Text = "This is paragraph 2"; 139 para.Range.InsertParagraphAfter(); 140 141 //插入Hyperlink 142 Microsoft.Office.Interop.Word.Selection mySelection = WordApp.ActiveWindow.Selection; 143 mySelection.Start = 9999; 144 mySelection.End = 9999; 145 Microsoft.Office.Interop.Word.Range myRange = mySelection.Range; 146 147 Microsoft.Office.Interop.Word.Hyperlinks myLinks = WordDoc.Hyperlinks; 148 object linkAddr = @"http://www.cnblogs.com/lantionzy"; 149 Microsoft.Office.Interop.Word.Hyperlink myLink = myLinks.Add(myRange, ref linkAddr, 150 ref oMissing); 151 WordApp.ActiveWindow.Selection.InsertAfter("\n"); 152 153 //落款 154 WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now.ToString(); 155 WordDoc.Paragraphs.Last.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight; 156 157 //保存 158 WordDoc.Save(); 159 WordDoc.Close(ref oMissing, ref oMissing, ref oMissing); 160 WordApp.Quit(ref oMissing, ref oMissing, ref oMissing); 161 return true; 162 } 163 catch (Exception e) 164 { 165 Console.WriteLine(e.Message); 166 Console.WriteLine(e.StackTrace); 167 return false; 168 } 169 } 170 171 #endregion 设置文档格式并添加文本内容 172 173 #region 文档中添加图片 174 /// <summary> 175 /// 文档中添加图片 176 /// </summary> 177 /// <param name="filePath">word文件名</param> 178 /// <param name="picPath">picture文件名</param> 179 /// <returns></returns> 180 public static bool AddPicture(string filePath, string picPath) 181 { 182 try 183 { 184 Object oMissing = System.Reflection.Missing.Value; 185 Microsoft.Office.Interop.Word._Application WordApp = new Application(); 186 WordApp.Visible = true; 187 object filename = filePath; 188 Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Open(ref filename, ref oMissing, 189 ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 190 ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); 191 192 //移动光标文档末尾 193 object count = WordDoc.Paragraphs.Count; 194 object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdParagraph; 195 WordApp.Selection.MoveDown(ref WdLine, ref count, ref oMissing);//移动焦点 196 WordApp.Selection.TypeParagraph();//插入段落 197 198 object LinkToFile = false; 199 object SaveWithDocument = true; 200 object Anchor = WordDoc.Application.Selection.Range; 201 WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(picPath, ref LinkToFile, ref SaveWithDocument, ref Anchor); 202 203 //保存 204 WordDoc.Save(); 205 WordDoc.Close(ref oMissing, ref oMissing, ref oMissing); 206 WordApp.Quit(ref oMissing, ref oMissing, ref oMissing); 207 return true; 208 } 209 catch (Exception e) 210 { 211 Console.WriteLine(e.Message); 212 Console.WriteLine(e.StackTrace); 213 return false; 214 } 215 } 216 #endregion 文档中添加图片 217 218 #region 表格处理(插入表格、设置格式、填充内容) 219 /// <summary> 220 /// 表格处理 221 /// </summary> 222 /// <param name="filePath">word文件名</param> 223 /// <returns></returns> 224 public static bool AddTable(string filePath) 225 { 226 try 227 { 228 Object oMissing = System.Reflection.Missing.Value; 229 Microsoft.Office.Interop.Word._Application WordApp = new Application(); 230 WordApp.Visible = true; 231 object filename = filePath; 232 Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Open(ref filename, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 233 ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); 234 235 //插入表格 236 Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref oMissing, ref oMissing); 237 //设置表格 238 newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinLargeGap; 239 newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle; 240 newTable.Columns[1].Width = 100f; 241 newTable.Columns[2].Width = 220f; 242 newTable.Columns[3].Width = 105f; 243 244 //填充表格内容 245 newTable.Cell(1, 1).Range.Text = "我的简历"; 246 //设置单元格中字体为粗体 247 newTable.Cell(1, 1).Range.Bold = 2; 248 249 //合并单元格 250 newTable.Cell(1, 1).Merge(newTable.Cell(1, 3)); 251 252 //垂直居中 253 WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter; 254 //水平居中 255 WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; 256 257 //填充表格内容 258 newTable.Cell(2, 1).Range.Text = "座右铭:..."; 259 //设置单元格内字体颜色 260 newTable.Cell(2, 1).Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorDarkBlue; 261 //合并单元格 262 newTable.Cell(2, 1).Merge(newTable.Cell(2, 3)); 263 WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter; 264 265 //填充表格内容 266 newTable.Cell(3, 1).Range.Text = "姓名:"; 267 newTable.Cell(3, 2).Range.Text = "雷鑫"; 268 //纵向合并单元格 269 newTable.Cell(3, 3).Select(); 270 //选中一行 271 object moveUnit = Microsoft.Office.Interop.Word.WdUnits.wdLine; 272 object moveCount = 3; 273 object moveExtend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend; 274 WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend); 275 WordApp.Selection.Cells.Merge(); 276 277 //表格中插入图片 278 string pictureFileName = System.IO.Directory.GetCurrentDirectory() + @"\picture.jpg"; 279 object LinkToFile = false; 280 object SaveWithDocument = true; 281 object Anchor = WordDoc.Application.Selection.Range; 282 WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(pictureFileName, ref LinkToFile, ref SaveWithDocument, ref Anchor); 283 //图片宽度 284 WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 100f; 285 //图片高度 286 WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 100f; 287 //将图片设置为四周环绕型 288 Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape(); 289 s.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare; 290 291 newTable.Cell(12, 1).Range.Text = "备注:"; 292 newTable.Cell(12, 1).Merge(newTable.Cell(12, 3)); 293 //在表格中增加行 294 WordDoc.Content.Tables[1].Rows.Add(ref oMissing); 295 296 //保存 297 WordDoc.Save(); 298 WordDoc.Close(ref oMissing, ref oMissing, ref oMissing); 299 WordApp.Quit(ref oMissing, ref oMissing, ref oMissing); 300 return true; 301 } 302 catch (Exception e) 303 { 304 Console.WriteLine(e.Message); 305 Console.WriteLine(e.StackTrace); 306 return false; 307 } 308 } 309 #endregion #region 表格处理 310 311 #region 把Word文档转化为Html文件 312 /// <summary> 313 /// 把Word文档转化为Html文件 314 /// </summary> 315 /// <param name="wordFileName">word文件名</param> 316 /// <param name="htmlFileName">要保存的html文件名</param> 317 /// <returns></returns> 318 public static bool WordToHtml(string wordFileName, string htmlFileName) 319 { 320 try 321 { 322 Object oMissing = System.Reflection.Missing.Value; 323 Microsoft.O |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论