引用using Microsoft.Office.Interop.Word;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Windows.Forms; 9 using Microsoft.Office.Interop.Word; 10 11 12 namespace frmWord 13 { 14 public partial class DataAccessTest : Form 15 { 16 public DataAccessTest() 17 { 18 InitializeComponent(); 19 } 20 21 private void DataAccessTest_Load(object sender, EventArgs e) 22 { 23 24 } 25 26 //C#读取Word表格中的数据 27 private void button1_Click(object sender, EventArgs e) 28 { 29 string temp = @"D:\test.doc"; 30 string name = @"D:\test111.doc"; 31 WordAPI wordAPI = new WordAPI(temp,name); 32 wordAPI.SaveAsWord(); 33 } 34 } 35 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using Microsoft.Office.Interop.Word; 6 7 namespace frmWord 8 { 9 public class WordAPI 10 { 11 private object _template; 12 private object _newWord; 13 private Microsoft.Office.Interop.Word.Application wordApp; 14 private Microsoft.Office.Interop.Word.Document _wordDocument; 15 private object defaultV = System.Reflection.Missing.Value; 16 private object documentType; 17 /// <summary> 18 /// 构造函数 19 /// </summary> 20 /// <param name="template">模板文件位置</param> 21 /// <param name="newWord">将要保存的位置</param> 22 public WordAPI(string template, string newWord) 23 { 24 this._template = template; 25 this._newWord = newWord; 26 wordApp = new Application(); 27 documentType = Microsoft.Office.Interop.Word.WdDocumentType.wdTypeDocument; 28 _wordDocument = wordApp.Documents.Add(ref _template, ref defaultV, ref documentType, ref defaultV); 29 } 30 /// <summary> 31 /// 设置默认一页行数 32 /// </summary> 33 /// <param name="size"></param> 34 public void SetLinesPage(int size) 35 { 36 wordApp.ActiveDocument.PageSetup.LinesPage = 40; 37 } 38 /// <summary> 39 /// 设置书签的值 40 /// </summary> 41 /// <param name="markName">书签名</param> 42 /// <param name="markValue">书签值</param> 43 public void SetBookMark(string markName, string markValue) 44 { 45 object _markName = markName; 46 try 47 { 48 _wordDocument.Bookmarks.get_Item(ref _markName).Range.Text = markValue; 49 } 50 catch 51 { 52 throw new Exception(markName + "未找到!!"); 53 } 54 } 55 /// <summary> 56 /// 设置添加页眉 57 /// </summary> 58 /// <param name="context">内容</param> 59 public void SetPageHeader(string context) 60 { 61 wordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView; 62 wordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader; 63 wordApp.ActiveWindow.ActivePane.Selection.InsertAfter(context); 64 wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; 65 //跳出页眉设置 66 wordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument; 67 } 68 /// <summary> 69 /// 当前位置处插入文字 70 /// </summary> 71 /// <param name="context">文字内容</param> 72 /// <param name="fontSize">字体大小</param> 73 /// <param name="fontColor">字体颜色</param> 74 /// <param name="fontBold">粗体</param> 75 /// <param name="familyName">字体</param> 76 /// <param name="align">对齐方向</param> 77 public void InsertText(string context, int fontSize, WdColor fontColor, int fontBold, string familyName, WdParagraphAlignment align) 78 { 79 //设置字体样式以及方向 80 wordApp.Application.Selection.Font.Size = fontSize; 81 wordApp.Application.Selection.Font.Bold = fontBold; 82 wordApp.Application.Selection.Font.Color = fontColor; 83 wordApp.Selection.Font.Name = familyName; 84 wordApp.Application.Selection.ParagraphFormat.Alignment = align; 85 wordApp.Application.Selection.TypeText(context); 86 87 } 88 /// <summary> 89 /// 翻页 90 /// </summary> 91 public void ToNextPage() 92 { 93 object breakPage = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak; 94 wordApp.Selection.InsertBreak(ref breakPage); 95 } 96 /// <summary> 97 /// 焦点移动count段落 98 /// </summary> 99 /// <param name="count"></param> 100 public void MoveParagraph(int count) 101 { 102 object _count = count; 103 object wdP = WdUnits.wdParagraph;//换一段落 104 wordApp.Selection.Move(ref wdP, ref _count); 105 } 106 /// <summary> 107 /// 焦点移动count行 108 /// </summary> 109 /// <param name="count"></param> 110 public void MoveRow(int count) 111 { 112 object _count = count; 113 object WdLine = WdUnits.wdLine;//换一行 114 wordApp.Selection.Move(ref WdLine, ref _count); 115 } 116 /// <summary> 117 /// 焦点移动字符数 118 /// </summary> 119 /// <param name="count"></param> 120 public void MoveCharacter(int count) 121 { 122 object _count = count; 123 object wdCharacter = WdUnits.wdCharacter; 124 wordApp.Selection.Move(ref wdCharacter, ref _count); 125 } 126 /// <summary> 127 /// 插入段落 128 /// </summary> 129 public void ToNextParagraph() 130 { 131 wordApp.Selection.TypeParagraph();//插入段落 132 } 133 134 /// <summary> 135 /// 回车换行 136 /// </summary> 137 public void ToNextLine() 138 { 139 wordApp.Selection.TypeParagraph(); 140 } 141 /// <summary> 142 /// 当前位置插入图片 143 /// </summary> 144 /// <param name="picture"></param> 145 public void InsertPicture(string picture) 146 { 147 //图片居中显示 148 wordApp.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter; 149 wordApp.Application.Selection.InlineShapes.AddPicture(picture, ref defaultV, ref defaultV, ref defaultV); 150 } 151 /// <summary> 152 /// 添加表格 153 /// </summary> 154 /// <param name="rowNum"></param> 155 /// <param name="cellNum"></param> 156 /// <returns></returns> 157 public Table CreatTable(int rowNum, int cellNum) 158 { 159 return this._wordDocument.Tables.Add(wordApp.Selection.Range, rowNum, cellNum, ref defaultV, ref defaultV); 160 } 161 /// <summary> 162 /// 设置列宽 163 /// </summary> 164 /// <param name="widths"></param> 165 public void SetColumnWidth(float[] widths, Table tb) 166 { 167 if (widths.Length > 0) 168 { 169 int len = widths.Length; 170 for (int i = 0; i < len; i++) 171 { 172 tb.Columns[i].Width = widths[i]; 173 } 174 } 175 } 176 /// <summary> 177 /// 合并单元格 178 /// </summary> 179 /// <param name="tb"></param> 180 /// <param name="cells"></param> 181 public void MergeColumn(Table tb, Cell[] cells) 182 { 183 if (cells.Length > 1) 184 { 185 Cell c = cells[0]; 186 int len = cells.Length; 187 for (int i = 1; i < len; i++) 188 { 189 c.Merge(cells[i]); 190 } 191 } 192 wordApp.Selection.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter; 193 194 } 195 /// <summary> 196 /// 设置单元格内容 197 /// </summary> 198 /// <param name="_c"></param> 199 /// <param name="v"></param> 200 /// <param name="align">对齐方式</param> 201 public void SetCellValue(Cell _c, string v, WdParagraphAlignment align) 202 { 203 wordApp.Selection.ParagraphFormat.Alignment = align; 204 _c.Range.Text = v; 205 } 206 207 /// <summary> 208 /// 保存新文件 209 /// </summary> 210 public void SaveAsWord() 211 { 212 object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges; 213 try 214 { 215 object fileFormat = WdSaveFormat.wdFormatRTF; 216 _wordDocument.SaveAs(ref _newWord, ref fileFormat, ref defaultV, ref defaultV, ref defaultV, ref defaultV, ref defaultV, ref defaultV, ref defaultV, 217 ref defaultV, ref defaultV, ref defaultV, ref defaultV, ref defaultV, ref defaultV, ref defaultV); 218 } 219 catch (Exception e) 220 { 221 throw new Exception(e.Message); 222 223 } 224 finally 225 { 226 disponse(); 227 } 228 } 229 /// <summary> 230 /// 释放资源 231 /// </summary> 232 private void disponse() 233 { 234 object missingValue = Type.Missing; 235 object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges; 236 _wordDocument.Close(ref doNotSaveChanges, ref missingValue, ref missingValue); 237 wordApp.Application.Quit(ref defaultV, ref defaultV, ref defaultV); 238 _wordDocument = null; 239 wordApp = null; 240 } 241 } 242 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 private void button1_Click(object sender, System.EventArgs e) 2 { 3 //调用打开文件对话框获取要打开的文件WORD文件,RTF文件,文本文件路径名称 4 OpenFileDialog opd = new OpenFileDialog(); 5 opd.InitialDirectory = "c://"; 6 opd.Filter = "Word文档(*.doc)|*.doc|文本文档(*.txt)|*.txt|RTF文档(*.rtf)|*.rtf|所有文档(*.*)|*.*"; 7 opd.FilterIndex = 1; 8 9 if (opd.ShowDialog() == DialogResult.OK && opd.FileName.Length > 0) 10 { 11 12 //建立Word类的实例,缺点:不能正确读取表格,图片等等的显示 13 Word.ApplicationClass app = new Word.ApplicationClass(); 14 Word.Document doc = null; 15 object missing = System.Reflection.Missing.Value; 16 17 object FileName = opd.FileName; 18 object readOnly = false; 19 object isVisible = true; 20 object index = 0; 21 try 22 { 23 doc = app.Documents.Open(ref FileName, ref missing, ref readOnly, 24 ref missing, ref missing, ref missing, ref missing, ref missing, 25 ref missing, ref missing, ref missing, ref isVisible, ref missing, 26 ref missing, ref missing, ref missing); 27 28 doc.ActiveWindow.Selection.WholeStory(); 29 doc.ActiveWindow.Selection.Copy(); 30 //从剪切板获取数据 31 IDataObject data=Clipboard.GetDataObject(); 32 this.richTextBox1.Text=data.GetData(DataFormats.Text).ToString(); 33 34 } 35 finally 36 { 37 if (doc != null) 38 { 39 doc.Close(ref missing, ref missing, ref missing); 40 doc = null; 41 } 42 43 if (app != null) 44 { 45 app.Quit(ref missing, ref missing, ref missing); 46 app = null; 47 } 48 } 49 50 } 51 52 }
从数据库数据写到新的word,并保存:
来自:http://blog.csdn.net/liusen5555/article/details/2048755