批量导出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[].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 * / progressBar1.Maximum != )
{
this.label2.Text = "当前输出进度为:" + this.progressBar1.Value * / progressBar1.Maximum + "%" + " 请耐心等待:)";
}
else if (this.progressBar1.Value * / progressBar1.Maximum == )
{
this.label2.Text = "输出结束!";
}
} /// <summary>
/// 设置当前进度
/// </summary>
/// <param name="state"></param>
void SetProgress(object state)
{
if (this.textBox1.Text == "")
{
MessageBox.Show("请选择文件输出路径", "提示");
}
else
{
for (int i = ; i < this.dataGridView1.Rows.Count; i++) //遍历获取table中需要的值,并分别创建word文档
{
#region 打开进度条
Thread.Sleep();
object[] objs = new object[] { this.dataGridView1.RowCount, i+ };
//异步调用
this.Invoke(new ShowProgressDelegate(ShowProgress), objs);
#endregion #region 获取word中需要添加的内容
string dm = this.dataGridView1.Rows[i].Cells[].Value.ToString();//承包方编码
string mc = this.dataGridView1.Rows[i].Cells[].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文档了
批量导出access某表内容到word文档的更多相关文章
- Java 导出数据库表信息生成Word文档
一.前言 最近看见朋友写了一个导出数据库生成word文档的业务,感觉很有意思,研究了一下,这里也拿出来与大家分享一波~ 先来看看生成的word文档效果吧 下面我们也来一起简单的实现吧 二.Java 导 ...
- C#导出文本内容到word文档源码
将做工程过程中较好的代码片段珍藏起来,下面的代码内容是关于C#导出文本内容到word文档的代码,希望能对小伙伴们也有好处.<%@ Page Language="C#" Aut ...
- 用注册表清除Office Word文档杀手病毒
不久前,笔者打开word文件时遇到了一件离奇的怪事,常用的Word文件怎么也打不开,总是出现提示框:"版本冲突:无法打开高版本的word文档".再仔细查看,文件夹里竟然有两个名字一 ...
- java通过freemarker导出包含富文本图片的word文档
废话不多说,进入正题! 本文重点在于:对富文本图片的导出(基础的freemarker+word模板导出这里不做详细解说哈) 参考文章:http://www.cnblogs.com/liaofeifig ...
- 孤荷凌寒自学python第七十九天开始写Python的第一个爬虫9并使用pydocx模块将结果写入word文档
孤荷凌寒自学python第七十九天开始写Python的第一个爬虫9 (完整学习过程屏幕记录视频地址在文末) 今天在上一天的基础上继续完成对我的第一个代码程序的书写. 到今天终于完成了对docx模块针对 ...
- Java生成word文档
itext-rtf-2.1.7.jar,下载地址:http://download.csdn.net/detail/xuxu198899223/7717727 itext-2.1.7.jar 下载地址: ...
- c#word文档输出
在工作中有时需要把内容用word文档展示出来 在写代码前要引用word的dll Microsoft.Office.Interop.Word“ sing System; using System.Col ...
- C# 导出word文档及批量导出word文档(4)
接下来是批量导出word文档和批量打印word文件,批量导出word文档和批量打印word文件的思路差不多,只是批量打印不用打包压缩文件,而是把所有文件合成一个word,然后通过js来调用 ...
- C# 导出word文档及批量导出word文档(3)
在初始化WordHelper时,要获取模板的相对路径.获取文档的相对路径多个地方要用到,比如批量导出时要先保存文件到指定路径下,再压缩打包下载,所以专门写了个关于获取文档的相对路径的类. #regio ...
随机推荐
- ASP.Net MVC开发基础学习笔记:一、走向MVC模式
一.ASP.Net的两种开发模式 1.1 ASP.Net WebForm的开发模式 (1)处理流程 在传统的WebForm模式下,我们请求一个例如http://www.aspnetmvc.com/bl ...
- Web前端开发工程师养成计划【转载】
Web前端开发工程师养成计划(入门篇) 最原始的忠告:这个世界上有想法的人很多,但是有想法又能实现它的人太少! 首先要感谢伟大的Web2.0概念.产品概念.用户体验概念.jQuery插件,是它们在中国 ...
- 实现Div拖拽
直观的理解div拖拽:当鼠标对着可拖拽部分按住后并拖动,div会跟着鼠标一起运动,并且其运动空间限制在浏览器内部,当放开鼠标时,则div停止运动. 实现div拖拽需要三个重要的事件: (1)onmou ...
- SQL Server 深入解析索引存储(上)
标签:SQL SERVER/MSSQL SERVER/数据库/DBA/索引体系结构/堆/聚集索引 概述 最近要分享一个课件就重新把这块知识整理了一遍出来,篇幅有点长,想要理解的透彻还是要上机实践. 聚 ...
- 逻辑回归(LR)总结复习
摘要: 1.算法概述 2.算法推导 3.算法特性及优缺点 4.注意事项 5.实现和具体例子 6.适用场合 内容: 1.算法概述 最基本的LR分类器适合于对两分类(类0,类1)目标进行分类:这个模型以样 ...
- salesforce 零基础学习(四十五)Approval Lock & UnLock相关注意事项
我们都知道,当一条记录进入审批流程以后会自动加锁,apex提供Approval类的lock和unlock方法可以让我们使用代码对记录进行加锁和解锁. 项目中遇到一个需求,需要当某种情况下对记录进行先解 ...
- JSON和JS对象之间的互转
1. jQuery插件支持的转换方式 $.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符串转换成json对象 2. 浏览器支持的 ...
- Android 设置对话框全屏
1.在styles.xml中添加一个style: <style name="Dialog_Fullscreen"> <item name="androi ...
- 【开源】OSharp框架解说系列(2.1):EasyUI的后台界面搭建及极致重构
OSharp是什么? OSharp是个快速开发框架,但不是一个大而全的包罗万象的框架,严格的说,OSharp中什么都没有实现.与其他大而全的框架最大的不同点,就是OSharp只做抽象封装,不做实现.依 ...
- JavaScrict中的断言调试
今天在看忍者秘籍的时候,看到一个断言方法.查阅了一下资料,原来javascript中的console也包含这个方法.具体用法如下: <script type="text/javascr ...