使用点聚 weboffice 以及vsto、 web service 实现word 的自动化文档处理

♀尐吖头ヾ 提交于 2020-07-29 07:35:32

开发环境的搭建:

1.visual studio 2010

2. 点聚web office

开发步骤

1. 创建word vsto 项目

比较简单

1. 添加任务窗格

页面如下:

代码如下:

1. 使用web service 获取数据

因为开发测试返回的数据时xml 所以进行了datatable的转换

 partial class ActionsPaneControl1 : UserControl
    {
        DataTable dt = new DataTable();
        DataTable dm = new DataTable();
        ufsea.SEAOrganizationService service = new ufsea.SEAOrganizationService();
        public ActionsPaneControl1()
        {
            InitializeComponent();
        }







        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            dt = CusdbCommon.XMLHelper.XmlDeserializeUnicode(service.GetCorporationListOfGuFen());
            dataGridView1.DataSource = dt;




        }

        private void button2_Click(object sender, EventArgs e)
        {
            Globals.ThisDocument.bookmark1.Text = "this is a dmeo";
            Globals.ThisDocument.bookmark2.Text = "first app";


        }

        private void button3_Click(object sender, EventArgs e)
        {
            Globals.ThisDocument.bookmark2.Text = textBox3.Text;
        }


        private void button4_Click(object sender, EventArgs e)
        {
            dm = dt.Clone();
            foreach (DataRow item in dt.Select("CorpName like '" + textBox2.Text + "%'"))
            {
             
                dm.ImportRow(item);
             
            }
            dataGridView1.DataSource = dm;
        }
       
        private void button5_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection selectlist=dataGridView1.SelectedRows;
            foreach (DataGridViewRow item in selectlist)
            {
                int rowcount = Globals.ThisDocument.Tables[1].Rows.Count;
                int colcount = Globals.ThisDocument.Tables[1].Columns.Count;
                 Microsoft.Office.Interop.Word.Table tbl= Globals.ThisDocument.Tables[1];
                 tbl.Rows.Add();                 int rowcount1 = Globals.ThisDocument.Tables[1].Rows.Count;
                int colcount1 = Globals.ThisDocument.Tables[1].Columns.Count;
                tbl.Cell(rowcount1, 1).Range.Text = item.Cells[1].Value.ToString();
                tbl.Cell(rowcount1, 2).Range.Text = item.Cells[2].Value.ToString();
                tbl.Cell(rowcount1, 3).Range.Text = item.Cells[3].Value.ToString(); 
            }
        }
    }


























数据转换的xmlhelper 如下:

 public class XMLHelper
    {
        public XMLHelper()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }







        public static string XMLError(string title)
        {
            return "<Customer><error>" + title + "</error></Customer>";
        }
        public static string XMLSucess(string title)
        {
            return "<Customer><success>" + title + "</success></Customer>";
        }
        public static string XMLRet(string title)
        {
            return "<ret>" + title + "</ret>";
        }
        public static string XMLHeader
        {
            get
            {
                return "<Customer>";
            }
        }
        public static string XMLFooter
        {
            get
            {
                return "</Customer>";
            }
        }

























        public static string getvalue(string str, string reg)
        {
            string regexString = @"<" + reg + ">([\\w\\W]*?)</" + reg + ">";
            string val = "";
            string[] getUrls = getElements(str, regexString, "$1");
            if (getUrls.Length != 0)
            {
                val = getUrls[0];
            }
            return val;
        }









        private static string[] getElements(string m_bookHTML, string strRegexString, string strReplaceText)
        {
            string[] ArrBlock = HtmlRegexURL(strRegexString, strReplaceText, m_bookHTML);
            return ArrBlock;
        }



        private static string[] HtmlRegexURL(string strRegexString, string replaceText, string strNeedDoHtml)
        {
            string[] returnarray;
            if (strNeedDoHtml == null)
            {
                returnarray = null;
            }
            else
            {
                Regex matchsRegex = new Regex(strRegexString, RegexOptions.None);
                int[] m_num = matchsRegex.GetGroupNumbers();
                MatchCollection matchsFound;
                matchsFound = matchsRegex.Matches(strNeedDoHtml);
                int returnnum = matchsFound.Count;
                returnarray = new string[returnnum];
                int i = 0;
                foreach (Match matchMade in matchsFound)
                {
                    if (replaceText.Length > 0)
                    {
                        returnarray[i] = matchMade.Result(replaceText);
                    }
                    else
                    {
                        returnarray[i] = matchMade.Value;
                    }
                    i++;
                }
            }
            return returnarray;
        }





























 

        #region 对象的序列化和反序列化(Xml格式) XmlSerialize/XmlDeserialize
        /*
            例子:
            String ReturnValue=XMLHelper.XmlSerialize(arrExample,typeof([ClassName]))
         */
        public static string XmlSerialize(object obj, params Type[] extra)
        {
            XmlSerializer ser = new XmlSerializer(obj.GetType(), extra);
            System.IO.MemoryStream mem = new MemoryStream();
            XmlTextWriter writer = new XmlTextWriter(mem, System.Text.Encoding.Default);
            ser.Serialize(writer, obj);
            string strtmp = Encoding.Default.GetString(mem.ToArray());
            return strtmp;
        }












        /*
            例子:
            ArrayList arrExample = (ArrayList)XMLHelper.XmlDeserialize(value, typeof(ArrayList), typeof([ClassName]));
         */
        public static object XmlDeserialize(string xml, Type type,params Type[] extra)
        {
            XmlSerializer serializer = new XmlSerializer(type,extra);
            System.IO.MemoryStream mem = new MemoryStream(Encoding.Default.GetBytes(xml));
            return serializer.Deserialize(mem);
        }









        public static string XmlSerialize(DataTable dt, string tableName)
        {
            dt.TableName = tableName;
            System.Text.StringBuilder strbuilder = new System.Text.StringBuilder();
            StringWriter writer = new StringWriter(strbuilder);
            dt.WriteXml(writer, System.Data.XmlWriteMode.IgnoreSchema);
            return strbuilder.ToString();
        }







        public static DataTable XmlDeserialize(string xml)
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            MemoryStream mem = new MemoryStream(Encoding.Default.GetBytes(xml));
            ds.ReadXml(mem);
            if (ds.Tables.Count > 0)
                dt = ds.Tables[0];
            return dt;
        }
        public static DataTable XmlDeserializeUnicode(string xml)
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            MemoryStream mem = new MemoryStream(Encoding.UTF8.GetBytes(xml));
            ds.ReadXml(mem);
            if (ds.Tables.Count > 0)
                dt = ds.Tables[0];
            return dt;
        }
        #endregion
    }




















2. word 页面的表格显示

页面如下:

3. 使用word 进行的测试

加载数据页面:

动态添加表格数据页面

 

4. 使用点聚web office 进行显示操作

点聚web office的使用大家百度一下就知道了。

我只贴出代码意识使用的方式:

使用WebOffice常用接口调用 文件夹中的例子即可

实际进行部署的时候我们一般选择使用安装包,我只是为了进行测试,这种方式在实际的环境中运行是有问题的

使用IE 进行测试的页面:与上面的类似。

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!