无法创建抽象类的实例

时间:2010-03-31 13:10:50

标签: c# .net asp.net vb.net vsto

我正在尝试编译以下代码,我收到错误:

无法创建抽象类的实例。请帮忙

m_objExcel = new Excel.Application();
m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
m_objBook = (Excel._Workbook)(m_objBooks.Add(m_objOpt));
m_objSheets = (Excel.Sheets)m_objBook.Worksheets;
m_objSheet = (Excel._Worksheet)(m_objSheets.get_Item(1));

// Create an array for the headers and add it to cells A1:C1.
object[] objHeaders = {"Order ID", "Amount", "Tax"};
m_objRange = m_objSheet.get_Range("A1", "C1");
m_objRange.Value = objHeaders;
m_objFont = m_objRange.Font;
m_objFont.Bold=true;

// Create an array with 3 columns and 100 rows and add it to
// the worksheet starting at cell A2.
object[,] objData = new Object[100,3];
Random rdm = new Random((int)DateTime.Now.Ticks);
double nOrderAmt, nTax;
for(int r=0;r<100;r++)
{
    objData[r,0] = "ORD" + r.ToString("0000");
    nOrderAmt = rdm.Next(1000);
    objData[r,1] = nOrderAmt.ToString("c");
    nTax = nOrderAmt*0.07;
    objData[r,2] = nTax.ToString("c");
}
m_objRange = m_objSheet.get_Range("A2", m_objOpt);
m_objRange = m_objRange.get_Resize(100,3);
m_objRange.Value = objData;

// Save the Workbook and quit Excel.
m_objBook.SaveAs(m_strSampleFolder + "Book2.xls", m_objOpt, m_objOpt,
    m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange,
    m_objOpt, m_objOpt, m_objOpt, m_objOpt);
m_objBook.Close(false, m_objOpt, m_objOpt);
m_objExcel.Quit();

2 个答案:

答案 0 :(得分:5)

我建议你确定它破裂的那条线......

无法直接创建

Abstract个类,而是需要通过派生类创建实例,这就是您获得异常的原因。

如果您没有使用try / catch包装代码或打开“公共语言运行时异常”(默认快捷键是VS2008中的Ctrl + E),那么您应该能够找到导致问题的行。 / p>

我认为它甚至可能是你的第一行,通常你会使用类似的东西:

new ApplicationClass();

答案 1 :(得分:0)

抽象类不能用'new'实现。查看错误,看看它的重点是什么。然后检查代码以查看它在做什么。如果您无法确定答案,请发布该行。

只需尝试Excel.Application而不是新的Excel.Application()