I've made a method to extract Excel data out of an existing Excel file. It contains 3 tabs where info should be extracted from. The data should be stored to use local so it's always fast available. It is currently working but it seems like I have a lot duplicate code that could most likely be written a lot shorter.
I'm using 9 different Properties.settings (all of the type list), 3 for each column in each of the tabs. The tabs I'm using are MPU, AAUX and ACTRL. I'm thinking of making a single list, where type is a class that contains 9 properties, but how do I accomplice this? Or isn't it even a good idea to do that? All the tabs contain the same columns only with different values.
public void ExcelExtract(string filePath)
{
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(filePath);
excelSheets = excelWorkbook.Worksheets;
excelWorksheet = (Excel.Worksheet)excelSheets.get_Item("MPU");
Excel.Range eersteColumn = excelWorksheet.UsedRange.Columns[1];
Excel.Range derdeColumn = excelWorksheet.UsedRange.Columns[3];
Excel.Range vierdeColumn = excelWorksheet.UsedRange.Columns[4];
Excel.Range vijfdeColumn = excelWorksheet.UsedRange.Columns[5];
System.Array eersteColumnWaarden = (System.Array)eersteColumn.Cells.Value;
System.Array derdeColumnWaarden = (System.Array)derdeColumn.Cells.Value;
System.Array vierdeColumnWaarden = (System.Array)vierdeColumn.Cells.Value;
System.Array vijfdeColumnWaarden = (System.Array)vijfdeColumn.Cells.Value;
var MPU = new List<object>();
foreach (var a in eersteColumnWaarden) { MPU.Add(a); }
Properties.Settings.Default.listFoutcodeMPU = MPU.Select(o => o == null ? String.Empty : o.ToString()).ToList();
Properties.Settings.Default.Save();
MPU = new List<object>();
foreach (var a in derdeColumnWaarden) { MPU.Add(a); }
Properties.Settings.Default.listGraadMPU = MPU.Select(o => o == null ? String.Empty : o.ToString()).ToList();
Properties.Settings.Default.Save();
MPU = new List<object>();
foreach (var a in vierdeColumnWaarden) { MPU.Add(a); }
Properties.Settings.Default.listProbleemMPU = MPU.Select(o => o == null ? String.Empty : o.ToString()).ToList();
Properties.Settings.Default.Save();
MPU = new List<object>();
foreach (var a in vijfdeColumnWaarden) { MPU.Add(a); }
Properties.Settings.Default.listOplossingMPU = MPU.Select(o => o == null ? String.Empty : o.ToString()).ToList();
Properties.Settings.Default.Save();
excelWorksheet = (Excel.Worksheet)excelSheets.get_Item("AAUX");
eersteColumn = excelWorksheet.UsedRange.Columns[1];
derdeColumn = excelWorksheet.UsedRange.Columns[3];
vierdeColumn = excelWorksheet.UsedRange.Columns[4];
vijfdeColumn = excelWorksheet.UsedRange.Columns[5];
eersteColumnWaarden = (System.Array)eersteColumn.Cells.Value;
derdeColumnWaarden = (System.Array)derdeColumn.Cells.Value;
vierdeColumnWaarden = (System.Array)vierdeColumn.Cells.Value;
vijfdeColumnWaarden = (System.Array)vijfdeColumn.Cells.Value;
var AAUX = new List<object>();
foreach (var a in eersteColumnWaarden) { AAUX.Add(a); }
Properties.Settings.Default.listFoutcodeAAUX = AAUX.Select(o => o == null ? String.Empty : o.ToString()).ToList();
Properties.Settings.Default.Save();
AAUX = new List<object>();
foreach (var a in derdeColumnWaarden) { AAUX.Add(a); }
Properties.Settings.Default.listGraadAAUX = AAUX.Select(o => o == null ? String.Empty : o.ToString()).ToList();
Properties.Settings.Default.Save();
AAUX = new List<object>();
foreach (var a in vierdeColumnWaarden) { AAUX.Add(a); }
Properties.Settings.Default.listProbleemAAUX = AAUX.Select(o => o == null ? String.Empty : o.ToString()).ToList();
Properties.Settings.Default.Save();
AAUX = new List<object>();
foreach (var a in vijfdeColumnWaarden) { AAUX.Add(a); }
Properties.Settings.Default.listOplossingAAUX = AAUX.Select(o => o == null ? String.Empty : o.ToString()).ToList();
Properties.Settings.Default.Save();
excelWorksheet = (Excel.Worksheet)excelSheets.get_Item("ACTRL");
eersteColumn = excelWorksheet.UsedRange.Columns[1];
derdeColumn = excelWorksheet.UsedRange.Columns[3];
vierdeColumn = excelWorksheet.UsedRange.Columns[4];
vijfdeColumn = excelWorksheet.UsedRange.Columns[5];
eersteColumnWaarden = (System.Array)eersteColumn.Cells.Value;
derdeColumnWaarden = (System.Array)derdeColumn.Cells.Value;
vierdeColumnWaarden = (System.Array)vierdeColumn.Cells.Value;
vijfdeColumnWaarden = (System.Array)vijfdeColumn.Cells.Value;
var ACTRL = new List<object>();
foreach (var a in eersteColumnWaarden) { ACTRL.Add(a); }
Properties.Settings.Default.listFoutcodeCTRL = ACTRL.Select(o => o == null ? String.Empty : o.ToString()).ToList();
Properties.Settings.Default.Save();
ACTRL = new List<object>();
foreach (var a in derdeColumnWaarden) { ACTRL.Add(a); }
Properties.Settings.Default.listGraadCTRL = ACTRL.Select(o => o == null ? String.Empty : o.ToString()).ToList();
Properties.Settings.Default.Save();
ACTRL = new List<object>();
foreach (var a in vierdeColumnWaarden) { ACTRL.Add(a); }
Properties.Settings.Default.listProbleemCTRL = ACTRL.Select(o => o == null ? String.Empty : o.ToString()).ToList();
Properties.Settings.Default.Save();
ACTRL = new List<object>();
foreach (var a in vijfdeColumnWaarden) { ACTRL.Add(a); }
Properties.Settings.Default.listOplossingCTRL = ACTRL.Select(o => o == null ? String.Empty : o.ToString()).ToList();
Properties.Settings.Default.Save();
excelApp.Quit();
}