excel工作簿出错

时间:2012-10-16 23:35:41

标签: c# excel

我很难与c#的excel工作簿一起工作。 我无法让基本操作正常工作,出于某种原因,它似乎是文档 关于excel相关课程不如其他人好。 到目前为止,我已经学会了使用xml和文本文件,这比excel要容易得多。 我从msdn论坛复制了下一个例子,但我不能让它工作,即使有困难 声明命名空间,我只从这段代码中得到错误:

using Microsoft.Office.Interop.Excel; 

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            object oExcel=new Excel.Application();
            object oBook = oExcel.Workbooks.Open("C:\\Book1.xls");
            object oSheet = oExcel.Worksheets(1);

1 个答案:

答案 0 :(得分:0)

using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Excel;

    Excel.ApplicationClass _Excel;
    Excel.Workbook WB;
    Excel.Worksheet WS;

try
    {

    _Excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
    WB = _Excel.Workbooks.Open("C:\\Book1.xls",
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing);

        //do something
        WB.Visible = true;
    }
    catch (Exception ex)
    {
        WB.Close(false, Type.Missing, Type.Missing);

        throw;
    }
    finally
    {
        GC.Collect();
        GC.WaitForPendingFinalizers();
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(WB);
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(_Excel);
    }

您可以尝试的选项2 .. 这是一个链接,也按照此链接提供的屏幕截图中的步骤 How to Open an Excel File in C#

using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel; 

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Excel.Application xlApp ;
            Excel.Workbook xlWorkBook ;
            Excel.Worksheet xlWorkSheet ;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Open("csharp.net-informations.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            MessageBox.Show(xlWorkSheet.get_Range("A1","A1").Value2.ToString());

            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
        }

        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Unable to release the Object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        } 
    }
}

以下是阅读或打开Excel文件的文章/其他方式 Excel Interop with .NET