Quantcast
Channel: NPOI
Viewing all articles
Browse latest Browse all 1621

New Post: error --- string search and replace

$
0
0
More in detail, i have a ".docx" WORD with paragraphs and tables,

Say, i have a paragraph "Name" followed with a table, and i want my c# app can read the "name" and repalce new word
now execute code .

//FileStream stream = File.OpenRead(basePath + filename);
       // XWPFDocument doc = new XWPFDocument(stream);
        OPCPackage pack = POIXMLDocument.OpenPackage("D:\\20140423095424.docx");
        XWPFDocument doc = new XWPFDocument(pack);
        IList<XWPFTable> tables = doc.Tables;
        foreach (XWPFTable t in tables)
        {
            IList<XWPFTableRow> rows = t.Rows;
            foreach (XWPFTableRow r in rows)
            {
                IList<XWPFTableCell> cells = r.GetTableCells();
                foreach (XWPFTableCell cell in cells)
                {
                    IList<XWPFParagraph> paras = cell.Paragraphs;
                    foreach (XWPFParagraph paragraph in paras)
                    {
                        string key = paragraph.ParagraphText;
                        if (key.StartsWith("$[") && key.EndsWith("]") && userInitParams.Contains(key.Substring(2, key.Length - 3)))
                        {
                            string value = userInitParams[key.Substring(2, key.Length - 3)].ToString();

                            IList<XWPFRun> run = paragraph.Runs;
                            //XWPFRun newRun=   paragraph.InsertNewRun(run.Count+1);
                            //newRun.SetText(value);
                            for (int i = 0; i < run.Count; i++)
                            {
                                //Boolean B1 = !string.IsNullOrEmpty(run[i].GetText(run[i].GetTextPosition()));
                                //Boolean B2 = run[i].GetText(run[i].GetTextPosition()).Equals(key);
                                //if (B1 && B2)
                                //{
                                //    run[i].SetUnderline(UnderlinePatterns.Thick);
                                //    run[i].SetText(value.ToString(), 0);
                                //}
                                if (i == 0)
                                {
                                    run[i].SetUnderline(UnderlinePatterns.Thick);
                                    run[i].SetText(value.ToString(), 0);
                                }
                                else
                                {
                                    //   paragraph.RemoveRun(i);
                                }
                            }
                        }
                    }
                }

            }
        }
        Stream str = new FileStream("d:\\dd.docx", FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite);
        doc.Write(str);

Viewing all articles
Browse latest Browse all 1621

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>