Feedback

Zeile in Excel-Datei hinzufügen

Sprache: C#

[b]Microsoft Excel Object Library hinzufügen![/b] Da ich selber ziemlich Lange suchen musste, um eine entsprechende Methode zusammenzustellen, möchte ich anderen diesen mühsammen Vorgang ersparen und meine entwickelte Methode teilen. Zwar nicht das schreiben, aber das finden der nächsten Zeile scheint sich als problematisch zu erweisen und eine aufwendige und langsamme Schleife 🙁 zu erfordern.
private bool AddLineToExcelFile(string file, string[] data)
        {
            try
            {
                ApplicationClass excelApplication = null;
                Workbook excelWorkbook = null;
                Worksheet excelWorksheet = null;
                excelApplication = new ApplicationClass();
                excelApplication.Visible = false;

                System.Threading.Thread.Sleep(2000);
                object missing = System.Reflection.Missing.Value;
                excelWorkbook = excelApplication.Workbooks.Open(file, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
                excelWorksheet = (Worksheet)excelWorkbook.Worksheets.get_Item(1);
                int row = 1;
                while (excelWorksheet.Cells[row, 1].ToString() != "")
                {
                    string cellNumber2 = ("A" + row).ToString();
                    if (excelWorksheet.get_Range(cellNumber2, cellNumber2).Value2 == null)
                        break;

                    else
                        row++;
                }

                for (int i = 0; i < data.Length; i++ )
                    excelWorksheet.Cells[row, i + 1] = data[i];
                
                excelWorkbook.Save();
                excelWorkbook.Close();
                excelApplication.Quit();
                return true;
            }
            catch
            {
                return false;
            }
        }
private bool AddLineToExcelFile(string file, string[] data)
        {
            try
            {
                ApplicationClass excelApplication = null;
                Workbook excelWorkbook = null;
                Worksheet excelWorksheet = null;
                excelApplication = new ApplicationClass();
                excelApplication.Visible = false;

                System.Threading.Thread.Sleep(2000);
                object missing = System.Reflection.Missing.Value;
                excelWorkbook = excelApplication.Workbooks.Open(file, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
                excelWorksheet = (Worksheet)excelWorkbook.Worksheets.get_Item(1);
                int row = 1;
                while (excelWorksheet.Cells[row, 1].ToString() != "")
                {
                    string cellNumber2 = ("A" + row).ToString();
                    if (excelWorksheet.get_Range(cellNumber2, cellNumber2).Value2 == null)
                        break;

                    else
                        row++;
                }

                for (int i = 0; i < data.Length; i++ )
                    excelWorksheet.Cells[row, i + 1] = data[i];
                
                excelWorkbook.Save();
                excelWorkbook.Close();
                excelApplication.Quit();
                return true;
            }
            catch
            {
                return false;
            }
        }