Beispiel #1
0
    /// <summary>
    /// word转pdf
    /// </summary>
    /// <param name="wordPath">word路径</param>
    /// <param name="pdfPath">pdf路径</param>
    /// <returns></returns>
    public static bool WordToPDF(string wordPath, string pdfPath)
    {
        bool result = false;

        Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
        Microsoft.Office.Interop.Word.Document    document    = null;
        try
        {
            //document.PageSetup.PaperSize = Microsoft.Office.Interop.Word.WdPaperSize.wdPaperA4;
            application.Visible = false;
            document            = application.Documents.Open(wordPath);
            document.ExportAsFixedFormat(pdfPath, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF);
            result = true;
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            result = false;
        }
        finally
        {
            document.Close();
        }
        return(result);
    }
Beispiel #2
0
    /*-------------------------word转化----------------------------------------------------------*/
    private static void WordToHtmlFile(string WordFilePath)
    {
        Microsoft.Office.Interop.Word.Application newApp = new Microsoft.Office.Interop.Word.Application();
        // 指定原文件和目标文件
        object Source       = WordFilePath;
        string SaveHtmlPath = WordFilePath.Substring(0, WordFilePath.Length - 3) + "html";
        object Target       = SaveHtmlPath;

        // 缺省参数
        object Unknown = Type.Missing;

        //为了保险,只读方式打开
        object readOnly = true;

        // 打开doc文件
        Microsoft.Office.Interop.Word.Document doc = newApp.Documents.Open(ref Source, ref Unknown,
                                                                           ref readOnly, ref Unknown, ref Unknown,
                                                                           ref Unknown, ref Unknown, ref Unknown,
                                                                           ref Unknown, ref Unknown, ref Unknown,
                                                                           ref Unknown, ref Unknown, ref Unknown,
                                                                           ref Unknown, ref Unknown);

        // 指定另存为格式(rtf)
        object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;

        // 转换格式
        doc.SaveAs(ref Target, ref format,
                   ref Unknown, ref Unknown, ref Unknown,
                   ref Unknown, ref Unknown, ref Unknown,
                   ref Unknown, ref Unknown, ref Unknown,
                   ref Unknown, ref Unknown, ref Unknown,
                   ref Unknown, ref Unknown);

        // 关闭文档和Word程序
        doc.Close(ref Unknown, ref Unknown, ref Unknown);
        newApp.Quit(ref Unknown, ref Unknown, ref Unknown);
    }
Beispiel #3
0
    protected void Button2_Click(object sender, EventArgs e)
    {
        SQLtoWord SW = new SQLtoWord();

        Button2.Enabled = false;

        string  str = "SELECT mt.[year], mt.Depart, mt.[time], mt.teacher_Name, mt.specify, mt.guiNum, mt.IsOne,mt.IsTaskGive, mt.IsrefRead, mt.IsGuided, mt.totalsituation, ms.student_Name, ms.title, ms.kind AS kind, ms.Ischanged, ms.IsReq, ms.IsCom, ms.Attitude, ms.quality, ms.result, mt.QA FROM midCheckForTea mt CROSS JOIN midCheckForStu ms where mt.teacher_ID='" + Session["username"] + "' and mt.year='" + Session["year"] + "'and ms.student_ID='" + Session["student_ID"] + "'";
        DB      db  = new DB();
        DataSet ds  = db.Select(str, db.DBconn());

        try
        {
            string tex   = ds.Tables[0].Rows[0][1].ToString();
            string path1 = Server.MapPath(@"..\\过程文档\\文档模版\\中期检查.doc");
            string path2 = Server.MapPath("..\\过程文档\\中期检查") + "\\" + Session["student_ID"] + this.Label6.Text + "中期检查表.doc";
            if (ds.Tables[0].Rows[0]["kind"].Equals("理论研究"))
            {
                ds.Tables[0].Rows[0]["kind"] = "√理论研究    □应用研究    □技术开发    □其他( )";
            }
            else if (ds.Tables[0].Rows[0]["kind"].Equals("应用研究"))
            {
                ds.Tables[0].Rows[0]["kind"] = "□理论研究    √应用研究    □技术开发    □其他( )";
            }
            else if (ds.Tables[0].Rows[0]["kind"].Equals("技术开发"))
            {
                ds.Tables[0].Rows[0]["kind"] = "□理论研究    □应用研究    √技术开发    □其他( )";
            }
            else
            {
                ds.Tables[0].Rows[0]["kind"] = "□理论研究    □应用研究    □技术开发    √其他(" + ds.Tables[0].Rows[0]["kind"] + ")";
            }



            Microsoft.Office.Interop.Word.Application appWord = null;
            Microsoft.Office.Interop.Word.Document    doc     = null;
            try
            {
                appWord         = new Microsoft.Office.Interop.Word.ApplicationClass();
                appWord.Visible = false;
                object objTrue     = true;
                object objFalse    = false;
                object objTemplate = path1;//模板路径
                object objDocType  = Microsoft.Office.Interop.Word.WdDocumentType.wdTypeDocument;
                doc = appWord.Documents.Add(ref objTemplate, ref objFalse, ref objDocType, ref objTrue);
                //第一步生成word文档
                //定义书签变量
                object[] obDD = new object[36];
                obDD[0]  = "year";
                obDD[1]  = "Depart";
                obDD[2]  = "time";
                obDD[3]  = "teacher_Name";
                obDD[4]  = "specify";
                obDD[5]  = "guiNum";
                obDD[6]  = "IsOne";
                obDD[7]  = "IsTaskGive";
                obDD[8]  = "IsrefRead";
                obDD[9]  = "IsGuided";
                obDD[10] = "totalsituation";
                obDD[11] = "student_Name1";
                obDD[12] = "student_Name2";
                obDD[13] = "title";
                obDD[14] = "kind1";
                obDD[15] = "kind2";
                obDD[16] = "kind3";
                obDD[17] = "Ischanged";
                obDD[18] = "IsReq";
                obDD[19] = "IsCom1";
                obDD[20] = "IsCom2";
                obDD[21] = "IsCom3";
                obDD[22] = "IsCom4";
                obDD[23] = "Attitude1";
                obDD[24] = "Attitude2";
                obDD[25] = "Attitude3";
                obDD[26] = "Attitude4";
                obDD[27] = "quality1";
                obDD[28] = "quality2";
                obDD[29] = "quality3";
                obDD[30] = "quality4";
                obDD[31] = "result1";
                obDD[32] = "result2";
                obDD[33] = "result3";
                obDD[34] = "result4";
                obDD[35] = "QA";
                for (int i = 0; i < 12; i++)
                {
                    doc.Bookmarks.get_Item(ref obDD[i]).Range.Text = ds.Tables[0].Rows[0][i].ToString();
                }
                doc.Bookmarks.get_Item(ref obDD[12]).Range.Text = ds.Tables[0].Rows[0][11].ToString();
                doc.Bookmarks.get_Item(ref obDD[13]).Range.Text = ds.Tables[0].Rows[0][12].ToString();
                //前13个循环加入书签,第13个开始要进行字符处理
                //14个是kind
                string   str1   = ds.Tables[0].Rows[0][13].ToString();
                string[] sArray = str1.Split('√');

                doc.Bookmarks.get_Item(ref obDD[14]).Range.Text = sArray[0]; //加入书签

                doc.Bookmarks.get_Item(ref obDD[15]).Range.Text = "";        //加入书签

                doc.Bookmarks.get_Item(ref obDD[16]).Range.Text = sArray[1]; //加入书签

                // 15,16,个是 ms.Ischanged,ms.IsReq
                doc.Bookmarks.get_Item(ref obDD[17]).Range.Text = ds.Tables[0].Rows[0][14].ToString();
                doc.Bookmarks.get_Item(ref obDD[18]).Range.Text = ds.Tables[0].Rows[0][15].ToString();
                //17个是 ms.IsCom
                if (ds.Tables[0].Rows[0][16].Equals("提前完成"))
                {
                    doc.Bookmarks.get_Item(ref obDD[19]).Range.Text = "√";
                    doc.Bookmarks.get_Item(ref obDD[20]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[21]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[22]).Range.Text = "  ";
                }
                else if (ds.Tables[0].Rows[0][16].Equals("按计划完成"))
                {
                    doc.Bookmarks.get_Item(ref obDD[20]).Range.Text = "√";
                    doc.Bookmarks.get_Item(ref obDD[19]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[21]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[22]).Range.Text = "  ";
                }
                else if (ds.Tables[0].Rows[0][16].Equals("延期完成"))
                {
                    doc.Bookmarks.get_Item(ref obDD[21]).Range.Text = "√";
                    doc.Bookmarks.get_Item(ref obDD[19]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[20]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[22]).Range.Text = "  ";
                }
                else if (ds.Tables[0].Rows[0][16].Equals("没有完成"))
                {
                    doc.Bookmarks.get_Item(ref obDD[22]).Range.Text = "√";
                    doc.Bookmarks.get_Item(ref obDD[19]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[21]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[20]).Range.Text = "  ";
                }

                //18个是ms.Attitude
                if (ds.Tables[0].Rows[0][17].Equals("认真"))
                {
                    doc.Bookmarks.get_Item(ref obDD[23]).Range.Text = "√";
                    doc.Bookmarks.get_Item(ref obDD[24]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[25]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[26]).Range.Text = "  ";
                }
                else if (ds.Tables[0].Rows[0][17].Equals("较认真"))
                {
                    doc.Bookmarks.get_Item(ref obDD[24]).Range.Text = "√";
                    doc.Bookmarks.get_Item(ref obDD[23]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[25]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[26]).Range.Text = "  ";
                }
                else if (ds.Tables[0].Rows[0][17].Equals("一般"))
                {
                    doc.Bookmarks.get_Item(ref obDD[25]).Range.Text = "√";
                    doc.Bookmarks.get_Item(ref obDD[24]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[23]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[26]).Range.Text = "  ";
                }
                else if (ds.Tables[0].Rows[0][17].Equals("不认真"))
                {
                    doc.Bookmarks.get_Item(ref obDD[26]).Range.Text = "√";
                    doc.Bookmarks.get_Item(ref obDD[24]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[25]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[23]).Range.Text = "  ";
                }

                //19个是ms.result
                if (ds.Tables[0].Rows[0][18].Equals("优"))
                {
                    doc.Bookmarks.get_Item(ref obDD[27]).Range.Text = "√";
                    doc.Bookmarks.get_Item(ref obDD[28]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[29]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[30]).Range.Text = "  ";
                }
                else if (ds.Tables[0].Rows[0][18].Equals("良"))
                {
                    doc.Bookmarks.get_Item(ref obDD[28]).Range.Text = "√";
                    doc.Bookmarks.get_Item(ref obDD[27]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[29]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[30]).Range.Text = "  ";
                }
                else if (ds.Tables[0].Rows[0][18].Equals("中"))
                {
                    doc.Bookmarks.get_Item(ref obDD[29]).Range.Text = "√";
                    doc.Bookmarks.get_Item(ref obDD[28]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[27]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[30]).Range.Text = "  ";
                }
                else if (ds.Tables[0].Rows[0][18].Equals("差"))
                {
                    doc.Bookmarks.get_Item(ref obDD[30]).Range.Text = "√";
                    doc.Bookmarks.get_Item(ref obDD[28]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[29]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[27]).Range.Text = "  ";
                }

                //20个是ms.result
                if (ds.Tables[0].Rows[0][19].Equals("表扬"))
                {
                    doc.Bookmarks.get_Item(ref obDD[31]).Range.Text = "√";
                    doc.Bookmarks.get_Item(ref obDD[32]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[33]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[34]).Range.Text = "  ";
                }
                else if (ds.Tables[0].Rows[0][19].Equals("通过"))
                {
                    doc.Bookmarks.get_Item(ref obDD[32]).Range.Text = "√";
                    doc.Bookmarks.get_Item(ref obDD[31]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[33]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[34]).Range.Text = "  ";
                }
                else if (ds.Tables[0].Rows[0][19].Equals("警告"))
                {
                    doc.Bookmarks.get_Item(ref obDD[33]).Range.Text = "√";
                    doc.Bookmarks.get_Item(ref obDD[32]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[31]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[34]).Range.Text = "  ";
                }
                else if (ds.Tables[0].Rows[0][19].Equals("严重警告"))
                {
                    doc.Bookmarks.get_Item(ref obDD[34]).Range.Text = "√";
                    doc.Bookmarks.get_Item(ref obDD[32]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[33]).Range.Text = "  ";
                    doc.Bookmarks.get_Item(ref obDD[31]).Range.Text = "  ";
                }
                //21个是mt.QA

                doc.Bookmarks.get_Item(ref obDD[35]).Range.Text = ds.Tables[0].Rows[0][20].ToString();


                //第四步 生成word
                object filename = path2;
                object miss     = System.Reflection.Missing.Value;
                try
                {
                    doc.SaveAs(ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
                    //doc.Visible = true;//测试
                }
                catch
                {
                    // System.Windows.Forms.MessageBox.Show("系统找不到指定目录下的文件: " + DATAWORDPATH + tempFileName + WORDPOSTFIX);
                    Response.Write("<script>alert('系统找不到指定目录文件输出路径')<script>");
                }
                object missingValue     = Type.Missing;
                object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
                doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);//关闭WordDoc文档对象
                //关闭WordApp组件对象

                appWord.Application.Quit(ref miss, ref miss, ref miss);
                doc     = null;
                appWord = null;
                // Response.Write("<script>alert('文档写入成功!')<script>");
                //yi流的方式传送给客户端
                String             filepath = path2;
                System.IO.FileInfo file     = new System.IO.FileInfo(filepath);
                Response.Clear();
                Response.Charset         = "GB2312";
                Response.ContentEncoding = System.Text.Encoding.UTF8;
                // 添加头信息,为"文件下载/另存为"对话框指定默认文件名
                Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
                // 添加头信息,指定文件大小,让浏览器能够显示下载进度
                Response.AddHeader("Content-Length", file.Length.ToString());

                // 指定返回的是一个不能被客户端读取的流,必须被下载
                Response.ContentType = "application/ms-excel";

                // 把文件流发送到客户端
                Response.WriteFile(file.FullName);
                // 停止页面的执行
                Response.End();
            }
            catch (System.Exception a)
            {
                //捕捉异常,如果出现异常则清空实例,退出word,同时释放资源
                string aa               = a.ToString();
                object miss             = System.Reflection.Missing.Value;
                object missingValue     = Type.Missing;
                object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
                doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
                appWord.Application.Quit(ref miss, ref miss, ref miss);
                doc     = null;
                appWord = null;
                Response.Write("<script>alert('向word文件中写入数据出错。');location='midCheckEA.aspx'</script>");

                return;
            }
        }
        catch
        {
            Response.Write("<script>alert('数据库出错,您可能还未提交中期检查表.');location='midCheckEA.aspx'</script>");
            return;
        }
        finally
        {
            ds.Clear();
            Button2.Enabled = true;
        }
    }
    protected void btnRegister_Click(object sender, EventArgs e)
    {
        if (FileUploadWord.HasFile)
        {
            int pid = generate_ID();

            string str   = FileUploadWord.FileName;
            int    pos   = str.LastIndexOf(".");
            string ext   = str.Substring(pos + 1);
            string fname = "P_" + pid + "." + ext;
            string upath = Server.MapPath("../Admin/Paper");
            String ppath = upath + "\\" + fname;
            FileUploadWord.SaveAs(ppath);
            Session.Add("pname", fname);


            //Pdf data

            //string file = FileUploadWord.FileName;
            //FileUploadWord.SaveAs(Server.MapPath(file));
            //StringBuilder textPdf = new StringBuilder();
            //PdfReader reader = new PdfReader(Server.MapPath(file));
            ////Response.Write(reader.NumberOfPages.ToString());
            //for (int i = 1; i <= reader.NumberOfPages; i++)
            //{
            //    ITextExtractionStrategy objExtractStrategy = new SimpleTextExtractionStrategy();
            //    string strLineText = PdfTextExtractor.GetTextFromPage(reader, i, objExtractStrategy);
            //    strLineText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(strLineText)));
            //    textPdf.Append(strLineText);
            //}

            //reader.Close();
            //TextBox1.Text = TextBox1.Text + " " + textPdf.ToString();

            string file = FileUploadWord.FileName;
            FileUploadWord.SaveAs(Server.MapPath(file));
            StringBuilder textWord = new StringBuilder();
            Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
            object miss     = System.Reflection.Missing.Value;
            object path     = @Server.MapPath(file);
            object readOnly = true;
            Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);

            for (int i = 0; i < docs.Paragraphs.Count; i++)
            {
                textWord.Append(" \r\n " + docs.Paragraphs[i + 1].Range.Text.ToString());
            }
            //TextBox1.Text = text.ToString();
            // text.Append(textWord.ToString());
            TextBox1.Text = TextBox1.Text + " " + textWord.ToString();
            docs.Close();


            string path1 = null;
            path1 = Server.MapPath("~/Paper/AllPaper.txt");

            File.WriteAllText(path1, TextBox1.Text.Trim());

            lblMsg.Text    = "Synopsis Data Save Successfully...";
            lblMsg.Visible = true;
        }
    }
Beispiel #5
0
    public bool ExportToWord(DataSet ds, string path1, string path2, string[] obDD)//说明ds为数据集,path1为模版路径,path2为生成文档的路径,obDD为末班里面的书签
    {
        Microsoft.Office.Interop.Word.Application appWord = null;
        Microsoft.Office.Interop.Word.Document    doc     = null;
        try
        {
            appWord         = new Microsoft.Office.Interop.Word.ApplicationClass();
            appWord.Visible = false;
            object objTrue     = true;
            object objFalse    = false;
            object objTemplate = path1;//模板路径
            object objDocType  = Microsoft.Office.Interop.Word.WdDocumentType.wdTypeDocument;
            doc = appWord.Documents.Add(ref objTemplate, ref objFalse, ref objDocType, ref objTrue);
            //第一步生成word文档
            //定义书签变量
            int      length    = obDD.Length;
            object[] obDD_Name = new object[length];
            int      j         = 0;

            for (int i1 = 0; i1 < ds.Tables[0].Rows.Count; i1++)
            {
                for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
                {
                    int total = i1 * ds.Tables[0].Columns.Count + i + 1;
                    if (length < total)
                    {
                        break;
                    }
                    obDD_Name[j] = obDD[j];

                    doc.Bookmarks.get_Item(ref obDD_Name[j]).Range.Text = ds.Tables[0].Rows[i1][i].ToString(); //循环加入书签
                    j++;
                }
            }

            // ....
            //第四步 生成word
            object filename = path2;
            object miss     = System.Reflection.Missing.Value;
            try
            {
                doc.SaveAs(ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
                //doc.Visible = true;//测试
            }
            catch
            {
                // System.Windows.Forms.MessageBox.Show("系统找不到指定目录下的文件: " + DATAWORDPATH + tempFileName + WORDPOSTFIX);
                HttpContext.Current.Response.Write("<script>alert('系统找不到指定目录文件输出路径')<script>");
                return(false);
            }
            object missingValue     = Type.Missing;
            object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
            doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);//关闭WordDoc文档对象
            //关闭WordApp组件对象

            appWord.Application.Quit(ref miss, ref miss, ref miss);
            doc     = null;
            appWord = null;
            return(true);
            // HttpContext.Current.Response.Write("<script>alert('文档写入成功!')<script>");
        }
        catch (System.Exception a)
        {
            //捕捉异常,如果出现异常则清空实例,退出word,同时释放资源
            string aa               = a.ToString();
            object miss             = System.Reflection.Missing.Value;
            object missingValue     = Type.Missing;
            object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
            doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
            appWord.Application.Quit(ref miss, ref miss, ref miss);
            doc     = null;
            appWord = null;
            return(false);
            // HttpContext.Current.Response.Write("<script>alert('向word文件中写入数据出错:" + a.Message + "')<script>");
        }
    }
Beispiel #6
0
    public bool ExportToWord(DataSet ds, string path1, string path2, string[] obDD)//说明ds为数据集,path1为模版路径,path2为生成文档的路径,obDD为末班里面的书签
    {
        Microsoft.Office.Interop.Word.Application appWord = null;
        Microsoft.Office.Interop.Word.Document    doc     = null;
        try
        {
            appWord         = new Microsoft.Office.Interop.Word.ApplicationClass();
            appWord.Visible = false;
            object objTrue     = true;
            object objFalse    = false;
            object objTemplate = path1;//模板路径
            object objDocType  = Microsoft.Office.Interop.Word.WdDocumentType.wdTypeDocument;
            doc = appWord.Documents.Add(ref objTemplate, ref objFalse, ref objDocType, ref objTrue);
            //第一步生成word文档
            //定义书签变量
            int      length    = obDD.Length;
            object[] obDD_Name = new object[length];

            for (int i = 0; i < obDD.Length; i++)
            {
                obDD_Name[i] = obDD[i];

                doc.Bookmarks.get_Item(ref obDD_Name[i]).Range.Text = ds.Tables[0].Rows[0][i].ToString(); //姓 名
                // object obDD_Name = "t1";//姓 名
                //object obDD_Sex = "t2";//性 别
                //object obDD_Age = "t3";//年龄
                // object obDD_Birthday = "DD_Birthday"; //出生年月
                // object obDD_Nation = "DD_Nation"; //民 族
                // object obDD_Native = "DD_Native"; //籍 贯
                // ...
                //第二步 读取数据,填充数据集
                // SqlDataReader dr = XXXXX;//读取出来的数据集
                //第三步 给书签赋值
                //给书签赋值
                //   doc.Bookmarks.get_Item(ref obDD_Name).Range.Text = "nihao"; //姓 名
                // doc.Bookmarks.get_Item(ref obDD_Sex).Range.Text = "性 别";//性 别
                // doc.Bookmarks.get_Item(ref obDD_Age).Range.Text = Convert.ToString(DateTime.Now.Year);//年龄
            }
            // ds.Clear();
            // ....
            //第四步 生成word
            //object filename = Server.MapPath("file") + "\\" + "接收毕业生情况表" + ".doc";
            object filename = path2;
            object miss     = System.Reflection.Missing.Value;
            try
            {
                doc.SaveAs(ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
                //doc.Visible = true;//测试
            }
            catch
            {
                // System.Windows.Forms.MessageBox.Show("系统找不到指定目录下的文件: " + DATAWORDPATH + tempFileName + WORDPOSTFIX);
                HttpContext.Current.Response.Write("<script>alert('系统找不到指定目录文件输出路径')<script>");
                return(false);
            }
            object missingValue     = Type.Missing;
            object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
            doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
            appWord.Application.Quit(ref miss, ref miss, ref miss);
            doc     = null;
            appWord = null;
            return(true);
            // HttpContext.Current.Response.Write("<script>alert('文档写入成功!')<script>");
        }
        catch (System.Exception a)
        {
            //捕捉异常,如果出现异常则清空实例,退出word,同时释放资源
            string aa               = a.ToString();
            object miss             = System.Reflection.Missing.Value;
            object missingValue     = Type.Missing;
            object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
            doc.Close(ref doNotSaveChanges, ref missingValue, ref missingValue);
            appWord.Application.Quit(ref miss, ref miss, ref miss);
            doc     = null;
            appWord = null;
            return(false);
            // HttpContext.Current.Response.Write("<script>alert('向word文件中写入数据出错:" + a.Message + "')<script>");
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int   count  = 0;
            int   ocount = 0;
            float per    = 0;
            float max    = 0;
            con = new SqlConnection(ConfigurationManager.AppSettings["LIS"]);
            cmd = new SqlCommand();
            con.Open();
            cmd.Connection  = con;
            cmd.CommandText = "select pname from Papers";
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                //Read file from location
                string pname = dr.GetString(0).ToString();


                //reader.Close();
                //TextBox1.Text = TextBox1.Text + " " + textPdf.ToString();


                //word File Code
                //   string file =pname;
                StringBuilder textWord = new StringBuilder();
                Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
                object miss     = System.Reflection.Missing.Value;
                object path     = @Server.MapPath("~/Admin/Paper/" + pname);
                object readOnly = true;
                Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);

                for (int i = 0; i < docs.Paragraphs.Count; i++)
                {
                    textWord.Append(" \r\n " + docs.Paragraphs[i + 1].Range.Text.ToString());
                }
                //TextBox1.Text = text.ToString();
                // text.Append(textWord.ToString());

                docs.Close();



                //Compare file

                string       newfile  = "~/Paper/AllPaper.txt";//path to text file
                StreamReader testTxt1 = new StreamReader(Server.MapPath(newfile));
                string       text1    = testTxt1.ReadToEnd();
                // Split the text block into an array of sentences.

                string[] sentences1 = text1.Split(new char[] { '.', '?', '!', ',' });
                // Response.Write(sentences1[1]);
                for (int i = 0; i < sentences1.Length; i++)
                {
                    // string oldfile = textPdf.ToString();//path to text file
                    // StreamReader testTxt = new StreamReader(Server.MapPath(oldfile));
                    string text = textWord.ToString();
                    // Split the text block into an array of sentences.

                    string[] sentences = text.Split(new char[] { '.', '?', '!' });
                    ocount = sentences.Length;
                    // Define the search terms. This list could also be dynamically populated at runtime.


                    string[] wordsToMatch;
                    wordsToMatch    = new string[1];
                    wordsToMatch[0] = sentences1[i];
                    // Find sentences that contain all the terms in the wordsToMatch array.
                    // Note that the number of terms to match is not specified at compile time.
                    var sentenceQuery = from sentence in sentences
                                        let w = sentence.Split(new char[] { '.', '?', '!', ';', ':', ',' },

                                                               StringSplitOptions.RemoveEmptyEntries)

                                                where w.Distinct().Intersect(wordsToMatch).Count() == wordsToMatch.Count()

                                                select sentence;
                    foreach (string str in sentenceQuery)
                    {
                        //  Response.Write("Hloo");
                        // Response.Write(str + ".");
                        //  txtFirstItem.Text = txtFirstItem.Text + " " + str + ".";
                        count++;
                        break;
                    }
                    //    Response.Write("Hloo");
                    // testTxt.Close();
                }
                //end for loop


                per = (count * 100) / ocount;
                if (per > max)
                {
                    max = per;
                }
            }
            //  float val = count / ocount;

            if (max > 100)
            {
                lblCount.Text = "100";
            }
            else
            {
                lblCount.Text = max.ToString();
            }

            if (max < 20)
            {
                lblStatus.Text = "Accepted";
            }
            else
            {
                lblStatus.Text = "Rejected";
            }
        }
    }