Example #1
0
        public static void WordExport()
        {
            string filepath = HttpContext.Current.Server.MapPath("~/simpleTable.docx");
            var    tt       = new WordReplace {
                name = "cjc", age = 29
            };

            using (var stream = System.IO.File.OpenRead(filepath))
            {
                var doc = new NPOI.XWPF.UserModel.XWPFDocument(stream);
                //遍历段落
                foreach (var para in doc.Paragraphs)
                {
                    WorkReplaceKey(para, tt);
                }                    //遍历表格
                var tables = doc.Tables;
                foreach (var table in tables)
                {
                    foreach (var row in table.Rows)
                    {
                        foreach (var cell in row.GetTableCells())
                        {
                            foreach (var para in cell.Paragraphs)
                            {
                                WorkReplaceKey(para, tt);
                            }
                        }
                    }
                }

                var out1 = new System.IO.FileStream(HttpContext.Current.Server.MapPath("~/simpleTable" + DateTime.Now.Ticks + ".docx"), System.IO.FileMode.Create);
                doc.Write(out1);
                out1.Close();
            }
        }
        public static bool ConvertFileContentsDOCX(TNDDropItem fc)
        {
            if (fc.Content == null)
            {
                return(false);
            }
            var doc        = new NPOI.XWPF.UserModel.XWPFDocument(new MemoryStream(fc.Content));
            var properties = doc.GetProperties();

            //var ps = new NPOI.OpenXml4Net.. .PropertySet(fc.Content);
            //var summaryInfo = new NPOI.HPSF.SummaryInformation(ps);
            //var x = new NPOI.HPSF.DocumentSummaryInformation(ps);
            //Console.WriteLine(summaryInfo.ApplicationName);
            //Console.WriteLine(summaryInfo.Author);
            //Console.WriteLine(summaryInfo.Comments);
            //Console.WriteLine(summaryInfo.CharCount);
            //Console.WriteLine(summaryInfo.EditTime);
            //Console.WriteLine(summaryInfo.Keywords);
            //Console.WriteLine(summaryInfo.LastAuthor);
            //Console.WriteLine(summaryInfo.PageCount);
            //Console.WriteLine(summaryInfo.RevNumber);
            //Console.WriteLine(summaryInfo.Security);
            //Console.WriteLine(summaryInfo.Subject);
            //Console.WriteLine(summaryInfo.Template);

            return(true);
        }
Example #3
0
        public void Word()
        {
            using (var stream = System.IO.File.OpenRead(@"d:\1.docx"))
            {
                var doc = new NPOI.XWPF.UserModel.XWPFDocument(stream);
                foreach (var para in doc.Paragraphs)
                {
                    //string text = para.ParagraphText; //获得文本
                    //string styleid = para.Style;

                    //for (int i = 0; i < runs.Count; i++)
                    //{
                    //    var run = runs[i];
                    //    text = run.ToString(); //获得run的文本
                    //}

                    para.ReplaceText("[关键字]", "[替换]");
                }

                var tables = doc.Tables;
                foreach (var table in tables)       //遍历表格
                {
                    foreach (var row in table.Rows) //遍历行
                    {
                        foreach (var cell in row.GetTableCells())
                        {
                            foreach (var para in cell.Paragraphs)
                            {
                                //string text = para.ParagraphText;
                                //处理段落

                                para.ReplaceText("[关键字]", "[替换]");
                            }
                        }
                    }
                }


                //插入图片
                var gfs = new System.IO.FileStream("f:\\pic\\1.jpg", System.IO.FileMode.Open, System.IO.FileAccess.Read);
                var gp  = doc.CreateParagraph();
                var gr  = gp.CreateRun();
                gr.AddPicture(gfs, (int)NPOI.XWPF.UserModel.PictureType.JPEG, "1.jpg", 1000000, 1000000);
                gfs.Close();

                var sw = System.IO.File.OpenWrite(@"d:\new.docx");
                doc.Write(sw);
                sw.Close();

                //var newDoc = new NPOI.XWPF.UserModel.XWPFDocument();
                //var sw = System.IO.File.OpenWrite("newSave.docx");
                //newDoc.
                //newDoc.Write(sw);
                //sw.Close();
            }
        }
Example #4
0
        /// <summary>
        /// 读取Word
        /// </summary>
        /// <param name="filePath">文件路径</param>
        public static NPOI.XWPF.UserModel.XWPFDocument ReadWord(string filePath)
        {
            NPOI.XWPF.UserModel.XWPFDocument document = null;

            // 读取
            using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                document = new NPOI.XWPF.UserModel.XWPFDocument(file);
            }

            return(document);
        }
        public void ReadDocx()
        {
            NPOI.XWPF.UserModel.XWPFDocument doc;
            using (var fs = new System.IO.FileStream(getPathDocx(), System.IO.FileMode.Open)) {
                doc = new NPOI.XWPF.UserModel.XWPFDocument(fs);
            }
            var properties         = doc.GetProperties();
            var coreProperties     = properties.CoreProperties;
            var customProperties   = properties.CustomProperties;
            var extendedProperties = properties.ExtendedProperties;
            var sb = new System.Text.StringBuilder();

            foreach (var p in doc.Paragraphs)
            {
                sb.AppendLine(p.Text);
            }
            Assert.IsTrue(sb.ToString() != "");
            //customProperties.AddProperty("greet", "hallole");
            //using (var fs = new System.IO.FileStream(getPath() + @"\abc2.docx", System.IO.FileMode.Create)) {
            //    doc.Write(fs);
            //}
        }