openxml操作word的基本应用

类库:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml.Drawing.Wordprocessing;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

namespace ReportDoc
{
    ///


    /// 作者:HaoZhenHu
    ///

    public class Report
    {
        ///
        /// 用于获取内容控件中指定的文档表
        ///

        /// 内容控件
        /// 标签
        ///
        public static Table DOC_TAB_GetTable(ref OpenXmlElement _oxe, string _tag)
        {
            SdtBlock _tBlock = _oxe.Descendants ().Where(el => el.SdtProperties.GetFirstChild().Val == _tag).Single();
            return _tBlock.Descendants().Single
();
        }
        ///
        /// 获取内容控件表中的行数
        ///

        /// 文档表对象
        ///
        public static int DOC_TAB_GetTableRowCount(Table _tb)
        {
            return _tb.Elements().Count();
        }
        ///
        /// 删除指定索引的行
        ///

        /// 文档表对象
        /// 删除行的索引
        ///
        public static bool DOC_TAB_DeleteTableRow(ref Table _tb, int _index)
        {
            if (!(_index >= 0 && _index < _tb.Elements().Count()))
            {
                return false;
            }
            try
            {
                _tb.RemoveChild(_tb.Elements().ElementAt(_index));
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            return true;
        }
        ///
        /// 删除指定行 (重载)
        ///

        /// 文档表
        /// 文档表行
        ///
        public static bool DOC_TAB_DeleteTableRow(ref Table _tb, ref TableRow _tableRow)
        {
            try
            {
                _tb.RemoveChild(_tableRow);
                return true;
            }
            catch
            {
                return false;
            }
        }
        ///
        /// 返回指定索引的行
        ///

        /// 文档表对象
        /// 指定行的索引
        ///
        public static TableRow DOC_TAB_GetTableRow(ref Table _tb, int _index)
        {
            try
            {
                return _tb.Elements().ElementAt(_index);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        ///
        /// 返回复制后的文档表行
        ///

        /// 要被复制的文档表行
        ///
        public static TableRow DOC_TAB_CloneTableRow(TableRow _tableRow)
        {
            return (TableRow)_tableRow.Clone();
        }
        ///
        /// 返回复制后的文档表行 (重载)
        ///

        /// 文档表
        /// 行所在索引
        ///
        public TableRow DOC_TAB_CloneTableRow(Table _tb, int _index)
        {
            return (TableRow)_tb.Elements().ElementAt(_index).Clone();
        }
        ///
        /// 向文档中指定的单元格添加数据
        ///

        /// 要添加的数据
        /// 文档行
        /// 单元格的索引
        public static void DOC_TAB_FillCellData(string _content, ref TableRow _tableRow, int _index)
        {
            TableCell _tableCell = _tableRow.Elements().ElementAt(_index);
            string[] _contentLine = System.Text.RegularExpressions.Regex.Split(_content, @"/r/n");
            DOC_TAB_ClearTextTableCell(ref _tableCell);

            for (int i = 0; i < _contentLine.Length; i++)
            {
                string _curCellData = _contentLine[i];
                //如果是第一行,则直接填充
                if (0 == i)
                {
                    if (_tableCell.Elements().Count() > 0)
                    {
                        Paragraph _newParagraph = _tableCell.Elements().Last();
                        if (_newParagraph.Elements().Count() > 0)
                        {
                            ParagraphProperties _paragraphProper = _newParagraph.Elements().First();

                            RunProperties _newRunProper = new RunProperties();
                            if (_paragraphProper.Elements().Count() > 0)
                            {
                                ParagraphMarkRunProperties _paraMarkRunProper = _paragraphProper.Elements().First();
                                for (int j = 0; j < _paraMarkRunProper.Elements().Count(); j++)
                                {
                                    _newRunProper.Append(_paraMarkRunProper.Elements().ElementAt(j).CloneNode(true));
                                }
                            }
                            else
                            {
                                _newRunProper.Append(new RunFonts() { Hint = FontTypeHintValues.EastAsia });
                            }
                            _newParagraph.Append(new Run(_newRunProper, new Text(_curCellData) { Space = "preserve" }));
                        }
                        else
                        {
                            _newParagraph.Append(new ParagraphMarkRunProperties(new RunFonts(){ Hint = FontTypeHintValues.EastAsia }));
                            _newParagraph.Append(new Run(new RunProperties(new RunFonts() { Hint = FontTypeHintValues.EastAsia }), new Text(_curCellData) { Space = "preserve" }));
                        }
                    }
                    else
                    {
                        Paragraph _newParagraph = new Paragraph();
                        _newParagraph.Append(new ParagraphMarkRunProperties(new RunFonts().EastAsia));
                        _newParagraph.Append(new DocumentFormat.OpenXml.Wordprocessing.Run(new RunProperties(new RunFonts() { Hint = FontTypeHintValues.EastAsia }), new Text(_curCellData) { Space = "preserve" }));
                    }
                }
                else
                {
                    Paragraph _paragraph = _tableCell.Elements().Last();
                    Paragraph _newParagraph = (Paragraph)_paragraph.Clone();
                    DocumentFormat.OpenXml.Wordprocessing.Run _newRun = _newParagraph.Elements().Last();
                    DocumentFormat.OpenXml.Wordprocessing.Text _newText = _newRun.Elements().Last();
                    _newText.Text = _curCellData;
                    _tableCell.Append(_newParagraph);
                }
            }
        }
        ///


        /// 在指定行后面添加新行
        ///

        /// 新行
        /// 文档表
        /// 行索引
        public static void DOC_TAB_InsertRowAfter(TableRow _newRow, ref Table _tb, int _index)
        {
            TableRow _tableRow = _tb.Elements().ElementAt(_index);
            _tb.InsertAfter(_newRow, _tableRow);
        }
        ///
        /// 在指定行前面添加新行
        ///

        /// 新行
        /// 文档表
        /// 行索引
        public static void DOC_TAB_InsertRowBefore(TableRow _newRow, ref Table _tb, int _index)
        {
            TableRow _tableRow = _tb.Elements().ElementAt(_index);
            _tb.InsertBefore(_newRow, _tableRow);
        }
        ///
        /// 清除单元格中的内容,但保留其格式
        ///

        /// 单元格
        public static void DOC_TAB_ClearTextTableCell(ref TableCell _tableCell)
        {
            for (int i = _tableCell.Elements().Count()-2; i >=0 ; i--)
            {
                _tableCell.Elements().ElementAt(i).Remove();
            }

            Paragraph _paragraph = _tableCell.Elements().Single();
            foreach (Run _run in _paragraph.Elements())
            {
                _run.RemoveAllChildren();
            }
        }
        ///


        /// 用于插入文本内容
        ///

        /// 要填充内容的节点
        /// 填充的内容
        public static void DOC_TEXT_FillData(ref OpenXmlElement _oxe, string _content)
        {
            string _eType = _oxe.GetType().Name;
            switch (_eType)
            {
                case "SdtBlock":
                    Paragraph _paragraph = (Paragraph)_oxe.Descendants().First().Clone();
                    Run _run = (Run)_paragraph.Elements().First().Clone();
                    _run.RemoveAllChildren();
                    _run.AppendChild(new Text(_content));
                    _paragraph.AppendChild(_run);

                    _oxe.RemoveAllChildren();
                    _oxe.AppendChild(_paragraph);
                    break;
                case "SdtRun":
                    SdtRun _sdtRun = (SdtRun)_oxe;
                    _sdtRun.SdtProperties.RemoveAllChildren();

                    RunProperties _runPro = (RunProperties)_sdtRun.Elements().Single().Clone();
                    Text _runText = new Text(_content);

                    Run _runAnother = _sdtRun.SdtContentRun.Elements().Single();
                    _runAnother.RemoveAllChildren();
                    _runAnother.AppendChild(_runPro);
                    _runAnother.AppendChild(_runText);
                    break;
                case "Run":
                    _oxe.RemoveAllChildren();
                    _oxe.AppendChild(new Text(_content));
                    break;
            }
        }
    }
}

 


应用:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ReportDoc;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System.IO;

namespace DocDemo
{
    public partial class _Default : System.Web.UI.Page
    {
        public struct TestData
        {
            public int ID;
            public string Name;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            List _testDataList = new List();
            TestData _myData;
            _myData.ID = 1;
            _myData.Name = "xiaohulove/r/ny";
            _testDataList.Add(_myData);
            _myData.ID = 2;
            _myData.Name = "/r/nisxiaohu'shoney";
            _testDataList.Add(_myData);
            WordprocessingDocument _wordPoc = null;

            try
            {
                _wordPoc = WordprocessingDocument.Open(Server.MapPath("./testDoc.docx"), true);


                MainDocumentPart _mainDocumentPart = _wordPoc.MainDocumentPart;
                OpenXmlElement _oxe = _mainDocumentPart.Document.Body;

                //_oxe.RemoveAllChildren();
                //Paragraph _paragraph = new Paragraph();
                //Run _newRun = new Run();
                //_paragraph.AppendChild(_newRun);
                //OpenXmlElement _element = _paragraph.Elements().Single();
                //Report.DOC_TEXT_FillData(ref _element, "aaaaaaa");
                //_oxe.AppendChild(_paragraph);


                DocumentFormat.OpenXml.Wordprocessing.Table _myTable = Report.DOC_TAB_GetTable(ref _oxe, "vs");
                //int count = _myTable.Elements().Count();
                int count = Report.DOC_TAB_GetTableRowCount(_myTable);
                DocumentFormat.OpenXml.Wordprocessing.TableRow _templateTRow = Report.DOC_TAB_GetTableRow(ref _myTable, 1);
                for (int j = count - 1; j > 0; j--)
                {
                    //_myTable.RemoveChild(_myTable.Elements().ElementAt(j));
                    Report.DOC_TAB_DeleteTableRow(ref _myTable, j);
                }

                int i = 0;

                foreach (TestData _test in _testDataList)
                {
                    DocumentFormat.OpenXml.Wordprocessing.TableRow _myRow = Report.DOC_TAB_CloneTableRow(_templateTRow);
                    Report.DOC_TAB_FillCellData(_test.ID.ToString(), ref _myRow, 0);
                    Report.DOC_TAB_FillCellData(_test.Name, ref _myRow, 1);
                    Report.DOC_TAB_InsertRowAfter(_myRow, ref _myTable, i);
                    i++;
                }

            }
            catch
            {
                _wordPoc.Close();
                GC.Collect();
                Page.RegisterStartupScript("aaaaaaa", "");
                return;
            }

            _wordPoc.MainDocumentPart.Document.Save();
            _wordPoc.Close();

            GC.Collect();
        }
    }
}

 

你可能感兴趣的:(.Net)