通过C#从数据库中读取内容生成WORD文档
2009-10-23 07:52
831 查看
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
相关文章推荐
- 将数据库的内容生成WORD文档
- C#生成PDF文档,读取TXT文件内容
- ASP 把数据库内容生成Word文档保存到目录下
- [转]ASP.NET C#如何读取word,写入word,复制内容到另一个word文档,批量修改文件名
- ASP.NET C#如何读取word,写入word,复制内容到另一个word文档,批量修改文件名
- ASP 把数据库内容生成Word文档保存到目录下
- c#生成的word文档,怎么控制选定的内容加下划线而不是整行加下划线
- C#通过读取配置文件里的内容连接数据库
- C#读取word文档中的内容
- C#生成PDF文档,读取TXT文件内容
- c#.net(B/s)读取默认配置文档内容及连接数据库
- C#读取word文档内容并显示
- C#生成PDF文档,读取TXT文件内容
- C#读取Word文档内容代码
- .NET1.1下,使用C#自动生成Word2003文档(通过操作COM组件实现)
- 能够按页号提取word文档文本内容的小程序,由C#实现
- 根据word模板生成word表格报表文档(C#)
- C#读取word文档文本
- C#程序通过模板自动创建Word文档.doc
- 如何在控制台程序中读取WORD文档的内容?