Esempio n. 1
0
 public Table getTableByName(Document doc, string title, Microsoft.Office.Interop.Word.Application testWord)  //testWord下的doc文档下
 //找到标题为title的表格
 {
     doc.Activate();
     testWord.Selection.HomeKey(WdUnits.wdStory, Type.Missing);
     Find find = testWord.Selection.Find;
     find.Text = title;
     Selection currentSelect = testWord.Selection;
     bool ifFind = false;
     while (find.Execute())
     {
         string s = currentSelect.Paragraphs[1].Range.Text.Trim();
         if (s.Equals(title))
         {
             ifFind = true;
             break;
         }
     }
     if (ifFind == false)
     {
         return null;
     }
     while (currentSelect.Tables.Count == 0)
     {
         currentSelect.GoToNext(WdGoToItem.wdGoToLine);
     }
     Table table = currentSelect.Tables[1];
     return table;
 }
Esempio n. 2
0
        private void generateTOC(Document doc, ApplicationClass app)
        {
            doc.Activate();
            Object oMissing = System.Reflection.Missing.Value;
            Object oTrue = true;
            Object oFalse = false;
            Object oUpperHeadingLevel = "1";
            Object oLowerHeadingLevel = "3";
            Object oTOCTableID = "TableOfContents";

            app.Selection.Start = 0;
            app.Selection.End = 0;//将光标移动到文档开始位置  
            object beginLevel = 2;//目录开始深度  
            object endLevel = 2;//目录结束深度  

            object rightAlignPageNumber = true;// 指定页码右对其  

            doc.TablesOfContents.Add(app.Selection.Range, ref oTrue, ref oUpperHeadingLevel,
                ref oLowerHeadingLevel, ref oMissing, ref oTOCTableID, ref oTrue,
                ref oTrue, ref oMissing, ref oTrue, ref oTrue, ref oTrue);
        }
Esempio n. 3
0
        private Table getTable(Document doc)//移动文档光标到表格并获取表格
        {
            doc.Activate();//使doc文档为活动文档

            Selection currentSelect = testWord.Selection;
            while (currentSelect.Tables.Count == 0)
            {
                currentSelect.GoToNext(WdGoToItem.wdGoToLine);
            }
            Table table = currentSelect.Tables[1];
            return table;
        }
Esempio n. 4
0
 private Find getFind(Document doc, string s)
 {
     doc.Activate();
     testWord.Selection.Find.Text = s;
     return testWord.Selection.Find;
 }
Esempio n. 5
0
 private string[] getTableOfContents(Document doc)
 {
     doc.Activate();
     generateTOC(doc, testWord);//自动生成目录
     Range tocRange = doc.TablesOfContents[1].Range;
     string[] tableOfContents = delimiterTOC(tocRange.Text);
     return tableOfContents;
 }
Esempio n. 6
0
 public void highLightKeyWords(string keyWord,
                        Application testWord, Document doc, RichTextBox richTextBox, Label keyWordCountLabel, RichTextBox statisticsInfor)/*对当前testWord的Doc文档在richTextBox进行
                                                                                                                    * keyWord的高亮显示,并在listBox上显示
                                                                                                                     * 关键字个数和在staticticsInfor上输出数值信息*/
 {
     doc.Activate();
     string s = abstarctRichBoxString(keyWord, testWord);
     richTextBox.Clear();
     if (s == "")
     {
         richTextBox.Text = "此关键字不存在";
     }
     else
     {
         richTextBox.Text = s;
     }
     int sumLength = 0;//记录标识的绝对位置
     int length = keyWord.Length;
     int k = s.IndexOf(keyWord);
     int count = 0;
     int start = 0;
     while (k >= 0)
     {
         count++;
         start = sumLength + k;
         richTextBox.Select(start, length);
         richTextBox.SelectionBackColor = SysColor.ERROR;
         s = s.Remove(0, k + length);//缩短总的testPage
         sumLength += k + length;
         k = s.IndexOf(keyWord);
         getStatisticsInformation(count, k, s);
     }
     string t = "数目:" + count;
     if (keyWordCountLabel != null)
     {
         keyWordCountLabel.Text = t; ;//关键字统计的listBox赋值关键字个数
     }
     //System.Diagnostics.Debug.WriteLine(tests);
     if (statisticsInfor != null)
     {
         if (tests == "")
         {
             statisticsInfor.Text = "未找到该关键字的数值信息";
         }
         else
         {
             statisticsInfor.Text = tests;
         }
     }
     //清空前一次统计信息
     tests = "";
     list.Clear();
 }
Esempio n. 7
0
        private void OpenpdfFileByBrowser()
        {
            try
            {
                //save file to folder
                if (Page.IsPostBack)
                {
                    string fileName = Session["uploadedfile"].ToString();

                    FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Uploads/") + fileName);
                    ////convert file from doc to pfd
                    Document doc = new Document(fileName);
                    doc.Activate();

                 //  doc.SaveAs("d:/desktop/document.pdf", WdSaveFormat.wdFormatPDF); tempcomment

                    //temp code
                    string[] filePaths = Directory.GetFiles(Server.MapPath("~/PDFdoc/"));
                   List<ListItem> files = new List<ListItem>();
                   foreach (string filePath in filePaths)
                   {
                       files.Add(new ListItem(Path.GetFileName(filePath), filePath));
                   }
                   GridView1.DataSource = files;
                   GridView1.DataBind();

                    //temp code

                }
            }
            catch (Exception ex)
            {
                lblCatchError.Text = ex.Message.ToString();
            }
        }
Esempio n. 8
0
        public void highLightRichString(RichTextBox rbTableTest, Application testWord, Document doc,List<string>keyItemList)
        {
            doc.Activate();
            testWord.Selection.HomeKey(WdUnits.wdStory, Type.Missing);
            string richString = generateRichString(testWord,keyItemList);
            int[] countNumber = new int[keyItemList.Count];
            getCountNumber(testWord,countNumber,keyItemList);
            rbTableTest.Clear();
            if (richString == "")
            {
                rbTableTest.Text = "此文档不存在这些关键字信息";
                return;
            }
            else
            {
                rbTableTest.Text = richString;
            }
            int i = 0;
            int sumLength = 0;//记录标识的绝对位置
            
            foreach (string keyWord in keyItemList)
            {
                int length = keyWord.Length;
                int k = richString.IndexOf(keyWord);
                int count = 0;
                int start = 0;
                while (k >= 0)
                {
                    count++;
                    start = sumLength + k;
                    rbTableTest.Select(start, length);
                    rbTableTest.SelectionBackColor = SysColor.ERROR;
                    richString = richString.Remove(0, k + length);//缩短总的testPage
                    sumLength += k + length;
                    k = richString.IndexOf(keyWord);
                    if (count == countNumber[i])
                    {
                        break;
                    }
                }
                i++;
            }

        }
Esempio n. 9
0
        private void FileSelectorClick(object sender, EventArgs e)
        {
            OpenFileDialog im = new OpenFileDialog();
            if (im.ShowDialog() == DialogResult.OK)
            {
                wordDocPath = im.FileName;
                Console.WriteLine("File Selected is " + wordDocPath);

                //open word file here
                ap = new Microsoft.Office.Interop.Word.Application();
                ap.Application.Visible = true;
                try
                {
                    doc = ap.Documents.Open( @wordDocPath, ReadOnly: false, Visible: true );
                    doc.Activate();
                    ap.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateMinimize;

                    MessageBox.Show("Start taking screenshots by clicking on Capture button.");

                    this.Controls.Remove(fileSelector);
                    fileSelector.Dispose();
                    capture = new Button();
                    capture.Name = "captureButton";
                    capture.Text = "Capture";
                    capture.Size = new Size(this.Width - 200, this.Height - 60);
                    capture.Location = new System.Drawing.Point(
                        (this.Width - capture.Width) / 8,
                        (this.Height - capture.Height) / 6);
                    capture.Click += new System.EventHandler(this.CaptureClick);

                    this.Controls.Add(capture);
                }catch ( Exception ex )
                {
                    MessageBox.Show("Unable to open word document. Refer Console for proper error message.");
                    Console.WriteLine( "Exception Caught: " + ex.Message ); // Could be that the document is already open (/) or Word is in Memory(?)
                    // Ambiguity between method 'Microsoft.Office.Interop.Word._Application.Quit(ref object, ref object, ref object)' and non-method 'Microsoft.Office.Interop.Word.ApplicationEvents4_Event.Quit'. Using method group.
                    // ap.Quit( SaveChanges: false, OriginalFormat: false, RouteDocument: false );
                    try{
                        ( (_Application)ap ).Quit( SaveChanges: false, OriginalFormat: false, RouteDocument: false );
                        System.Runtime.InteropServices.Marshal.ReleaseComObject( ap );
                    }catch (Exception exception){
                        Console.WriteLine(exception.ToString());
                    }

                }
            }else{
                MessageBox.Show("Please select any word document to save screenshots");
            }
        }
Esempio n. 10
0
        private void btnGenerateNewPWs_Click(object sender, EventArgs e)
        {
            //killprocess("winword");
            app = new Microsoft.Office.Interop.Word.Application { Visible = false };
            
            System.Data.DataTable dt = new System.Data.DataTable();
            string qry = "SELECT * FROM ibis_pw_userlist WHERE ibis_pw_userlist_active = 1 AND ibis_pw_userlist_id > 1";
            //string qry = "SELECT * FROM ibis_pw_userlist WHERE ibis_pw_userlist_active = 1";

            Random randnum = new Random();
            int _min4 = 0;
            int _max4 = 9999;
            int _min6 = 0;
            int _max6 = 999999;

            using (MySqlConnection dbh = new MySqlConnection(Properties.Resources.DB_CONNSTR_HES))
            {
                dbh.Open();
                using (MySqlDataAdapter da = new MySqlDataAdapter(qry, dbh))
                {
                    da.Fill(dt);

                    foreach (DataRow row in dt.Rows)
                    {
                        doc = app.Documents.Add(@"C:\Users\dc1_000\Documents\HoldenEngineering\pw_memo_template.docx");
                        doc.Activate();

                        string newpw;
                        if (row["ibis_pw_userlist_un"].ToString().Equals("dholden"))
                        {
                            newpw = String.Format("{0:000000}", randnum.Next(_min6, _max6));
                        }
                        else
                        {
                            // HERE IS WHERE ONE COULD RANDOMIZE THE PLACEMENT OF
                            // THE ABBREVIATION RELATIVE TO THE FOUR-DIGIT NUMBER

                            newpw = row["ibis_pw_userlist_abbr"].ToString() + String.Format("{0:0000}", randnum.Next(_min4, _max4));
                        }

                        doc.Bookmarks["bmNameLbl"].Range.Text = row["ibis_pw_userlist_label"].ToString();
                        doc.Bookmarks["bmGenPW"].Range.Text = newpw;
                        doc.Bookmarks["bmUserName"].Range.Text = row["ibis_pw_userlist_un"].ToString();
                        doc.Bookmarks["bmOldPW"].Range.Text = row["ibis_pw_userlist_currpw"].ToString();
                        doc.Bookmarks["bmNewPW"].Range.Text = newpw;
                        doc.Bookmarks["bmNewPW2"].Range.Text = newpw;

                        doc.PrintOut();
                        ((Microsoft.Office.Interop.Word._Document)doc).Close(false, ref missing, ref missing);

                        try
                        {
                            MySqlCommand cmd = new MySqlCommand();
                            cmd.Connection = dbh;

                            cmd.CommandText = "UPDATE ibis_pw_userlist SET ibis_pw_userlist_currpw = @npw, ibis_pw_userlist_prevpw = @opw WHERE ibis_pw_userlist_id = @uid";
                            cmd.Prepare();

                            cmd.Parameters.AddWithValue("@npw", newpw);
                            cmd.Parameters.AddWithValue("@opw", row["ibis_pw_userlist_currpw"].ToString());
                            cmd.Parameters.AddWithValue("@uid", row["ibis_pw_userlist_id"].ToString());

                            cmd.ExecuteNonQuery();
                        }
                        catch (MySqlException mysqle)
                        {
                            MessageBox.Show("mysql ERROR: " + mysqle.ToString());
                            return;
                        }
                        catch (Exception m1ex)
                        {
                            MessageBox.Show("M1_ERROR: " + m1ex.ToString());
                            return;
                        }
                    }
                }
                dbh.Close();
            }
        }
Esempio n. 11
0
        private void btnPrintPWMemos_Click(object sender, EventArgs e)
        {
            app = new Microsoft.Office.Interop.Word.Application { Visible = false };

            System.Data.DataTable dt = new System.Data.DataTable();
            string qry = "SELECT * FROM ibis_pw_userlist WHERE ibis_pw_userlist_active = 1";

            using (MySqlConnection dbh = new MySqlConnection(Properties.Resources.DB_CONNSTR_HES))
            {
                dbh.Open();
                using (MySqlDataAdapter da = new MySqlDataAdapter(qry, dbh))
                {
                    da.Fill(dt);

                    foreach (DataRow row in dt.Rows)
                    {
                        doc = app.Documents.Add(@"C:\Users\dc1_000\Documents\HoldenEngineering\pw_memo_template.docx");
                        doc.Activate();

                        doc.Bookmarks["bmNameLbl"].Range.Text = row["ibis_pw_userlist_label"].ToString();
                        doc.Bookmarks["bmGenPW"].Range.Text = row["ibis_pw_userlist_currpw"].ToString();
                        doc.Bookmarks["bmUserName"].Range.Text = row["ibis_pw_userlist_un"].ToString();
                        doc.Bookmarks["bmOldPW"].Range.Text = row["ibis_pw_userlist_prevpw"].ToString();
                        doc.Bookmarks["bmNewPW"].Range.Text = row["ibis_pw_userlist_currpw"].ToString();
                        doc.Bookmarks["bmNewPW2"].Range.Text = row["ibis_pw_userlist_currpw"].ToString();

                        doc.PrintOut();
                        ((Microsoft.Office.Interop.Word._Document)doc).Close(false, ref missing, ref missing);
                    }
                }
                dbh.Close();
            }

        }
        private void cmdViewContracts_Click(object sender, EventArgs e)
        {
            string sotien;
            string benvay;
            string identi, ngaycap, noicap, soDT, Diachi, BankAccount, BankName;
            string makh, ngayhd, kyhan, laisuat, mahd;
            string sqlStr, strDate;
            int maloaiKH, length, i;
            double loinhuan;
            string strLoinhuan;
            object sourcefile;
            object destfile;
            DateTime ngaydaohan;
            TimeSpan ngaychiulai;
            OleDbDataReader oleReader;
            ApplicationClass word = new ApplicationClass();
            Document doc = new Document();

            if (cbxDonvitinh.Text == "Ngay")
                ngaydaohan = cbxDateContracts.Value.AddDays(int.Parse(cbxKyhan.Text));
            else if (cbxDonvitinh.Text == "Thang")
                ngaydaohan = cbxDateContracts.Value.AddMonths(int.Parse(cbxKyhan.Text));
            else ngaydaohan = cbxDateContracts.Value.AddYears(int.Parse(cbxKyhan.Text));
            ngaychiulai = ngaydaohan.Subtract(cbxDateContracts.Value);
            System.Diagnostics.Process Proc = new System.Diagnostics.Process();
            makh = cbxMaKH.Text;
            ngayhd = cbxDateContracts.Value.ToShortDateString();
            kyhan = cbxKyhan.Text;
            laisuat = cbxLaisuat.Text;
            sotien = txtTongtien.Text;
            length = sotien.Length;
            benvay = "";
            Diachi = "";
            soDT = "";
            ngaycap = "";
            identi = "";
            BankAccount = "";
            BankName = "";
            mahd = txtMaHD.Text.Replace("\\", "");
            mahd = mahd.Replace("/", "_");
            mahd = mahd.Replace(":", "_");
            mahd = mahd.Replace(" ", "_");
            mahd = mahd.Replace("*", "");
            mahd = mahd.Replace("<", "");
            mahd = mahd.Replace("|", "");
            mahd = mahd.Replace(">", "");
            strDate = cbxDateContracts.Value.ToShortDateString().Replace("/", "_");
            i = 1;
            loinhuan = Tinhloi(ngaychiulai, sotien, laisuat);
            strLoinhuan = loinhuan.ToString();
            while (3*i < length)
            {
                sotien = sotien.Insert(length - 3*i, ".");
                i++;
            }

            length = strLoinhuan.Length;
            i = 1;
            while (3 * i < length)
            {
                strLoinhuan = strLoinhuan.Insert(length - 3 * i, ".");
                i++;
            }

            sqlStr = "SELECT [TenKH], [DinhDanh], FORMAT([Ngaycap], 'dd/mm/yyyy') AS Ngaycap, [Noicap], [SoDT], [Diachi], [TaikhoanNH], [TenNH], [MaLoaiKH] FROM [KHACHHANG] WHERE [MaKH] = '" + makh + "'";
            maloaiKH = 0;
            try
            {
                oleReader = contractDb.genDataReader(sqlStr);
                if (oleReader.Read())
                {
                    benvay = oleReader["TenKH"].ToString();
                    identi = oleReader["Dinhdanh"].ToString();
                    ngaycap = oleReader["Ngaycap"].ToString();
                    noicap = oleReader["Noicap"].ToString();
                    soDT = oleReader["SoDT"].ToString();
                    Diachi = oleReader["Diachi"].ToString();
                    BankAccount = oleReader["TaikhoanNH"].ToString();
                    BankName = oleReader["TenNH"].ToString();
                    maloaiKH = int.Parse(oleReader["MaLoaiKH"].ToString());
                    benvay = benvay.Replace(" ", "");
                }
            }
            catch (Exception ex)
            {
                return;
            }

            //Ghi hop dong ra file
            if (cbxLoaiHD.Text == "Cho vay")
            {
                if (maloaiKH == 1)
                    //Khach hang doanh nghiep
                    //sourcefile = @"E:\Project\SVN\quanlydongtien\Source\Quanlydongtien\Quanlydongtien\bin\Temp\Hopdong\Khachhangdoanhnghiep.doc";
                    sourcefile = @dirWork + "\\Temp\\Hopdong\\Chovay\\Khachhangdoanhnghiep.doc";
                else
                    //Khach hang ca nhan
                    sourcefile = @dirWork + "\\Temp\\Hopdong\\Chovay\\Khachhangcanhan.doc";
            }
            else
            {
                if (maloaiKH == 1)
                    sourcefile = @dirWork + @"\Temp\Hopdong\Huydong\Khachhangdoanhnghiep.doc";
                else sourcefile = @dirWork + @"\Temp\Hopdong\Huydong\Khachhangcanhan.doc";
            }

            if (cbxLoaiHD.Text == "Cho vay")
                destfile = @dirWork + @"\Contracts\Chovay\" + mahd + "_" + strDate + ".doc";
            else destfile = @dirWork + @"\Contracts\Huydong\" + mahd + "_" + strDate + ".doc"; ;
            object missing = Type.Missing;

            try
            {
                doc = word.Documents.Open(ref sourcefile, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                doc.Activate();
                if (!Utilities.Replace_String_In_Word_File(ref doc, "#TEN KHACH HANG#", benvay))
                {
                    doc.Close(ref missing, ref missing, ref missing);
                    word.Application.Quit(ref missing, ref missing, ref missing);
                    return;
                    //doc.SaveAs(ref destfile, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                }

                if (!Utilities.Replace_String_In_Word_File(ref doc, "#Dia chi khach hang#", Diachi))
                {
                    doc.Close(ref missing, ref missing, ref missing);
                    word.Application.Quit(ref missing, ref missing, ref missing);
                }

                if (!Utilities.Replace_String_In_Word_File(ref doc, "#Dien thoai#", soDT))
                {
                    doc.Close(ref missing, ref missing, ref missing);
                    word.Application.Quit(ref missing, ref missing, ref missing);
                }

                if (!Utilities.Replace_String_In_Word_File(ref doc, "#Tong so tien#", sotien))
                {
                    doc.Close(ref missing, ref missing, ref missing);
                    word.Application.Quit(ref missing, ref missing, ref missing);
                }

                if (!Utilities.Replace_String_In_Word_File(ref doc, "#Laisuat#", laisuat))
                {
                    doc.Close(ref missing, ref missing, ref missing);
                    word.Application.Quit(ref missing, ref missing, ref missing);
                }

                if (!Utilities.Replace_String_In_Word_File(ref doc, "#Thoihan#", kyhan))
                {
                    doc.Close(ref missing, ref missing, ref missing);
                    word.Application.Quit(ref missing, ref missing, ref missing);
                }

                if (!Utilities.Replace_String_In_Word_File(ref doc, "#Donvi#", cbxDonvitinh.Text))
                {
                    doc.Close(ref missing, ref missing, ref missing);
                    word.Application.Quit(ref missing, ref missing, ref missing);
                }

                if (!Utilities.Replace_String_In_Word_File(ref doc, "#Ngayhopdong#", cbxDateContracts.Value.ToShortDateString()))
                {
                    doc.Close(ref missing, ref missing, ref missing);
                    word.Application.Quit(ref missing, ref missing, ref missing);
                }

                if (!Utilities.Replace_String_In_Word_File(ref doc, "#Ngaydaohan#", ngaydaohan.ToShortDateString()))
                {
                    doc.Close(ref missing, ref missing, ref missing);
                    word.Application.Quit(ref missing, ref missing, ref missing);
                }

                if (!Utilities.Replace_String_In_Word_File(ref doc, "#Phiuythacvon#", txtPhiUT.Text))
                {
                    doc.Close(ref missing, ref missing, ref missing);
                    word.Application.Quit(ref missing, ref missing, ref missing);
                }

                if (!Utilities.Replace_String_In_Word_File(ref doc, "#Account Numver#", BankAccount))
                {
                    doc.Close(ref missing, ref missing, ref missing);
                    word.Application.Quit(ref missing, ref missing, ref missing);
                }

                if (!Utilities.Replace_String_In_Word_File(ref doc, "#Bank Name#", BankName))
                {
                    doc.Close(ref missing, ref missing, ref missing);
                    word.Application.Quit(ref missing, ref missing, ref missing);
                }

                if (!Utilities.Replace_String_In_Word_File(ref doc, "#Loinhuan#", strLoinhuan))
                {
                    doc.Close(ref missing, ref missing, ref missing);
                    word.Application.Quit(ref missing, ref missing, ref missing);
                }

                doc.SaveAs(ref destfile, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                doc.Close(ref missing, ref missing, ref missing);
                word.Application.Quit(ref missing, ref missing, ref missing);
                Proc.StartInfo.FileName = @"WINWORD.EXE";
                Proc.StartInfo.Arguments = destfile.ToString();
                Proc.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                doc.Close(ref missing, ref missing, ref missing);
                word.Application.Quit(ref missing, ref missing, ref missing);
            }
        }