c# doc转docx,docx转doc,doc和docx互转

发布时间:2017-11-13编辑:佚名阅读(2886)

引用Microsoft.Office.Interop.Word 

private void Doc2Docx(object sourceFileName, object targetFileName)
{
    object missingValue = System.Reflection.Missing.Value;
    Object Nothing = System.Reflection.Missing.Value; 
    Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
    Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(
        ref sourceFileName,
        ref missingValue, ref missingValue, ref missingValue, ref missingValue,
        ref missingValue, ref missingValue, ref missingValue, ref missingValue,
        ref missingValue, ref missingValue, ref missingValue, ref missingValue,
        ref missingValue, ref missingValue, ref missingValue);
    object FileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocumentDefault;
    //将wordDoc文档对象的内容保存为DOCX文档
    doc.SaveAs(ref targetFileName, ref FileFormat, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
    //关闭wordDoc文档对象
    doc.Close(ref Nothing, ref Nothing, ref Nothing);
    //关闭进程
    wordApp.Application.Quit(ref Nothing, ref Nothing, ref Nothing);
}
private void Docx2Doc(object sourceFileName, object targetFileName)
{
    object missingValue = System.Reflection.Missing.Value;
    
    Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
    Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(
        ref sourceFileName,
        ref missingValue, ref missingValue, ref missingValue, ref missingValue,
        ref missingValue, ref missingValue, ref missingValue, ref missingValue,
        ref missingValue, ref missingValue, ref missingValue, ref missingValue,
        ref missingValue, ref missingValue, ref missingValue);
    object FileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;
    object LockComments = false;
    object Password = missingValue;
    object AddToRecentFiles = false;
    object WritePassword = missingValue;
    object ReadOnlyRecommended = false;
    object EmbedTrueTypeFonts = true;
    object SaveNativePictureFormat = missingValue;
    object SaveFormsData = missingValue;
    object SaveAsAOCELetter = missingValue;
    object Encoding = missingValue;
    object InsertLineBreaks = missingValue;
    object AllowSubstitutions = missingValue;
    object LineEnding = missingValue;
    object AddBiDiMarks = missingValue;
    object CompatibilityMode = missingValue;
    doc.SaveAs(ref targetFileName, ref FileFormat,
        ref LockComments, ref Password, ref AddToRecentFiles, ref WritePassword,
        ref ReadOnlyRecommended, ref EmbedTrueTypeFonts, ref SaveNativePictureFormat, ref SaveFormsData,
        ref SaveAsAOCELetter, ref Encoding, ref InsertLineBreaks, ref AllowSubstitutions,
        ref LineEnding, ref AddBiDiMarks);
    wordApp.Documents.Close(ref missingValue, ref missingValue, ref missingValue);
    //关闭进程
    object saveOption = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
    wordApp.Application.Quit(ref saveOption, ref missingValue, ref missingValue);
}


  关键字:doc转docxdocx转docdoc和docx互转


鼓掌

0

正能量

1

0

呵呵

0


评论区