Beispiel #1
0
        private Boolean readDocx(String docPath)
        {
            Boolean header      = true;
            int     s_conter    = 0;
            int     total_sub   = 0;
            int     result_attr = 3;
            String  allSub      = null;
            String  hedMsg      = null;
            int     n           = 0;

            String[] arr = new String[result_attr];
            String[] tem = new String[result_attr];
            String   id  = "";

            String[] sub = new String[4];
            ids.Clear();
            results.Clear();
            int c = 0;

            try
            {
                //string docPath = "E:\\CsharpResultText.docx";
                Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
                Document doc   = app.Documents.Open(docPath);
                String   words = doc.Content.Text;
                //Console.Write("Docx texx " + words);
                doc.Close();
                app.Quit();
                using (StringReader reader = new StringReader(words))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        if (header && !line.Equals("Id"))
                        {
                            hedMsg = hedMsg + "\n" + line;
                        }
                        else if ((line.Contains("Id")) || (line.Contains("Subject")) || (line.Contains("Credit")) || (line.Contains("GPA")))
                        {
                            if (line.Contains("Subject"))
                            {
                                total_sub++;
                            }
                            if ((s_conter < result_attr) && !(line.Contains("Id")))
                            {
                                arr[s_conter] = line;
                                s_conter++;
                            }
                            if (header)
                            {
                                mme.header = hedMsg;
                            }
                            header = false;
                        }
                        else if (!line.Equals(" ") && !line.Equals("\n"))
                        {
                            line = line.Replace(" ", "");
                            line = line.Replace("\u0007", "");
                            Regex reg2 = new Regex(ID_PATTERN);
                            Match m2   = reg2.Match(line);
                            if (m2.Success)
                            {
                                results.Clear();
                                id     = line;
                                allSub = null;
                                foreach (var item in arr)
                                {
                                    allSub = allSub + "  " + item;
                                }
                                allSub = allSub + "\n";
                            }
                            else if (!line.Equals(" ") && !line.Equals(""))
                            {
                                tem[n] = line;
                                if (n == (result_attr - 1))
                                {
                                    ResultModel rr = new ResultModel();
                                    rr.subject = tem[0];
                                    rr.credit  = tem[1];
                                    rr.gpa     = tem[2];
                                    results.Add(rr);
                                    n = 0;
                                }
                                else
                                {
                                    n++;
                                }
                                if (c == ((result_attr * total_sub) - 1))
                                {
                                    IdModel i = new IdModel();
                                    i.id = id;
                                    ids.Add(i);
                                    var sss = JsonConvert.SerializeObject(new { res = results });
                                    temlist.Add(JObject.Parse(sss));
                                    //Console.WriteLine("all sub "+sss);
                                    //Console.Write("id " + id + " " + sub[0] + " " + sub[2]);
                                    c = 0;
                                }
                                else
                                {
                                    c++;
                                }
                            }
                        }
                    }
                    //  Console.Write("Docx Header " + hedMsg);
                }
            }
            catch (Exception e) { Console.WriteLine("Docx texx exception  " + e); return(false); }
            return(true);
        }