zq198972510 2015-06-16 14:02 采纳率: 100%
浏览 1994
已采纳

在ASP.net中操作word的问题

在ASP.net中操作word时,需要保存功能,我已经引用了Microsoft Word 12.0 Object Library组件,但是在调用Document的SaveAs方法时,没有这个方法,只用一个SaveAs2方法,求解决!下面是全部代码!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using msWord = Microsoft.Office.Interop.Word;
using System.IO;
using System.Reflection;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
    object path = @"c:\123.docx";
    msWord.Application word;
    msWord.Document doc;
    object nothing = Missing.Value;

    word = new msWord.Application();
    doc = word.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing);
    msWord.Table table = doc.Tables.Add(word.Selection.Range, 5, 5, nothing, nothing);
    table.Borders.Enable = 1;
    for (int i = 1; i < 6; i++)
    {
        for (int y = 1; y < 6; y++)
        {
            table.Cell(i, y).Range.Text = "" + i + "," + y + "";
        }
    }

    object famote = msWord.WdSaveFormat.wdFormatDocumentDefault;
            //此处的没有SaveAs(),有SaveAs2(),但是提示错误,错误如下面所述。
    doc.SaveAs2( path, ref famote, 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, ref nothing);
    doc.Close(ref nothing, ref nothing, ref nothing);
    word.Quit(ref nothing, ref nothing, ref nothing);

}

}


错误提示如下:
错误 1 “Microsoft.Office.Interop.Word.Document”不包含“SaveAs2”的定义,并且找不到可接受类型为“Microsoft.Office.Interop.Word.Document”的第一个参数的扩展方法“SaveAs2”(是否缺少 using 指令或程序集引用?)

  • 写回答

5条回答

  • zq198972510 2015-07-10 13:43
    关注

    最后还是知道为什么了,因为我在新建网站的时候是新建的ASP.NET空网站,所以没有SaveAs()方法;如果新建的是ASP.NET网站,项目里会有一个引用文件夹,把上述引用添加到引用文件夹里就有SaveAs()方法了。记录下来是希望后来人少走弯路。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入