濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > asp.net中如何批量導(dǎo)出access某表內(nèi)容到word文檔

asp.net中如何批量導(dǎo)出access某表內(nèi)容到word文檔

熱門(mén)標(biāo)簽:上海市三維地圖標(biāo)注 西寧電銷(xiāo)外呼系統(tǒng)公司 云南外呼系統(tǒng)代理 辦公用地圖標(biāo)注網(wǎng)點(diǎn)怎么操作 聊城智能電銷(xiāo)機(jī)器人電話 寧德防封版電銷(xiāo)卡 海東防封電銷(xiāo)卡 安陸市地圖標(biāo)注app 南昌自動(dòng)外呼系統(tǒng)線路

下面通過(guò)圖文并茂的方式給大家介紹asp.net中批量導(dǎo)出access某表內(nèi)容到word文檔的方法,具體詳情如下:

一、需求:

 需要將表中每一條記錄中的某些內(nèi)容導(dǎo)出在一個(gè)word文檔中,并將這些文檔保存在指定文件夾目錄下

二、界面,簡(jiǎn)單設(shè)計(jì)如下:

三、添加office相關(guān)引用

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

四、添加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;//線程需用,進(jìn)程中
namespace word
{
 delegate void ShowProgressDelegate(int totalStep, int currentStep); //定義委托,異步調(diào)用
 public partial class Form1 : Form
 {
  public Form1()
  {
   InitializeComponent();
  }
  public string filepath = "D:\\zjy\\其他\\NCTDCBJYQ04.mdb"; //數(shù)據(jù)庫(kù)所在位置設(shè)置
  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)//輸出路徑設(shè)置
  {
   FolderBrowserDialog dilog = new FolderBrowserDialog();
   dilog.Description = "請(qǐng)選擇文件夾";
   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;//標(biāo)記為后臺(tái)進(jìn)程,在窗口退出時(shí),正常退出
   progressThread.Start();
  }
   /// summary>
  /// 刷新進(jìn)度條
  /// /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 = "當(dāng)前輸出進(jìn)度為:" + this.progressBar1.Value * 100 / progressBar1.Maximum + "%" + " 請(qǐng)耐心等待:)";
   }
   else if (this.progressBar1.Value * 100 / progressBar1.Maximum == 100)
   {
    this.label2.Text = "輸出結(jié)束!";
   }
  }
  /// summary>
  /// 設(shè)置當(dāng)前進(jìn)度
  /// /summary>
  /// param name="state">/param>
  void SetProgress(object state)
  {
   if (this.textBox1.Text == "")
   {
    MessageBox.Show("請(qǐng)選擇文件輸出路徑", "提示");
   }
   else
   {
    for (int i = 0; i  this.dataGridView1.Rows.Count; i++) //遍歷獲取table中需要的值,并分別創(chuàng)建word文檔
    {
     #region 打開(kāi)進(jìn)度條
     Thread.Sleep(1);
     object[] objs = new object[] { this.dataGridView1.RowCount, i+1 };
     //異步調(diào)用
     this.Invoke(new ShowProgressDelegate(ShowProgress), objs);
     #endregion
     #region 獲取word中需要添加的內(nèi)容
     string dm = this.dataGridView1.Rows[i].Cells[1].Value.ToString();//承包方編碼
     string mc = this.dataGridView1.Rows[i].Cells[2].Value.ToString();//承包方名稱
     #endregion
     #region 創(chuàng)建word文檔,并將內(nèi)容寫(xiě)入word,并保存起來(lái)
     //初始化變量
     object Nothing = Missing.Value;      //COM調(diào)用時(shí)用于占位
     object format = Word.WdSaveFormat.wdFormatDocument; //Word文檔的保存格式
     Word.ApplicationClass wordApp = new Word.ApplicationClass();    //聲明一個(gè)wordAPP對(duì)象
     Word.Document worddoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);//新建一個(gè)word對(duì)象
     //向文檔中寫(xiě)入內(nèi)容
     string wordstr = "承包方代碼:" + dm + "\n" + "承包方名稱:" + mc;
     worddoc.Paragraphs.Last.Range.Text = wordstr;
     //保存文檔   
     pathword = path + "\\" + dm; //設(shè)置文件保存路徑
     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);
     //關(guān)閉文檔
     worddoc.Close(ref Nothing, ref Nothing, ref Nothing); //關(guān)閉worddoc文檔對(duì)象
     wordApp.Quit(ref Nothing, ref Nothing, ref Nothing); //關(guān)閉wordApp組對(duì)象
     #endregion
    }
    MessageBox.Show("文檔創(chuàng)建成功!","提示");
   }   
  }    
 }
}

六、讀取數(shù)據(jù)庫(kù)中表需要的數(shù)據(jù)庫(kù)類(lèi)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; //正則表達(dá)式引用所需
namespace word
{
 //access的數(shù)據(jù)訪問(wèn)接口
 class AccessDAO
 {
  public static class Property
  {
   public static string accessFilePath = "d:\\nCTDCBJYQ04DataSet.mdb";
   //若放入主程序,則可如下設(shè)置
   //one mainFrm = (one)this.Owner;
   //string prjName = mainFrm.laPrj.Text;
   //string prjPath = mainFrm.laFile_Path.Text;
   // public static string accessFilePath = prjPath + "\\矢量數(shù)據(jù)\\" + prjName + ".mdb";
  }
  //從access數(shù)據(jù)庫(kù)獲取數(shù)據(jù)
  //dataFilePath指定access文件的路徑
  //sql指定數(shù)據(jù)庫(kù)的查詢語(yǔ)句
  //DataSet為查詢返回的數(shù)據(jù)集
  public static DataSet getDataSetFromAccessTable(string sql, string dataFilePath)
  {
   // 連接數(shù)據(jù)庫(kù) 
   OleDbConnection connct = new OleDbConnection();
   string oleDB = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dataFilePath;
   connct.ConnectionString = oleDB;
   //創(chuàng)建命令
   OleDbCommand command = new OleDbCommand(sql, connct);
   //打開(kāi)數(shù)據(jù)庫(kù)
   connct.Open();
   //執(zhí)行命令
   DataSet dataSet = new DataSet();
   OleDbDataAdapter dataAdapter = new OleDbDataAdapter(command);
   dataAdapter.Fill(dataSet);
   // 關(guān)閉連接 
   connct.Close();
   return dataSet;
  }
  //更新或者插入數(shù)據(jù)到access數(shù)據(jù)庫(kù)
  //dataFilePath指定access文件的路徑
  //sql指定數(shù)據(jù)庫(kù)的更新或者插入語(yǔ)句
  //返回值int表示此次更新影響的行數(shù)
  public static int updateAccessTable(string sql, string dataFilePath)
  {
   // 連接數(shù)據(jù)庫(kù) 
   OleDbConnection connct = new OleDbConnection();
   string oleDB = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dataFilePath;
   connct.ConnectionString = oleDB;
   //打開(kāi)數(shù)據(jù)庫(kù)
   connct.Open();
   //執(zhí)行命令
   OleDbCommand myCommand = new OleDbCommand(sql, connct);
   int res = myCommand.ExecuteNonQuery();
   // 關(guān)閉連接 
   connct.Close();
   return res;
  }
  //更新或者插入數(shù)據(jù)到access數(shù)據(jù)庫(kù)
  //dataFilePath指定access文件的路徑
  //command指定操作(更新或者插入)數(shù)據(jù)庫(kù)的命令
  //返回值int表示此次更新影響的行數(shù)
  public static int updateAccessTable(OleDbCommand command, string dataFilePath)
  {
   // 連接數(shù)據(jù)庫(kù) 
   OleDbConnection connct = new OleDbConnection();
   string oleDB = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dataFilePath;
   connct.ConnectionString = oleDB;
   //打開(kāi)數(shù)據(jù)庫(kù)
   connct.Open();
   //執(zhí)行命令
   //OleDbCommand myCommand = new OleDbCommand(sql, connct);
   command.Connection = connct;
   int res = command.ExecuteNonQuery();
   // 關(guān)閉連接 
   connct.Close();
   return res;
  }
  public bool ckDigital_Num(string digitalItem, int digitalNum)    //正則檢查是否為數(shù)字,且位數(shù)一定
  {
   bool isDigital_Num = false;
   Regex reGen = new Regex(@"^\d{" + digitalNum.ToString("F0") + "}$");     //正則表達(dá)式,n位數(shù)字
   if (reGen.IsMatch(digitalItem))
    isDigital_Num = true;
   return isDigital_Num;
  }
 }
}

ok了,至此就可完成批量導(dǎo)出成word文檔了

您可能感興趣的文章:
  • asp.net 按指定模板導(dǎo)出word,pdf實(shí)例代碼
  • asp.net+Ligerui實(shí)現(xiàn)grid導(dǎo)出Excel和Word的方法
  • asp.net實(shí)現(xiàn)導(dǎo)出DataTable數(shù)據(jù)到Word或者Excel的方法
  • ASP.NET MVC 導(dǎo)出Word報(bào)表
  • ASP.NET導(dǎo)出word實(shí)例

標(biāo)簽:崇左 汕尾 衢州 贛州 洛陽(yáng) 南寧 青海

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《asp.net中如何批量導(dǎo)出access某表內(nèi)容到word文檔》,本文關(guān)鍵詞  asp.net,中,如何,批量,導(dǎo)出,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《asp.net中如何批量導(dǎo)出access某表內(nèi)容到word文檔》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于asp.net中如何批量導(dǎo)出access某表內(nèi)容到word文檔的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    察隅县| 三穗县| 荣成市| 沽源县| 白山市| 嫩江县| 正蓝旗| 桃园市| 扎兰屯市| 澳门| 庆城县| 西贡区| 津市市| 米泉市| 宜阳县| 元江| 平果县| 怀柔区| 七台河市| 鹤峰县| 祁连县| 武威市| 内乡县| 巴南区| 浠水县| 华容县| 湖州市| 吉首市| 普陀区| 大化| 海南省| 永修县| 正镶白旗| 涿鹿县| 保康县| 海宁市| 韩城市| 张家口市| 垦利县| 来凤县| 广东省|