Last active
March 12, 2019 10:15
-
-
Save SamKr/9e0e04f4141ad4c897c58d0815a1c90c to your computer and use it in GitHub Desktop.
Reads an XML file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Microsoft Excel object in references-> COM tab | |
using Excel = Microsoft.Office.Interop.Excel; | |
var xlApp = new Excel.Application(); | |
var xlWorkbook = xlApp.Workbooks.Open(Program.XmlFile); | |
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1]; | |
var xlRange = xlWorksheet.UsedRange; | |
var rowCount = xlRange.Rows.Count; | |
var colCount = xlRange.Columns.Count; | |
for (var i = 1; i <= rowCount; i++) | |
{ | |
var client = new Client(); | |
for (var j = 1; j <= colCount; j++) | |
{ | |
if (j == 1) client = new Client(); | |
if (xlRange.Cells[i, j] == null || xlRange.Cells[i, j].Value2 == null) continue; | |
switch (j) | |
{ | |
case 1: | |
client.Naam = xlRange.Cells[i, j].Value2.ToString().Trim(); | |
break; | |
case 2: | |
string nummer = xlRange.Cells[i, j].Value2.ToString().Trim(); | |
nummer = nummer.Substring(0, nummer.Length - 1); | |
client.Nummer = nummer; | |
if (!Program.Clients.Contains(client)) Program.Clients.Add(client); | |
else Console.WriteLine($"[XmlManager] duplicaat aangetroffen, naam: {client.Naam}, nummer: {client.Nummer}"); | |
break; | |
} | |
} | |
} | |
GC.Collect(); | |
GC.WaitForPendingFinalizers(); | |
Marshal.ReleaseComObject(xlRange); | |
Marshal.ReleaseComObject(xlWorksheet); | |
xlWorkbook.Close(); | |
Marshal.ReleaseComObject(xlWorkbook); | |
xlApp.Quit(); | |
Marshal.ReleaseComObject(xlApp); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment