VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > c#教程 >
  • C#教程之C#教程之C# 使用NPOI处理word

本站最新发布   C#从入门到精通
试听地址  
https://www.xin3721.com/eschool/CSharpxin3721/

需要引入referece: Microsoft.Office.Interop.Word.dll (适用于word2007,2010等早期版本)

适用于word2016可以使用NPOI(在Preferences->Manage Nuget Packages中搜索NPOI)

下面是使用npoi创建word文档和读取其中段落文字的简单方法:

static void Main(string[] args)
        {
            CreateFile();
            Console.WriteLine(ExcuteWordText());
            Console.ReadLine();
        }
        public static void CreateFile()
        {
            XWPFDocument doc = new XWPFDocument(); //文档

            XWPFParagraph p1 = doc.CreateParagraph(); //段落
            p1.Alignment = ParagraphAlignment.CENTER; //字体居中
            p1.IndentationFirstLine = (int)100; //首行缩进
            XWPFRun r1 = p1.CreateRun();                //向该段落中添加文字
            r1.SetText("测试段落一");
            XWPFParagraph p2 = doc.CreateParagraph();
            p2.Alignment = ParagraphAlignment.LEFT;
            XWPFRun r2 = p2.CreateRun();
            r2.SetText("测试段落二");

            FileStream out1 = new FileStream("simple.docx", FileMode.Create);
            doc.Write(out1);
            out1.Close();
        }
        //读段落
        public static string ExcuteWordText()
        {
            StringBuilder sb = new StringBuilder();
            using (FileStream stream = File.OpenRead(AppDomain.CurrentDomain.BaseDirectory+ "\\"+"simple.docx"))
            {
                XWPFDocument doc = new XWPFDocument(stream);
                foreach (var para in doc.Paragraphs)
                {
                    string text = para.ParagraphText; //获得文本
                    var runs = para.Runs;
                    string styleid = para.Style;
                    for (int i = 0; i < runs.Count; i++)
                    {
                        var run = runs[i];
                        text = run.ToString(); //获得run的文本
                        sb.Append(text + ",");
                    }
                }
            }
            return sb.ToString();
        }

下面是两个处理table中文字格式的方法(未调试)

        /// <summary>
        /// 设置字体格式
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="table"></param>
        /// <param name="setText"></param>
        /// <returns></returns>
        public XWPFParagraph SetCellText(XWPFDocument doc, XWPFTable table, string setText)
        {
            //table中的文字格式设置
            CT_P para = new CT_P();
            XWPFParagraph pCell = new XWPFParagraph(para, table.Body);
            pCell.Alignment = ParagraphAlignment.CENTER;//字体居中
            pCell.VerticalAlignment = TextAlignment.CENTER;//字体居中

            XWPFRun r1c1 = pCell.CreateRun();
            r1c1.SetText(setText);
            r1c1.FontSize = 12;
            r1c1.SetFontFamily("华文楷体", FontCharRange.None);//设置雅黑字体
                                                           //r1c1.SetTextPosition(20);//设置高度

            return pCell;
        }

        /// <summary>
        /// 设置单元格格式
        /// </summary>
        /// <param name="doc">doc对象</param>
        /// <param name="table">表格对象</param>
        /// <param name="setText">要填充的文字</param>
        /// <param name="align">文字对齐方式</param>
        /// <param name="textPos">rows行的高度</param>
        /// <returns></returns>
        public XWPFParagraph SetCellText(XWPFDocument doc, XWPFTable table, string setText, ParagraphAlignment align, int textPos)
        {
            CT_P para = new CT_P();
            XWPFParagraph pCell = new XWPFParagraph(para, table.Body);
            //pCell.Alignment = ParagraphAlignment.LEFT;//字体
            pCell.Alignment = align;

            XWPFRun r1c1 = pCell.CreateRun();
            r1c1.SetText(setText);
            r1c1.FontSize = 12;
            r1c1.SetFontFamily("华文楷体", FontCharRange.None);//设置雅黑字体
            r1c1.SetTextPosition(textPos);//设置高度

            return pCell;
        }

相关教程