csharp Excel阅读器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp Excel阅读器相关的知识,希望对你有一定的参考价值。

// 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);

以上是关于csharp Excel阅读器的主要内容,如果未能解决你的问题,请参考以下文章

csharp 阅读Excel

csharp 阅读Excel文件

csharp 例如-CSHARP-GroupDocs.AssemblyExamples-GenerateReport-GenerateBulletedListFromJsoninOpenExcel.c

csharp leer excel

csharp .Net Excel导出

csharp leer excel