以文本方式查看主题

-  计算机科学论坛  (http://bbs.xml.org.cn/index.asp)
--  『 WORD to XML, HTML to XML 』  (http://bbs.xml.org.cn/list.asp?boardid=13)
----  [求助]使用C#把word或pdf转换成xml  (http://bbs.xml.org.cn/dispbbs.asp?boardid=13&rootid=&id=46471)


--  作者:Luer_Ann
--  发布时间:5/6/2007 10:08:00 AM

--  [求助]使用C#把word或pdf转换成xml
  因做毕业设计,所以得有源码,至于现成的工具,在网上确实可以找得着,所以请高手一定不吝赐教,小女子在此先谢过各位大侠了:-)
--  作者:Tassadar
--  发布时间:5/7/2007 3:37:00 PM

--  
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication4
{
    public partial class Form1 : Form
    {
        //保存结果的文件夹路径
        private string resultFile;
        private string saveFileName = "Result\\";

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            resultFile = System.AppDomain.CurrentDomain.BaseDirectory + saveFileName + "a.xml";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.InitialDirectory = System.AppDomain.CurrentDomain.BaseDirectory;

            if(openFile.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }

            //获取文件目录
            string fileName = openFile.FileName;

            //开始转换
            //生成操作对象
            Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.ApplicationClass();
            Microsoft.Office.Interop.Word.Documents documents = word.Documents;
            Type wordType = word.GetType();
            Type docType = documents.GetType();

            //打开文件
            Microsoft.Office.Interop.Word.Document document = (Microsoft.Office.Interop.Word.Document)
                docType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, (object)documents, new Object[] { fileName, true, true });

            //另存为
            docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
                    null, document, new object[] { resultFile, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXML });
            // 退出 Word
            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);

            
            
        }

        //杀线程
        private void button2_Click(object sender, EventArgs e)
        {
            GC.Collect();
        }

        //PDF转Word
        private void button3_Click(object sender, EventArgs e)
        {
            
        }
    }
}


--  作者:Tassadar
--  发布时间:5/7/2007 3:40:00 PM

--  
在程序中使用office的"另存"功能. word-->Xml  反过来也没有问题。pdf我也在研究.
--  作者:Luer_Ann
--  发布时间:5/7/2007 9:46:00 PM

--  
终于有人理我了,太高兴了,呵呵,我先看看,不过还是谢谢了哦
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
78.125ms