asp.net中如何批量导出access某表内容到word文档
程序员文章站
2024-02-13 13:22:16
下面通过图文并茂的方式给大家介绍asp.net中批量导出access某表内容到word文档的方法,具体详情如下:
一、需求:
需要将表中每一条记录中的某些内...
下面通过图文并茂的方式给大家介绍asp.net中批量导出access某表内容到word文档的方法,具体详情如下:
一、需求:
需要将表中每一条记录中的某些内容导出在一个word文档中,并将这些文档保存在指定文件夹目录下
二、界面,简单设计如下:
三、添加office相关引用
添加后可在解决方案资源管理器中看到:
四、添加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 name="totalstep"></param> /// <param name="currentstep"></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 name="state"></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代码如下:
using system; using system.collections.generic; using system.linq; using system.text; using system.data; using system.data.oledb; using system.data.sqlclient; using system.text.regularexpressions; //正则表达式引用所需 namespace word { //access的数据访问接口 class accessdao { public static class property { public static string accessfilepath = "d:\\nctdcbjyq04dataset.mdb"; //若放入主程序,则可如下设置 //one mainfrm = (one)this.owner; //string prjname = mainfrm.laprj.text; //string prjpath = mainfrm.lafile_path.text; // public static string accessfilepath = prjpath + "\\矢量数据\\" + prjname + ".mdb"; } //从access数据库获取数据 //datafilepath指定access文件的路径 //sql指定数据库的查询语句 //dataset为查询返回的数据集 public static dataset getdatasetfromaccesstable(string sql, string datafilepath) { // 连接数据库 oledbconnection connct = new oledbconnection(); string oledb = "provider=microsoft.jet.oledb.4.0;data source=" + datafilepath; connct.connectionstring = oledb; //创建命令 oledbcommand command = new oledbcommand(sql, connct); //打开数据库 connct.open(); //执行命令 dataset dataset = new dataset(); oledbdataadapter dataadapter = new oledbdataadapter(command); dataadapter.fill(dataset); // 关闭连接 connct.close(); return dataset; } //更新或者插入数据到access数据库 //datafilepath指定access文件的路径 //sql指定数据库的更新或者插入语句 //返回值int表示此次更新影响的行数 public static int updateaccesstable(string sql, string datafilepath) { // 连接数据库 oledbconnection connct = new oledbconnection(); string oledb = "provider=microsoft.jet.oledb.4.0;data source=" + datafilepath; connct.connectionstring = oledb; //打开数据库 connct.open(); //执行命令 oledbcommand mycommand = new oledbcommand(sql, connct); int res = mycommand.executenonquery(); // 关闭连接 connct.close(); return res; } //更新或者插入数据到access数据库 //datafilepath指定access文件的路径 //command指定操作(更新或者插入)数据库的命令 //返回值int表示此次更新影响的行数 public static int updateaccesstable(oledbcommand command, string datafilepath) { // 连接数据库 oledbconnection connct = new oledbconnection(); string oledb = "provider=microsoft.jet.oledb.4.0;data source=" + datafilepath; connct.connectionstring = oledb; //打开数据库 connct.open(); //执行命令 //oledbcommand mycommand = new oledbcommand(sql, connct); command.connection = connct; int res = command.executenonquery(); // 关闭连接 connct.close(); return res; } public bool ckdigital_num(string digitalitem, int digitalnum) //正则检查是否为数字,且位数一定 { bool isdigital_num = false; regex regen = new regex(@"^\d{" + digitalnum.tostring("f0") + "}$"); //正则表达式,n位数字 if (regen.ismatch(digitalitem)) isdigital_num = true; return isdigital_num; } } }
ok了,至此就可完成批量导出成word文档了