none
Excel in DataGrid die 2te RRS feed

  • Frage

  • Meeeep

    Ich komm einfach nicht darauf. Debugger zeigt keinen fehler. Und mit der Exception in dem zusammenhang kann ich auch(noch) nichts anfangen.
    Warum Befüllt er  mir den DataGrid nicht.

    Ich habe im Code wo ich meine das der Fehler liegt Markiert:

    using Microsoft.Office;
    using Microsoft.Office.Interop;
    using Microsoft.Office.Interop.Excel;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.OleDb;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    
    namespace Tagesbericht_Fehlteilliste_WFA
    {
        public partial class Form1 : Form
        {
            string SchubDateipfad = (@"G:\\PAB\Fehlteileliste.xls");
            string FlyJiDateipfad = (@"G:\\PAB\Fehlteileliste1.xls");
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                string sheetSA = "Fehlteileliste";
                ArrayList ArraySA = ProcessWorkbook(SchubDateipfad, sheetSA);
                if (ArraySA != null)
                    dataGridSA.DataSource = ArraySA;
    
                string sheetFJ = "Fehlteileliste1";
                ArrayList ArrayFJ = ProcessWorkbook(FlyJiDateipfad, sheetFJ);
                if (ArrayFJ != null)
                    dataGridFJ.DataSource = ArrayFJ;
            }
    
    
    
    
            public ArrayList ProcessWorkbook(string filePath, string SheetnameS)
            {
    
                string file = filePath;
                string Sheetname = "$"+ SheetnameS;
    
                Microsoft.Office.Interop.Excel.Application excel = null;
                Microsoft.Office.Interop.Excel.Workbook wkb = null;
                ArrayList al = new ArrayList();
                try
                {
                    excel = new Microsoft.Office.Interop.Excel.Application();
                    ExcelTools.OpenBook(excel, file, true, false, true);
                    Microsoft.Office.Interop.Excel.Worksheet sheet = wkb.Sheets[Sheetname] as Microsoft.Office.Interop.Excel.Worksheet; // Exception siehe unten
                    Microsoft.Office.Interop.Excel.Range range = null;
    
                    if (sheet != null)
                        range = sheet.get_Range("H7:L1000", System.Type.Missing);
    
    
                    if (range != null)
                    {
                        foreach (Microsoft.Office.Interop.Excel.Range r in range)
                        {
                            al.Add(r.Text);
                        }
                    }
                }
                catch (Exception ex)
                {
                    //if you need to handle stuff
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    if (wkb != null)
                        ExcelTools.ReleaseRCM(wkb);
    
                    if (excel != null)
                        ExcelTools.ReleaseRCM(excel);
                }
                return al;
            }
        }
    
        public static class ExcelTools
        {
            public static Microsoft.Office.Interop.Excel.Workbook OpenBook(Microsoft.Office.Interop.Excel.Application excelInstance, string fileName, bool readOnly, bool editable,
            bool updateLinks)
            {
                Microsoft.Office.Interop.Excel.Workbook book = excelInstance.Workbooks.Open(
                    fileName, updateLinks, readOnly,
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                    Type.Missing, editable, Type.Missing, Type.Missing, Type.Missing,
                    Type.Missing, Type.Missing);
                return book;
            }
    
            public static void ReleaseRCM(object o)
            {
                try
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
                }
                catch
                {
                }
                finally
                {
                    o = null;
                }
            }
        }
    }
    

    Exception:
    +  $exception {"Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt."} System.Exception {System.NullReferenceException}

    Der wert ist doch nicht null :/

    Vielen Dank für eure tipps.

    LG

    Loki

    Dienstag, 21. April 2015 07:04

Antworten

  • Hallo,

    wenn Du die Rückgabe Deiner "Hilfsmethode" OpenBook nicht Deiner Variable wkb zuweist, so kann das nichts werden...

    excel = new Microsoft.Office.Interop.Excel.Application();
    // wkb zuweisen
    wkb = ExcelTools.OpenBook(excel, file, true, false, true);
    

    Wobei es effizienter wäre, die Excel Instanz für beide Operationen zu verwenden, die ArrayList durch eine List<T> zu ersetzen uam.

    Gruß Elmar

    Dienstag, 21. April 2015 08:44
    Beantworter