using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ExcelRefer = Microsoft.Office.Interop.Excel; namespace ReadFromExcel { class Excel { ExcelRefer.Range range; ExcelRefer.Application excelApp; ExcelRefer.Workbook excelWorkBook; ExcelRefer.Worksheet excelWorkSheet; public void InitExcelConnection(string excelFilePath) { try { excelApp = new ExcelRefer.Application(); excelWorkBook = excelApp.Workbooks.Open(excelFilePath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); excelWorkSheet = (ExcelRefer.Worksheet)excelWorkBook.Worksheets.get_Item(1); // Zugriff auf erstes Tabellenblatt range = excelWorkSheet.UsedRange; } catch (Exception) { } } public float[] ParseExcelData(float sheet_Metal_Material_Thickness) { float[] sheetMetalValues = new float[5]; try { // Gehe das ganze Zabellenblatt durch for (int i = 1; i <= range.Rows.Count; i++) { // Zugriff auf jede Zeile string name = ((range.Cells[i, 1] as ExcelRefer.Range).Value2).ToString(); if (name != null) { if (name.ToString() == sheet_Metal_Material_Thickness.ToString()) { sheetMetalValues[0] = Convert.ToSingle(((range.Cells[i, 1] as ExcelRefer.Range).Value2)); sheetMetalValues[1] = Convert.ToSingle(((range.Cells[i, 2] as ExcelRefer.Range).Value2)); sheetMetalValues[2] = Convert.ToSingle(((range.Cells[i, 3] as ExcelRefer.Range).Value2)); sheetMetalValues[3] = Convert.ToSingle(((range.Cells[i, 4] as ExcelRefer.Range).Value2)); sheetMetalValues[4] = Convert.ToSingle(((range.Cells[i, 5] as ExcelRefer.Range).Value2)); } } } } catch (Exception) { } excelWorkBook.Close(true, null, null); excelApp.Quit(); return sheetMetalValues; } //public void ParseExcelData() //{ // // Gehe das ganze Zabellenblatt durch // for (int i = 1; i <= range.Rows.Count; i++) // { // // Zugriff auf jede Zeile // if ((range.Cells[i, 1] as ExcelRefer.Range).Value2 != null) // { // try // { // string title = (string)(range.Cells[i, 1] as ExcelRefer.Range).Value2; // string description = (string)(range.Cells[i, 2] as ExcelRefer.Range).Value2; // } // catch (Exception) // { // } // } // } // excelWorkBook.Close(true, null, null); // excelApp.Quit(); //} } }