<rp id="wnpn7"><ruby id="wnpn7"></ruby></rp>
<progress id="wnpn7"><track id="wnpn7"><rt id="wnpn7"></rt></track></progress>
<ruby id="wnpn7"></ruby>
<ruby id="wnpn7"><blockquote id="wnpn7"><div id="wnpn7"></div></blockquote></ruby>

    1. <em id="wnpn7"><ruby id="wnpn7"><input id="wnpn7"></input></ruby></em>
      1. <button id="wnpn7"><acronym id="wnpn7"></acronym></button><button id="wnpn7"><acronym id="wnpn7"></acronym></button>

        <rp id="wnpn7"><acronym id="wnpn7"></acronym></rp>

        <li id="wnpn7"><object id="wnpn7"><u id="wnpn7"></u></object></li>
        VB.net 2010 視頻教程 VB.net 2010 視頻教程 python基礎視頻教程
        SQL Server 2008 視頻教程 c#入門經典教程 Visual Basic從門到精通視頻教程
        當前位置:
        首頁 > 編程開發 > c#教程 >
        • C#教程之C# 使用NPOI處理word

        本站最新發布   C#從入門到精通
        試聽地址  
        http://www.squ68.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;
                }

        相關教程
                
        免费看成年人视频大全_免费看成年人视频在线观看