dinghao

记录成长点滴

 

解析WordprocessingML(二)通过数据集自动生成表格

这要比查找替换简单很多,思路很简单首先在word中打上标记(并不是word中定义的标记,只是Word表格中的纯文本),然后通过WordMl在打标记的位置生成表格。

       表格的格式延续原来的格式,如果某些行有内容,可以在配置文件中指定该行不被替换。因为简单很简单只贴出部分代码:
   
/// <summary>
        
/// 替换指定标记的表格
        
/// </summary>
        
/// <param name="tableDate">表格的数据</param>
        
/// <param name="wordTable">要替换的表格</param>
        
/// <param name="tableMarker"></param>
        
/// <returns></returns>

        public XmlDocument ReplaceTable(DataTable tableDate, Table wordTable, TableMarker tableMarker)
        
{
            XmlElement old 
= (XmlElement)GetTable(tableMarker);
            XmlNamespaceManager nsmgr 
= new XmlNamespaceManager(_wordDoc.NameTable);
            nsmgr.AddNamespace(
"w", nsw);
            
//根据prTr设置表格的属性
            XmlNode prTr;
            
if (wordTable.RowPr != 0)
            
{
                
string path = string.Format("w:tr[{0}]", wordTable.RowPr);
                prTr 
= old.SelectSingleNode(path, nsmgr);
            }

            
else
            
{
                prTr 
= old.SelectSingleNode("w:tr", nsmgr);
            }



            
//获取tr属性
            XmlNode tblPrEx = prTr.SelectSingleNode("w:tblPrEx", nsmgr);
            XmlNode trPr 
= prTr.SelectSingleNode("w:trPr", nsmgr);
            
//获取tc属性
            XmlNode tcPr = prTr.SelectSingleNode("w:tc/w:tcPr", nsmgr);
            
//获取<w:pPr>
            XmlNode pPr = prTr.SelectSingleNode("w:tc/w:p/w:pPr", nsmgr);
            XmlElement newTable 
= _wordDoc.CreateElement("w:tbl", nsw);
            
//获取tbl属性
            Nodehelper.AppendNotNullChild(newTable, old.SelectSingleNode("w:tblPr", nsmgr));

            Nodehelper.AppendNotNullChild(newTable, old.SelectSingleNode(
"w:tblGrid", nsmgr));

            TableConfigProcesser tmp 
= new TableConfigProcesser();
            NameValueCollection nv 
= tmp.GetTableColumns(wordTable);
            
if (wordTable.ReservationFirstRows != 0)
            
{
                IList
<XmlNode> resRow = ReservationRowNode(old, wordTable.ReservationFirstRows, nsmgr);
                
//保留前几行
                foreach (XmlNode row in resRow)
                
{
                    Nodehelper.AppendNotNullCloneChild(newTable, row);
                    
//newTable(row);
                }



            }

            
foreach (DataRow row in tableDate.Rows)
            
{

                XmlNode ttr 
= _wordDoc.CreateElement("w:tr", nsw);
                newTable.AppendChild(ttr);
                Nodehelper.AppendNotNullCloneChild(ttr, tblPrEx);

                Nodehelper.AppendNotNullCloneChild(ttr, trPr);

                
for (int j = 1; j <= nv.Count; j++)
                
{
                    XmlNode tc 
= _wordDoc.CreateElement("w:tc", nsw);

                    XmlNode tp 
= _wordDoc.CreateElement("w:p", nsw);
                    XmlNode tr 
= _wordDoc.CreateElement("w:r", nsw);
                    XmlNode tt 
= _wordDoc.CreateElement("w:t", nsw);
                    
string c = nv.Get(j.ToString());
                    
if (c == "")
                    
{
                        tt.InnerText 
= "";


                    }

                    
else
                    
{
                        tt.InnerText 
= row[c].ToString();
                    }


                    Nodehelper.AppendNotNullCloneChild(tc, tcPr);


                    ttr.AppendChild(tc);
                    tc.AppendChild(tcPr);
                    tc.AppendChild(tp);

                    Nodehelper.AppendNotNullCloneChild(tp, pPr);

                    tp.AppendChild(tr);
                    tr.AppendChild(tt);

                }



            }


            
if (wordTable.ReservationLastRows != 0)
            
{
                IList
<XmlNode> resRow = ReservationRowNode(old, wordTable.ReservationLastRows, nsmgr);
                
//保留最后几行
                foreach (XmlNode row in resRow)
                
{
                    Nodehelper.AppendNotNullCloneChild(newTable, row);
                    
//newTable.AppendChild(row);
                }


            }

            XmlNode pa 
= old.ParentNode;
            pa.ReplaceChild(newTable, old);
            
return _wordDoc;

        }


      还有另外一种解析方式就是通过WordprocessingML的类库实现这种操作,这种方式更简单,而且能更容易的实现其他方式的表格生成,如:用数据集填充表格。

       下面的代码是用我自己的WordprocessingML库实现填充表格的代码:

  

public void GenerateTable(DataTable tableDate, Table wordTable, TableMarker tableMarker)
        
{
           
// int r = tableDate.Rows.Count;
            XmlNode n = GetTable(tableMarker);
            CT_Tbl tbl 
= new CT_Tbl((XmlElement)n);
            TableConfigProcesser tmp 
= new TableConfigProcesser();
            NameValueCollection nv 
= tmp.GetTableColumns(wordTable);
            
int i=0;
            
foreach (CT_Row tr in tbl.tr)
            
{
                
if (tr._row.InnerText == "" ||tr._row.InnerText== tableMarker.ToString()||tr._row.InnerText == null)
                
{
                    DataRow dRow 
= tableDate.Rows[i];
                    
if (nv.Count == tr.tc.Count)//只生成列数符合配置文件列数的表
                    {
                        
int j = 1;//nv 的ID
                        foreach (CT_Tc tc in tr.tc)
                        
{
                            
if (tc.Paragraphs[0].TextRuns.Count == 0)
                            
{                               
                                XmlNode r 
= CreatR();
                                tc.Paragraphs[
0]._p.AppendChild(r);
                                CT_R cr 
= new CT_R((XmlElement)r);
                                tc.Paragraphs[
0].TextRuns.Add(cr);
                            }

                            
if (nv.Get(j.ToString()) == "")
                            
{
                                
if (tc.Paragraphs[0].TextRuns.Count > 1)
                                
{
                                    tc.Paragraphs[
0]._p.RemoveAll();
                                    XmlNode r 
= CreatR();
                                    tc.Paragraphs[
0]._p.AppendChild(r);
                                    CT_R cr 
= new CT_R((XmlElement)r);
                                    tc.Paragraphs[
0].TextRuns.Add(cr);
                                    tc.Paragraphs[
0].TextRuns[0].Text = "";
                                }

                                
                            }

                            
else
                            
{
                                tc.Paragraphs[
0].TextRuns[0].Text = dRow[nv.Get(j.ToString())].ToString();
                            }

                            j
++;

                        }

                    }

                    i
++;

                   
                }

            }


           

            
        }


解析WordprocessingML(一)查找和替换

http://www.cnblogs.com/bluewater/admin/EditPosts.aspx?postid=627710

解析WordprocessingML(二)通过数据集自动生成表格

http://www.cnblogs.com/bluewater/archive/2007/03/02/662040.html

解析WordprocessingML(三)解析WordMl的基本方法和途径(a

http://www.cnblogs.com/bluewater/archive/2007/03/02/661824.html

解析WordprocessingML(三)解析WordMl的基本方法和途径(b

http://www.cnblogs.com/bluewater/archive/2007/03/02/661885.html

解析WordprocessingML(四)转换到Html格式

Xsd入门

http://www.cnblogs.com/bluewater/archive/2007/03/02/661927.html

posted on 2007-03-02 21:15  思无邪  阅读(4728)  评论(1编辑  收藏  举报

导航