C# word 转 Xml

将.doc?文件转换为.xml格式
本文介绍了一个使用C#实现的程序,用于将特定类型的.doc?文件转换为.xml格式,并提供了详细的代码示例。
namespace WordToXml
{
    class Program
    {
        public static void Main(string[] args)
        {
            IEnumerable<string> temp = Directory.EnumerateFiles(@"", "*.doc?");
            foreach (string path in temp)
            {
                FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
                string inputFile = fileStream.Name;
                string fileName = System.IO.Path.GetFileNameWithoutExtension(inputFile);
                string outputFile = @"" + fileName + ".xml";
                string outpuFileTxt = @"*.txt";
                if (Directory.Exists(outputFile) == true)
                {
                    File.Delete(outputFile);
                }
                if (Directory.Exists(outputFile) == false)
                {
                    Directory.CreateDirectory(@"");
                    if (!File.Exists(outpuFileTxt))
                    {
                        using (FileStream fs = new FileStream(outpuFileTxt, FileMode.Create, FileAccess.Write))
                        {
                            StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
                            sw.WriteLine();

                        }

                    }

                    String fileType = System.IO.Path.GetExtension(inputFile);
                    try
                    {
                        WordToXml(inputFile, outputFile);
                    }
                    catch (Exception e)
                    {
                        File.AppendAllText(outpuFileTxt, fileName + e.Message + "\r\n");
                        continue;
                    }

                }
            }
        }

        public static void WordToXml(string strFileName, string strSaveFileName)
        {

            Microsoft.Office.Interop.Word.Document WordDoc;
            Object oMissing = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Word._Application WordApp = new Application();
            object fileName = strFileName;

            WordDoc = WordApp.Documents.Open(ref fileName,
             ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
             ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
             ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

            Type wordType = WordApp.GetType();
            // 打开文件
            Type docsType = WordApp.Documents.GetType();
            // 转换格式,另存为
            Type docType = WordDoc.GetType();
            object saveFileName = strSaveFileName;
            docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, WordDoc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXML });

            #region 其它格式:

            ///wdFormatHTML
            ///wdFormatDocument
            ///wdFormatDOSText
            ///wdFormatDOSTextLineBreaks
            ///wdFormatEncodedText
            ///wdFormatRTF
            ///wdFormatTemplate
            ///wdFormatText
            ///wdFormatTextLineBreaks
            ///wdFormatUnicodeText
            //-----------------------------------------------------------------------------------
            #endregion
            WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
            WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);


        }

    }
}

http://www.cnblogs.com/Tiffany_2008/archive/2010/12/22/1914160.html


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值