求助!!!如何将数据库表里的数据读出来,插入到已经设置好的word表格里,谢谢!
在C#winform编程里,如何将数据库表里的数据读出来,插入到已经设置好的word表格里?谢谢!
private static Microsoft.Office.Interop.Word._Application oWord =null; private static Microsoft.Office.Interop.Word._Document odoc = null; private static Microsoft.Office.Interop.Word._Document oDoc { get { if (odoc == null) { odoc = oWord.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); } return odoc; } set { if (value != null) { odoc = value; } } } //打开文档 public static bool Open(string filePath, bool isVisible) { try { oWord.Visible = isVisible; object path = filePath; oDoc = oWord.Documents.Open(ref path, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); return true; } catch (Exception) { return false; } } //操作表格 private void InsertVauleInTable() { Microsoft.Office.Interop.Word.Table table = oDoc.Tables[0];//假设操作word中的第一个表格 table.Cell(1, 1).Range.Text ="....";第一排第一单元格,填充值 table.Cell(1, 2).Range.Text ="....";第一排第二单元格,填充值 ...... } //保存文档 public static bool Save(string savePath, bool isClose) { try { object fileName = savePath; oDoc.SaveAs(ref fileName, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); if (isClose) { return CloseDocument(); } return true; } catch (Exception) { return false; } } //退出WORD进程 public static bool Quit() { try { object saveOption = Word.WdSaveOptions.wdDoNotSaveChanges; oWord.Quit(ref saveOption, ref Nothing, ref Nothing); oWord = null; return true; } catch (Exception) { return false; } }