using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.OleDb; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.Threading; namespace SaveDoc { public partial class frmDbToDoc : Form { Db db = new Db(); public frmDbToDoc() { InitializeComponent(); CheckForIllegalCrossThreadCalls = false; } private void btnExecute_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtConn.Text.Trim())) { MessageBox.Show(null, "请填写数据库连接字符串!", "操作错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (string.IsNullOrEmpty(txtTable.Text.Trim())) { MessageBox.Show(null, "请填写数据表名称!", "操作错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if ((string.IsNullOrEmpty(txtName.Text.Trim())) || (string.IsNullOrEmpty(txtContent.Text.Trim()))) { MessageBox.Show(null, "请填写所有字段!", "操作错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (string.IsNullOrEmpty(txtSaveDir.Text.Trim())) { MessageBox.Show(null, "请选择保存目录!", "操作错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } Thread th = new Thread(new ThreadStart(Execute)); th.Start(); } private void Execute() { btnExecute.Enabled = false; btnSelectSaveDir.Enabled = false; string strFileIdSql = " select [" + txtName.Text.Trim() + "],[" + txtContent.Text.Trim() + "] from [" + txtTable.Text.Trim() + "] "; OleDbDataReader odr = db.GetList(txtConn.Text.Trim(), strFileIdSql); int i = 1; lblTotal.Text = "总计:" + db.GetCount(txtConn.Text.Trim(), " select count(1) from [" + txtTable.Text.Trim() + "] ").ToString(); while (odr.Read()) { string strFileName = odr[txtName.Text.Trim()].ToString(); string strContent = odr[txtContent.Text.Trim()].ToString(); strContent = System.Text.RegularExpressions.Regex.Replace(strContent, @"<[//]*br[^>]*>", "/n/r", System.Text.RegularExpressions.RegexOptions.IgnoreCase); strContent = strContent.Replace("<br>", "/n/r"); strContent = strContent.Replace("nbsp;", " "); strContent = strContent.Replace("nbsp", " "); SaveToWord(txtSaveDir.Text.Trim(), strFileName, strContent); lblCompleted.Text = "已处理:" + i.ToString(); i = i + 1; } MessageBox.Show(null, "处理完成!", "处理完成", MessageBoxButtons.OK, MessageBoxIcon.Information); btnExecute.Enabled = true; btnSelectSaveDir.Enabled = true; } private void SaveToWord(string strDir, string strFileName, string strContent) { Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application(); object vOpt = System.Reflection.Missing.Value; try { string txtf = Path.Combine(strDir, strFileName) + ".doc"; object f = (object)txtf; Microsoft.Office.Interop.Word.Document vDoc = oWord.Documents.Add(ref vOpt, ref vOpt, ref vOpt, ref vOpt); //Microsoft.Office.Interop.Word.Document vDoc = oWord.Documents.Open(ref f, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt); txtf = GetFileName(txtf); //if (File.Exists(txtf)) //{ // Random rand = new Random(); // txtf = txtf.Replace(".doc", "_" + rand.Next(10).ToString() + ".doc"); // //File.Delete(txtf); //} object otxt = (object)txtf; //oWord.Application.Selection.Text = strContent; oWord.Application.Selection.TypeText(strContent); object saveFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatText; object encoding = (object)System.Text.Encoding.UTF8; vDoc.SaveAs(ref otxt, ref saveFormat, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt, ref vOpt); vDoc.Close(ref vOpt, ref vOpt, ref vOpt); } catch { } finally { oWord.Quit(ref vOpt, ref vOpt, ref vOpt); oWord = null; } } private string GetFileName(string strName) { if (File.Exists(strName)) { Random rand = new Random(); strName = strName.Replace(".doc", "_" + rand.Next(10).ToString() + ".doc"); strName = GetFileName(strName); } return strName; } private void btnSelectSaveDir_Click(object sender, EventArgs e) { folderBrowser.ShowDialog(); txtSaveDir.Text = folderBrowser.SelectedPath; } } } 需要引用Interop.Microsoft.Office.Interop.Word.dll和Interop.Word.dll