コード例 #1
0
        private object G_str_path;                         //定义文件保存路径字段

        private void btn_New_Click(object sender, EventArgs e)
        {
            btn_New.Enabled = false;                                                  //停用新建按钮
            ThreadPool.QueueUserWorkItem(                                             //开始线程池
                (pp) =>                                                               //使用lambda表达式
            {
                G_wa               = new Microsoft.Office.Interop.Word.Application(); //创建应用程序对象
                object P_obj       = "Normal.dot";                                    //定义文档模板
                Word.Document P_wd = G_wa.Documents.Add(                              //向Word应用程序中添加文档
                    ref P_obj, ref G_missing, ref G_missing, ref G_missing);
                Word.Range P_Range = P_wd.Range(                                      //得到文档范围
                    ref G_missing, ref G_missing);
                string P_Server   = string.Empty;                                     //定义字符串变量用于存放服务器信息
                string P_DataBase = string.Empty;                                     //定义字符串变量用于存放数据库名称
                string P_UserName = string.Empty;                                     //定义字符串变量用于存放用户名
                string P_PassWord = string.Empty;                                     //定义字符串变量用于存放密码
                this.Invoke(
                    (MethodInvoker)(() =>
                {
                    P_Server = txt_Server.Text;                     //得到数据库服务器信息
                    P_DataBase = txt_DataBase.Text;                 //得到数据库名称
                    P_UserName = txt_UserName.Text;                 //得到数据库用户名
                    P_PassWord = txt_PassWord.Text;                 //得到数据库密码
                }));
                DataTier P_DataTier = new DataTier(                 //创建数据层对象
                    P_Server, P_DataBase, P_UserName, P_PassWord);
                List <string> P_List_Str = P_DataTier.GetMessage(); //得到数据集合
                foreach (string P_str in P_List_Str)                //遍历数据集合
                {
                    P_Range.Text += P_str;                          //向文档中添加数据
                }
                G_str_path = string.Format(                         //计算文件保存路径
                    @"{0}\{1}", G_FolderBrowserDialog.SelectedPath,
                    DateTime.Now.ToString("yyyy年M月d日h时s分m秒fff毫秒") + ".doc");
                P_wd.SaveAs(    //保存Word文件
                    ref G_str_path,
                    ref G_missing, ref G_missing, ref G_missing, ref G_missing,
                    ref G_missing, ref G_missing, ref G_missing, ref G_missing,
                    ref G_missing, ref G_missing, ref G_missing, ref G_missing,
                    ref G_missing, ref G_missing, ref G_missing);
                ((Word._Application)G_wa.Application).Quit( //退出应用程序
                    ref G_missing, ref G_missing, ref G_missing);
                this.Invoke(                                //调用窗体线程
                    (MethodInvoker)(() =>                   //使用lambda表达式
                {
                    MessageBox.Show(                        //提示已经创建Word
                        "成功创建Word文档!", "提示!");
                    btn_display.Enabled = true;             //启用显示按钮
                }));
            });
        }
コード例 #2
0
        private object G_str_path;                         //定義文件儲存路徑欄位

        private void btn_New_Click(object sender, EventArgs e)
        {
            btn_New.Enabled = false;                                    //停用新建按鈕
            ThreadPool.QueueUserWorkItem(                               //開始線程池
                (pp) =>                                                 //使用lambda表達式
            {
                G_wa = new Microsoft.Office.Interop.Word.Application(); //建立應用程式對像

                Word.Document P_wd = G_wa.Documents.Add();
                Word.Range P_Range = P_wd.Range(   //得到文件檔範圍
                    ref G_missing, ref G_missing);
                string P_Server   = string.Empty;  //定義字串變數用於存放伺服器訊息
                string P_DataBase = string.Empty;  //定義字串變數用於存放資料庫名稱
                string P_UserName = string.Empty;  //定義字串變數用於存放用戶名
                string P_PassWord = string.Empty;  //定義字串變數用於存放密碼
                this.Invoke(
                    (MethodInvoker)(() =>
                {
                    P_Server = txt_Server.Text;                     //得到資料庫伺服器訊息
                    P_DataBase = txt_DataBase.Text;                 //得到資料庫名稱
                    P_UserName = txt_UserName.Text;                 //得到資料庫用戶名
                    P_PassWord = txt_PassWord.Text;                 //得到資料庫密碼
                }));
                DataTier P_DataTier = new DataTier(                 //建立資料層對像
                    P_Server, P_DataBase, P_UserName, P_PassWord);
                List <string> P_List_Str = P_DataTier.GetMessage(); //得到資料集合
                foreach (string P_str in P_List_Str)                //深度搜尋資料集合
                {
                    P_Range.Text += P_str;                          //向文件檔中新增資料
                }
                G_str_path = string.Format(                         //計算文件儲存路徑
                    @"{0}\{1}", G_FolderBrowserDialog.SelectedPath,
                    DateTime.Now.ToString("yyyy年M月d日h時s分m秒fff毫秒") + ".doc");
                P_wd.SaveAs(    //儲存Word文件
                    ref G_str_path,
                    ref G_missing, ref G_missing, ref G_missing, ref G_missing,
                    ref G_missing, ref G_missing, ref G_missing, ref G_missing,
                    ref G_missing, ref G_missing, ref G_missing, ref G_missing,
                    ref G_missing, ref G_missing, ref G_missing);
                ((Word._Application)G_wa.Application).Quit( //退出應用程式
                    ref G_missing, ref G_missing, ref G_missing);
                this.Invoke(                                //呼叫視窗線程
                    (MethodInvoker)(() =>                   //使用lambda表達式
                {
                    MessageBox.Show(                        //提示已經建立Word
                        "成功建立Word文件檔!", "提示!");
                    btn_display.Enabled = true;             //啟用顯示按鈕
                }));
            });
        }