Example #1
0
        public ReadWord(string filename)
        {
            try
            {
                //使用文件路径打开WordprocessingDocument进行处理
                using (WordprocessingDocument wordprocessingDocument =
                           WordprocessingDocument.Open(filename, true))
                {
                    // DocumentFormat.OpenXml.Wordprocessing.Body创建一个body对象,存储上述文档的body
                    DocumentFormat.OpenXml.Wordprocessing.Body body = wordprocessingDocument.MainDocumentPart.Document.Body;

                    //  定义一个elements,获取所有元素
                    IEnumerable <DocumentFormat.OpenXml.OpenXmlElement> elements = body.Elements <DocumentFormat.OpenXml.OpenXmlElement>();
                    //遍历元素列表,输出内容
                    foreach (DocumentFormat.OpenXml.OpenXmlElement element in elements)
                    {
                        strList += element.InnerText.ToString();
                        TextList.Add(element.InnerText);
                    }
                }
            }
            catch
            {
                Console.WriteLine("没有找到文件");
                Console.ReadKey();
            }
        }
        public void InitWFile(string theFilepath)
        {
            // XmlDocument theDocument = null;

            var fileInfo = new FileInfo(theFilepath);

            string tempFile = Path.GetTempPath();

            Task T = Task.Run(() => { System.IO.File.Copy(fileInfo.FullName, tempFile + ".docx"); });


            Task T2 = Task.Run(() => { WordprocessingDocument theDocument = WordprocessingDocument.Open(fileInfo.FullName, false);
                                       W.Body body = theDocument.MainDocumentPart.Document.Body;
                                       foreach (W.Paragraph theParagraph in body.Elements <W.Paragraph>())
                                       {
                                           // using theParagraph.innerText for each line
                                       }
                               });
            //using (WordprocessingDocument theDocument = WordprocessingDocument.Open(fileInfo.FullName, false))

            //OK
        }
Example #3
0
        private void opMaachenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult thedlgOpenRes;

            dlgOpen.FileName = "";
            dlgOpen.Filter   = "documents (*.docx)|*.docx";

            thedlgOpenRes = dlgOpen.ShowDialog();
            if (thedlgOpenRes == DialogResult.OK)
            {
                //File Open
                using (WordprocessingDocument theDocument = WordprocessingDocument.Open(dlgOpen.FileName, false))
                {
                    W.Body body    = theDocument.MainDocumentPart.Document.Body;
                    int    counter = 0;

                    DataGridView dataGrid = new DataGridView();
                    dataGrid.Name = "dataGrid";
                    dataGrid.AllowUserToAddRows      = false;
                    dataGrid.AllowUserToOrderColumns = false;
                    splitContainer1.Panel1.Controls.Add(dataGrid);
                    dataGrid.MouseDown += new System.Windows.Forms.MouseEventHandler(this.dataGrid_MouseDown);

                    dataGrid.Dock       = DockStyle.Fill;
                    dataGrid.DataSource = null;

                    dataGrid.ColumnCount         = 3;
                    dataGrid.Columns[0].ReadOnly = true;
                    dataGrid.Columns[1].ReadOnly = true;
                    dataGrid.Columns[2].ReadOnly = true;
                    dataGrid.Columns[0].Name     = "ID";
                    dataGrid.Columns[1].Name     = "Source";
                    dataGrid.Columns[2].Name     = "Target";
                    dataGrid.AutoSizeRowsMode    = DataGridViewAutoSizeRowsMode.AllCells;
                    dataGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                    dataGrid.Columns[1].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
                    dataGrid.Columns[2].DefaultCellStyle.WrapMode = DataGridViewTriState.True;

                    foreach (W.Paragraph theParagraph in body.Elements <W.Paragraph>())
                    {
                        if (theParagraph.InnerText.Length >= 1)
                        // check if it's not an empty line
                        {
                            counter++;
                            //if (counter > 0) { reEditor.AppendText("\r\n"); }

                            dataGrid.Rows.Add(counter, theParagraph.InnerText, " ");

                            // data
                            // dataGrid.Rows[counter].Cells[0].Value = counter;
                            // dataGrid.Rows[counter].Cells[1].Value = theParagraph.InnerText;

                            /*
                             * //Edito
                             * reEditor.ForeColor = Color.Gray;
                             * reEditor.AppendText("None");
                             * reEditor.AppendText("\r\n");
                             *
                             * //BaseText
                             * reEditor.SelectionBackColor = Color.LightGreen;
                             * reEditor.AppendText(theParagraph.InnerText);
                             * reEditor.AppendText("\r\n");
                             *
                             * //Translation
                             * reEditor.SelectionBackColor = System.Drawing.Color.Yellow;
                             * reEditor.AppendText("The empty translation");
                             * reEditor.AppendText("\r\n");
                             */
                        }
                    }
                }
            }
        }