如何使用C#创建doc 2003文件

时间:2011-10-22 15:38:36

标签: c# itext doc

有什么办法可以用C#创建doc文件。我已经尝试过,但它在2003年没有开放,但它在2007年开放了吗?

我一直在使用此代码,但doc文件未在word 2003中打开:

//writing to doc file
object oMissing = System.Reflection.Missing.Value;               
Microsoft.Office.Interop.Word._Application oWord = new Microsoft.Office.Interop.Word.Application();

Microsoft.Office.Interop.Word._Document oDoc = new Microsoft.Office.Interop.Word.Document();

oWord.Visible = false;

oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

//Insert a paragraph at the beginning of the document.
Microsoft.Office.Interop.Word.Paragraph oPara1;

oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
oPara1.Range.Text = text;

object fileName = "C:\\question.doc";
oDoc.SaveAs(ref fileName,
    ref oMissing, ref oMissing,
    ref oMissing, ref oMissing,
    ref oMissing, ref oMissing,
    ref oMissing, ref oMissing,
    ref oMissing, ref oMissing);

2 个答案:

答案 0 :(得分:1)

参考现有问题: -

<强> How can a Word document be created in C#?

以及其他网址供您参考: -

<强> Walkthrough: Creating a Table in Word

<强> Creating Word document using C#

答案 1 :(得分:1)

您需要使用wdFormatDocument97作为SaveAs()的第二个参数 - 有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word._document.saveas.aspxhttp://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.wdsaveformat.aspx

编辑 - 根据评论:

使用

object vFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument97;
oDoc.SaveAs(ref fileName,
        ref vFormat, ref oMissing,
        ref oMissing, ref oMissing,
        ref oMissing, ref oMissing,
        ref oMissing, ref oMissing,
        ref oMissing, ref oMissing);