using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using System.Text.RegularExpressions;
using Microsoft.Office.Tools.Word;
using System.Xml;
using System.IO;namespace duquword
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void submit_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.ShowDialog();
            labText.Text = ofd.FileName;
        }        private void button1_Click(object sender, EventArgs e)
        {
            Operationcs per = new Operationcs();          
            for (int i = 0; i < dataGridView2.RowCount; i++)
            {
                string Author = "";
                if (dataGridView2.Rows[i].Cells["Author"].Value == null)
                {
                    Author = "";
                }
                else
                {
                    Author = dataGridView2.Rows[i].Cells["Author"].Value.ToString();
                }
                string ChapterCaption = "";
                if (dataGridView2.Rows[i].Cells["ChapterCaption"].Value == null)
                {
                    ChapterCaption = "";
                }
                else
                {
                    ChapterCaption = dataGridView2.Rows[i].Cells["ChapterCaption"].Value.ToString();
                }
                string ChapterName = "";
                if (dataGridView2.Rows[i].Cells["ChapterName"].Value == null)
                {
                    ChapterName = "";
                }
                else
                {
                    ChapterName = dataGridView2.Rows[i].Cells["ChapterName"].Value.ToString();
                }
                string SectionCaption = "";
                if (dataGridView2.Rows[i].Cells["SectionCaption"].Value == null)
                {
                    SectionCaption = "";
                }
                else
                {
                    SectionCaption = dataGridView2.Rows[i].Cells["SectionCaption"].Value.ToString();
                }
                string SectionName = "";
                if (dataGridView2.Rows[i].Cells["SectionName"].Value == null)
                {
                    SectionName = "";
                }
                else
                {
                    SectionName = dataGridView2.Rows[i].Cells["SectionName"].Value.ToString();
                }
                string SectionContent = "";
                if (dataGridView2.Rows[i].Cells["SectionContent"].Value == null)
                {
                    SectionContent = "";
                }
                else
                {
                    SectionContent = dataGridView2.Rows[i].Cells["SectionContent"].Value.ToString();
                }
                per.Insertdoc(Author, ChapterCaption, ChapterName, SectionCaption, SectionName, SectionContent);
            }
            MessageBox.Show("ok");
        }        private void Form1_Load(object sender, EventArgs e)
        {
            Operationcs per = new Operationcs();
            dataGridView1.DataSource = per.Select1();
        }        private void button2_Click(object sender, EventArgs e)
        {
                     ReadDoc rdoc = new ReadDoc();
                     List<DeskbookItem> docText = rdoc.Read(labText.Text);                     DataTable tblDatas = new DataTable("Datas");
                     DataColumn dc = null;
                     dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
                     dc.AutoIncrement = true;//自动增加
                     dc.AutoIncrementSeed = 1;//起始为1
                     dc.AutoIncrementStep = 1;//步长为1
                     dc.AllowDBNull = false;//                     dc = tblDatas.Columns.Add("Author", Type.GetType("System.String"));
                     dc = tblDatas.Columns.Add("ChapterCaption", Type.GetType("System.String"));
                     dc = tblDatas.Columns.Add("ChapterName", Type.GetType("System.String"));
                     dc = tblDatas.Columns.Add("SectionCaption", Type.GetType("System.String"));
                     dc = tblDatas.Columns.Add("SectionName", Type.GetType("System.String"));
                     dc = tblDatas.Columns.Add("SectionContentid", Type.GetType("System.String"));
                     dc = tblDatas.Columns.Add("SectionContent", Type.GetType("System.String"));                     foreach (DeskbookItem itemn in docText)
                     {
                         DataRow newRow;
                         newRow = tblDatas.NewRow();
                         newRow["Author"] = itemn.Author;
                         newRow["ChapterCaption"] = itemn.ChapterCaption;
                         newRow["ChapterName"] = itemn.ChapterName;
                         newRow["SectionCaption"] = itemn.SectionCaption;
                         newRow["SectionName"] = itemn.SectionName;
                         newRow["SectionContentid"] = itemn.SectionContent.ToString();
                         newRow["SectionContent"] = itemn.SectionContentid;
                         tblDatas.Rows.Add(newRow);
                     }                     dataGridView2.DataSource = tblDatas;   
        }    }

解决方案 »

  1.   

    生成xml
            private void button3_Click(object sender, EventArgs e)
            {
                Operationcs per = new Operationcs();
                DataTable dt = per.Select();
                XmlDocument doc = new XmlDocument();
                try
                {
                    doc.Load("new.xml");
                }
                catch
                {
                    XmlTextWriter xtw = new XmlTextWriter("new.xml", Encoding.UTF8);   //新建XML文件
                    xtw.WriteStartDocument();
                    xtw.WriteStartElement("Deskbooks");                 // gnode根节点
                    xtw.WriteEndElement();
                    xtw.WriteEndDocument();
                    xtw.Close();
                    doc.Load("new.xml");
                }            for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow dr = dt.Rows[i];
                    XmlNode xn = doc.DocumentElement;                 //  找到根节点
                    XmlElement xE = doc.CreateElement("deskbook");
                    XmlElement xe = doc.CreateElement("Title");         //    在根节点下创建元素,如果是属性,则用XmlAttribute;
                    xe.InnerText = "Juvenile Law";                                      //       给子节点写入文本节点(值)
                    xE.AppendChild(xe);                                          //  根节点将其纳入
                    xn.AppendChild(xE);                XmlElement Edition = doc.CreateElement("Edition");
                    Edition.InnerText = "Juvenile Law 2011";
                    xE.AppendChild(Edition);
                    xn.AppendChild(xE);                XmlElement Supplement = doc.CreateElement("Supplement");
                    Supplement.InnerText = "False";
                    xE.AppendChild(Supplement);
                    xn.AppendChild(xE);                XmlElement Author = doc.CreateElement("Author");
                    Author.InnerText = dr["Author"].ToString();
                    xE.AppendChild(Author);
                    xn.AppendChild(xE);                XmlElement ChapterCaption = doc.CreateElement("ChapterCaption");
                    ChapterCaption.InnerText = dr["ChapterCaption"].ToString();
                    xE.AppendChild(ChapterCaption);
                    xn.AppendChild(xE);                XmlElement ChapterName = doc.CreateElement("ChapterName");
                    ChapterName.InnerText = dr["ChapterName"].ToString();
                    xE.AppendChild(ChapterName);
                    xn.AppendChild(xE);                XmlElement ChapterContent = doc.CreateElement("ChapterContent");
                    ChapterContent.InnerText = "";
                    xE.AppendChild(ChapterContent);
                    xn.AppendChild(xE);                XmlElement SectionCaption = doc.CreateElement("SectionCaption");
                    SectionCaption.InnerText = "Section "+dr["SectionCaption"].ToString();
                    xE.AppendChild(SectionCaption);
                    xn.AppendChild(xE);                XmlElement SectionName = doc.CreateElement("SectionName");
                    SectionName.InnerText = dr["SectionName"].ToString();
                    xE.AppendChild(SectionName);
                    xn.AppendChild(xE);                XmlElement SectionContent = doc.CreateElement("SectionContent");
                    SectionContent.InnerText = dr["SectionContent"].ToString();
                    xE.AppendChild(SectionContent);
                    xn.AppendChild(xE);                XmlElement EditionDate = doc.CreateElement("EditionDate");
                    EditionDate.InnerText = "";
                    xE.AppendChild(EditionDate);
                    xn.AppendChild(xE);                XmlElement RevisionDate = doc.CreateElement("RevisionDate");
                    RevisionDate.InnerText = "";
                    xE.AppendChild(RevisionDate);
                    xn.AppendChild(xE);                XmlElement ShortName = doc.CreateElement("ShortName");
                    ShortName.InnerText = " Juvenile Law 2011 - " + dr["ChapterCaption"].ToString() + "." + dr["SectionCaption"].ToString();
                    xE.AppendChild(ShortName);
                    xn.AppendChild(xE);                XmlElement LibrarySourceConst = doc.CreateElement("LibrarySourceConst");
                    LibrarySourceConst.InnerText = "MODESKBOOKS";
                    xE.AppendChild(LibrarySourceConst);
                    xn.AppendChild(xE);
                }
                doc.Save("new2.xml");                        //  利用XmlDocument保存文件            MessageBox.Show("ok");
            }
      

  2.   

    读取word
        class ReadDoc
        {
            //读取整个文本
            public string ReadContent(string docFileName)
            {
                object readOnly = true;            object missing = System.Reflection.Missing.Value;            object fileName = docFileName;
                string content="";            //初始化word程序
                ApplicationClass wordapp = new ApplicationClass();
                //打开指定的doc文件
                _Document doc = wordapp.Documents.Open(ref fileName,        ref missing, ref readOnly, ref missing, ref missing, ref missing,        ref missing, ref missing, ref missing, ref missing, ref missing,        ref missing, ref missing, ref missing, ref missing, ref missing);
                content=doc.Content.Text;            
               doc.Close(ref missing, ref missing, ref missing);
               wordapp.Quit(ref missing, ref missing, ref missing);            return content;
            }
            //读取Ch的内容
            public List<DeskbookItem> Read(string docFileName)
            {
                object readOnly = true;            object missing = System.Reflection.Missing.Value;            object fileName = docFileName;
                List<DeskbookItem> LstBookItem = new List<DeskbookItem>();
                DeskbookItem BookItem;
                //初始化word程序
                ApplicationClass wordapp = new ApplicationClass();
                //打开指定的doc文件
                _Document doc = wordapp.Documents.Open(ref fileName,        ref missing, ref readOnly, ref missing, ref missing, ref missing,        ref missing, ref missing, ref missing, ref missing, ref missing,        ref missing, ref missing, ref missing, ref missing, ref missing);
                //读取作者
                string Author = "";
                string ChapterCaption = "";
                string ChapterName = "";
                int parcount = doc.Paragraphs.Count;
                string[] SectionCaption = new string[parcount];
                string[] SectionName = new string[parcount];
                if (doc.Paragraphs[1].Range.Font.Bold == -1 && doc.Paragraphs[1].Range.Font.Size == 10 && doc.Paragraphs[1].Range.Font.Name == "Century Schoolbook")
                {
                    Author += doc.Paragraphs[1].Range.Text;
                }            //读取指定的doc文件文本  初号=42磅 小初=36磅 一号=26磅 小一=24磅 二号=22磅 小二=18磅 三号=16磅
                //小三=15磅 四号=14磅 小四=12磅 五号=10.5磅 小五=9磅 六号=7.5磅 小六=6.5磅 七号=5.5磅 八号=5磅
                for (int i = 2; i < parcount; i++)
                {                if (doc.Paragraphs[i].Range.Font.Bold == -1 && doc.Paragraphs[i].Range.Font.Size == 14 && doc.Paragraphs[i].Range.Font.Name == "Century Schoolbook")
                    {
                        Regex regex = new Regex("Chapter \\d");
                        if (regex.IsMatch(doc.Paragraphs[i].Range.Text))
                        {
                            ChapterCaption += doc.Paragraphs[i].Range.Text;
                        }
                    }                if (doc.Paragraphs[i].Range.Font.Bold == -1 && doc.Paragraphs[i].Range.Font.Size == 18 && doc.Paragraphs[i].Range.Font.Name == "Century Schoolbook")
                    {
                        ChapterName += doc.Paragraphs[i].Range.Text;
                    }            
                    if (doc.Paragraphs[i].Range.Font.Bold == -1 && doc.Paragraphs[i].Range.Font.Name == "Century Schoolbook")
                    {
                        Regex regex = new Regex("\\(§\\d\\.\\d{1,2}\\)");
                        if (regex.IsMatch(doc.Paragraphs[i].Range.Text))
                        {
                            string[] words = doc.Paragraphs[i].Range.Text.Split('(');
                            string[] words1 = words[1].Split(')');
                            string[] words2 = words1[0].Split('.');
                            BookItem = new DeskbookItem();
                            BookItem.Author = Author;
                            BookItem.ChapterCaption = ChapterCaption;
                            BookItem.ChapterName = ChapterName;
                            BookItem.SectionName = words1[1];
                            BookItem.SectionCaption = words2[1];
                            BookItem.SectionContent = i;
                            BookItem.parcount = parcount;
                            LstBookItem.Add(BookItem);
                        }                }        
                }            for (int i = 0; i < LstBookItem.Count; i++)
                {
                    if (i != LstBookItem.Count - 1)
                    {
                        string SectionContent = "";
                        for (int n = LstBookItem[i].SectionContent; n < LstBookItem[i + 1].SectionContent; n++)
                        {
                            if (doc.Paragraphs[n].Range.Font.Bold == 0 && doc.Paragraphs[n].Range.Font.Size == 10 && doc.Paragraphs[n].Range.Font.Name == "Century Schoolbook")
                            {
                                if (doc.Paragraphs[n].Range.Text.Trim() != "")
                                {
                                    SectionContent += doc.Paragraphs[n].Range.Text;
                                }
                            }
                        }
                        LstBookItem[i].SectionContentid = SectionContent;
                    }
                    else
                    {
                        string SectionContent = "";
                        for (int n = LstBookItem[i].SectionContent; n < LstBookItem[i].parcount; n++)
                        {
                            if (doc.Paragraphs[n].Range.Font.Bold == 0 && doc.Paragraphs[n].Range.Font.Size == 10 && doc.Paragraphs[n].Range.Font.Name == "Century Schoolbook")
                            {
                                if (doc.Paragraphs[n].Range.Text.Trim() != "")
                                {
                                    SectionContent += doc.Paragraphs[n].Range.Text;
                                }
                            }
                        }
                        LstBookItem[i].SectionContentid = SectionContent;
                    }            }
                
               doc.Close(ref missing, ref missing, ref missing);
               wordapp.Quit(ref missing, ref missing, ref missing);            return LstBookItem;
            }
        }    class DeskbookItem
        {
            public string Author;
            public string ChapterCaption;
            public string ChapterName;
            public string SectionCaption;
            public string SectionName;
            public int SectionContent;
            public int parcount;
            public string SectionContentid;
        }
    }