asp.net中如何批量导出access某表内容到word文档

下面通过图文并茂的方式给大家介绍asp.net中批量导出access某表内容到word文档的方法,具体详情如下:

一、需求:

需要将表中每一条记录中的某些内容导出在一个word文档中,并将这些文档保存在指定文件夹目录下

二、界面,简单设计如下:

asp.net中如何批量导出access某表内容到word文档

三、添加office相关引用

asp.net中如何批量导出access某表内容到word文档

添加后可在解决方案资源管理器中看到:

asp.net中如何批量导出access某表内容到word文档

四、添加form1中的引用

using System.Data.OleDb; using System.Data.SqlClient; using System.IO; using Microsoft.Office.Core; using Word=Microsoft.Office.Interop.Word; using System.Reflection;

五、窗体Form1中代码如下:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.OleDb; using System.Data.SqlClient; using System.IO; using Microsoft.Office.Core; using Word=Microsoft.Office.Interop.Word; using System.Reflection; using System.Threading;//线程需用,进程中 namespace word { delegate void ShowProgressDelegate(int totalStep, int currentStep); //定义委托,异步调用 public partial class Form1 : Form { public Form1() { InitializeComponent(); } public string filepath = "D:\\zjy\\其他\\NCTDCBJYQ04.mdb"; //数据库所在位置设置 public string path; //输出路径 private void Form1_Load(object sender, EventArgs e) { string sqlstr = "select OBJECTID,CBFBM,CBFMC from CBF"; //string sqlstr = "select * from CBF"; DataSet ds = AccessDAO.getDataSetFromAccessTable(sqlstr, filepath); this.dataGridView1.DataSource = ds.Tables[0].DefaultView; dataGridView1.AllowUserToAddRows = false; } private void textBox1_MouseClick(object sender, MouseEventArgs e)//输出路径设置 { FolderBrowserDialog dilog = new FolderBrowserDialog(); dilog.Description = "请选择文件夹"; if (dilog.ShowDialog() == DialogResult.OK || dilog.ShowDialog() == DialogResult.Yes) { path = dilog.SelectedPath; this.textBox1.Text = path; } } object pathword; //声明文件路径变量 private void button2_Click(object sender, EventArgs e) //批量输出 { ParameterizedThreadStart start = new ParameterizedThreadStart(SetProgress); Thread progressThread = new Thread(start); progressThread.IsBackground = true;//标记为后台进程,在窗口退出时,正常退出 progressThread.Start(); } /// <summary> /// 刷新进度条 /// </summary> /// <param></param> /// <param></param> void ShowProgress(int totalStep, int currentStep) { this.progressBar1.Maximum = totalStep; this.progressBar1.Value = currentStep; if (this.progressBar1.Value * 100 / progressBar1.Maximum != 100) { this.label2.Text = "当前输出进度为:" + this.progressBar1.Value * 100 / progressBar1.Maximum + "%" + " 请耐心等待:)"; } else if (this.progressBar1.Value * 100 / progressBar1.Maximum == 100) { this.label2.Text = "输出结束!"; } } /// <summary> /// 设置当前进度 /// </summary> /// <param></param> void SetProgress(object state) { if (this.textBox1.Text == "") { MessageBox.Show("请选择文件输出路径", "提示"); } else { for (int i = 0; i < this.dataGridView1.Rows.Count; i++) //遍历获取table中需要的值,并分别创建word文档 { #region 打开进度条 Thread.Sleep(1); object[] objs = new object[] { this.dataGridView1.RowCount, i+1 }; //异步调用 this.Invoke(new ShowProgressDelegate(ShowProgress), objs); #endregion #region 获取word中需要添加的内容 string dm = this.dataGridView1.Rows[i].Cells[1].Value.ToString();//承包方编码 string mc = this.dataGridView1.Rows[i].Cells[2].Value.ToString();//承包方名称 #endregion #region 创建word文档,并将内容写入word,并保存起来 //初始化变量 object Nothing = Missing.Value; //COM调用时用于占位 object format = Word.WdSaveFormat.wdFormatDocument; //Word文档的保存格式 Word.ApplicationClass wordApp = new Word.ApplicationClass(); //声明一个wordAPP对象 Word.Document worddoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);//新建一个word对象 //向文档中写入内容 string wordstr = "承包方代码:" + dm + "\n" + "承包方名称:" + mc; worddoc.Paragraphs.Last.Range.Text = wordstr; //保存文档 pathword = path + "\\" + dm; //设置文件保存路径 worddoc.SaveAs(ref pathword, ref format, 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); //关闭文档 worddoc.Close(ref Nothing, ref Nothing, ref Nothing); //关闭worddoc文档对象 wordApp.Quit(ref Nothing, ref Nothing, ref Nothing); //关闭wordApp组对象 #endregion } MessageBox.Show("文档创建成功!","提示"); } } } }

六、读取数据库中表需要的数据库类AccessDAO.cs代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjwxxj.html