0

我很难使用 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);
4

1 回答 1

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.. 这是一个链接以及按照此链接中提供的屏幕截图中的步骤 如何在 C# 中打开 Excel 文件

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

于 2012-10-16T23:53:21.933 回答