
C# CSV Dosyaları Okuma: Bir Eğitim
Çeşitli Excel formatları ile çalışmak genellikle verileri okumak ve ardından programlı olarak yeniden yapılandırmayı gerektirir. Bu makalede, mükemmel bir araç olan IronXL kullanarak C#'da bir CSV dosyasını nasıl okuyacağımızı ve bir Excel elektronik tablosundan veri nasıl çözeceğimizi öğreneceğiz.
CSV Nedir?
CSV basit bir veri formatıdır, ancak birçok farklar olabilir; Çünkü satırlar ve verilerin sütunları arasında ayırıcılar kullanır, C# projelerimizde programlı olarak okumak zor olabilir. Bu makale, CSV dosyalarını okumak için IronXL kütüphanesini nasıl kullanacağınızı gösterecek.
1. How to read a CSV File in C#
MVC, ASP.NET veya .NET Core'da CSV dosyalarını okumak için IronXL'den yararlanmadan önce, onu kurmalısınız. İşte hızlı bir walkthrough.
- Visual Studio'da, Proje menüsünü seçin
- NuGet Paketlerini Yönet
- IronXL.Excel'i Arayın
- Yükleyin
Visual Studio'da NuGet Paket Yöneticisinde IronXL'i arayın
C#'da CSV dosyalarını okumanız gerektiğinde, IronXL mükemmel bir araçtır. Virgüller veya aşağıda görülen kod segmentlerinde olduğu gibi herhangi bir başka ayırıcı ile bir CSV dosyasını okuyabilirsiniz.
// Load a CSV file and interpret it as an Excel-like workbook
WorkBook workbook = WorkBook.LoadCSV("Weather.csv", fileFormat: ExcelFileFormat.XLSX, ListDelimiter: ",");
// Access the default worksheet in the workbook
WorkSheet ws = workbook.DefaultWorkSheet;
// Save the workbook to a new Excel file
workbook.SaveAs("Csv_To_Excel.xlsx");' Load a CSV file and interpret it as an Excel-like workbook
Dim workbook As WorkBook = WorkBook.LoadCSV("Weather.csv", fileFormat:= ExcelFileFormat.XLSX, ListDelimiter:= ",")
' Access the default worksheet in the workbook
Dim ws As WorkSheet = workbook.DefaultWorkSheet
' Save the workbook to a new Excel file
workbook.SaveAs("Csv_To_Excel.xlsx")Çıktı:
Virgül Ayırıcılı Çıkış CSV Dosyası
Kod Açıklaması:
Bir WorkBook nesnesi oluşturulur. Daha sonra WorkBook nesnesi için LoadCSV yöntemi kullanılarak, CSV'nin adı, formatı ve CSV dosyasında kullanılan ayırıcılar belirtilir. Bu durumda, ayırıcı olarak virgüller kullanılır.
Daha sonra bir WorkSheet nesnesi oluşturulur. CSV dosyasının içeriği buraya yerleştirilecektir. Dosya, yeni bir ad ve format altında kaydedilir.
Microsoft Excel'de Görüntülenen Veriler
2. Excel Dosyaları için IronXL
IronXL'i projeniz için kullanarak C#'da Excel dosya formatlarıyla çalışmak için verimli bir yol oluşturabilirsiniz. IronXL'i direkt indirmeyle kurabilirsiniz. Alternatif olarak, Visual Studio için NuGet Kurulumu kullanabilirsiniz. Yazılım geliştirici için ücretsizdir.
3. WorkBook Yükleyin ve WorkSheet Erişin
WorkBook, Excel dosyasına ve tüm işlevlerine tam erişim sağlayan IronXL sınıfıdır. Örneğin, bir Excel dosyasına erişmek istiyorsak, kodu kullanırız:
// Load the Excel file
WorkBook wb = WorkBook.Load("sample.xlsx"); // Excel file path' Load the Excel file
Dim wb As WorkBook = WorkBook.Load("sample.xlsx") ' Excel file pathExcel dosyasının belirli bir çalışma sayfasına erişmek için IronXL, WorkSheet sınıfını sağlar.
// Access a specific worksheet by name
WorkSheet ws = wb.GetWorkSheet("Sheet1"); // by sheet name' Access a specific worksheet by name
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1") ' by sheet nameExcel çalışma sayfası ws elde ettikten sonra, ondan her tür veriyi çıkarabilir ve tüm Excel işlevlerini uygulayabilirsiniz. Excel çalışma sayfası ws'den verilere şu süreçle erişilebilir:
using IronXL;
class Program
{
static void Main(string[] args)
{
// Load the workbook and access a specific worksheet
WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
// Iterate through a range of cells and display their values
foreach (var cell in ws["A2:A10"])
{
Console.WriteLine("Value is: {0}", cell.Text);
}
Console.ReadKey();
}
}Imports IronXL
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Load the workbook and access a specific worksheet
Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
' Iterate through a range of cells and display their values
For Each cell In ws("A2:A10")
Console.WriteLine("Value is: {0}", cell.Text)
Next cell
Console.ReadKey()
End Sub
End Class4. Bir Excel Çalışma Sayfasını DataTable Olarak Okumak
IronXL kullanarak, bir Excel WorkSheet'yi bir DataTable gibi kullanmak çok kolaydır.
DataTable dt = ws.ToDataTable(true); // Converts the worksheet to a DataTable, using the first row as column namesDim dt As DataTable = ws.ToDataTable(True) ' Converts the worksheet to a DataTable, using the first row as column namesAşağıdaki ad alanlarını kullanın:
using IronXL;
using System.Data;Imports IronXL
Imports System.DataAşağıdaki kodu yazın:
class Program
{
static void Main(string[] args)
{
// Load the workbook and access a specific worksheet
WorkBook wb = WorkBook.Load("Weather.xlsx"); // Your Excel file Name
WorkSheet ws = wb.GetWorkSheet("Sheet1");
// Parse worksheet into datatable
DataTable dt = ws.ToDataTable(true); // Parse Sheet1 of sample.xlsx file into DataTable
// Iterate through rows and columns to display their values
foreach (DataRow row in dt.Rows) // Access rows
{
for (int i = 0; i < dt.Columns.Count; i++) // Access columns of corresponding row
{
Console.Write(row[i] + " ");
}
Console.WriteLine();
}
}
}Friend Class Program
Shared Sub Main(ByVal args() As String)
' Load the workbook and access a specific worksheet
Dim wb As WorkBook = WorkBook.Load("Weather.xlsx") ' Your Excel file Name
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
' Parse worksheet into datatable
Dim dt As DataTable = ws.ToDataTable(True) ' Parse Sheet1 of sample.xlsx file into DataTable
' Iterate through rows and columns to display their values
For Each row As DataRow In dt.Rows ' Access rows
For i As Integer = 0 To dt.Columns.Count - 1 ' Access columns of corresponding row
Console.Write(row(i) & " ")
Next i
Console.WriteLine()
Next row
End Sub
End Class
DataTable Nesnesinden Konsol Çıkışı
Bu örnekte, bir Excel dosyasını DataSet olarak nasıl kullanacağımızı göreceğiz.
class Program
{
static void Main(string[] args)
{
// Load the workbook and convert it to a DataSet
WorkBook wb = WorkBook.Load("sample.xlsx");
DataSet ds = wb.ToDataSet(); // Parse WorkBook wb into DataSet
// Iterate through tables to display their names
foreach (DataTable dt in ds.Tables)
{
Console.WriteLine(dt.TableName);
}
}
}Friend Class Program
Shared Sub Main(ByVal args() As String)
' Load the workbook and convert it to a DataSet
Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
Dim ds As DataSet = wb.ToDataSet() ' Parse WorkBook wb into DataSet
' Iterate through tables to display their names
For Each dt As DataTable In ds.Tables
Console.WriteLine(dt.TableName)
Next dt
End Sub
End Class
DataSet Nesnesinden Sayfa Adına Erişim
Tüm Excel sayfaları boyunca her hücre değerine nasıl erişileceğini gösteren başka bir örneğe bakalım. Burada, bir Excel dosyasındaki her çalışma sayfasının her hücre değerine erişebiliriz.
class Program
{
static void Main(string[] args)
{
// Load the workbook and convert it to a DataSet
WorkBook wb = WorkBook.Load("Weather.xlsx");
DataSet ds = wb.ToDataSet(); // Treat the complete Excel file as DataSet
// Iterate through each table and its rows and columns
foreach (DataTable dt in ds.Tables) // Treat Excel WorkSheet as DataTable
{
foreach (DataRow row in dt.Rows) // Corresponding Sheet's Rows
{
for (int i = 0; i < dt.Columns.Count; i++) // Sheet columns of corresponding row
{
Console.Write(row[i] + " ");
}
Console.WriteLine();
}
}
}
}Friend Class Program
Shared Sub Main(ByVal args() As String)
' Load the workbook and convert it to a DataSet
Dim wb As WorkBook = WorkBook.Load("Weather.xlsx")
Dim ds As DataSet = wb.ToDataSet() ' Treat the complete Excel file as DataSet
' Iterate through each table and its rows and columns
For Each dt As DataTable In ds.Tables ' Treat Excel WorkSheet as DataTable
For Each row As DataRow In dt.Rows ' Corresponding Sheet's Rows
For i As Integer = 0 To dt.Columns.Count - 1 ' Sheet columns of corresponding row
Console.Write(row(i) & " ")
Next i
Console.WriteLine()
Next row
Next dt
End Sub
End Class
DataSet Nesnesinin Konsol Çıktısı
5. C# .NET ile CSV ayrıştırma
CSV dosyalarının, satır sonlarının alanlarda nasıl ele alındığı veya basit bir dize ayırma yaklaşımını tamamen engelleyen tekliflerle kapsanan alanların nasıl işleneceği konusunda bolluğu vardır. C# .NET'te CSV dönüştürürken, değerlere virgülle ayrılmasını yerine özelleştirilebilir bir ayırıcıyı belirterek yakın zamanda şu seçenekleri keşfettim: string.Split(',')
6. C# Kayıtlarında CSV Verilerini Okuma
Bu süreç okuyucuyu bir sonraki dosya boyunca ilerletir. TryGetField'de CSV alan dosyalarını okuruz. CSV dosyalarının alan alanlarında kayıt alanları olarak okuma işlevini kullanırız.
// Load a CSV file, specify the file format and delimiter
WorkBook workbook = WorkBook.LoadCSV("Weather.csv", fileFormat: ExcelFileFormat.XLSX, ListDelimiter: ",");
// Access the default worksheet from the workbook
WorkSheet ws = workbook.DefaultWorkSheet;
// Convert worksheet to DataTable
DataTable dt = ws.ToDataTable(true); // Parse Sheet1 of sample.xlsx file into DataTable
// Iterate through rows and columns to display their values
foreach (DataRow row in dt.Rows) // Access rows
{
for (int i = 0; i < dt.Columns.Count; i++) // Access columns of corresponding row
{
Console.Write(row[i] + " ");
}
Console.WriteLine();
}' Load a CSV file, specify the file format and delimiter
Dim workbook As WorkBook = WorkBook.LoadCSV("Weather.csv", fileFormat:= ExcelFileFormat.XLSX, ListDelimiter:= ",")
' Access the default worksheet from the workbook
Dim ws As WorkSheet = workbook.DefaultWorkSheet
' Convert worksheet to DataTable
Dim dt As DataTable = ws.ToDataTable(True) ' Parse Sheet1 of sample.xlsx file into DataTable
' Iterate through rows and columns to display their values
For Each row As DataRow In dt.Rows ' Access rows
For i As Integer = 0 To dt.Columns.Count - 1 ' Access columns of corresponding row
Console.Write(row(i) & " ")
Next i
Console.WriteLine()
Next row
DataTable'dan Konsol Çıkışı
7. Excel Dosyalarından Veri Alma
Artık açık Excel WorkSheet'den çeşitli yöntemler kullanarak her türlü veriyi kolayca edinebiliriz. Aşağıdaki örnekte, belirli bir hücre değerine nasıl erişileceğini ve onu string olarak nasıl ayrıştırılacağını görebiliriz:
// Access the data by cell addressing
string val = ws["Cell Address"].ToString();' Access the data by cell addressing
Dim val As String = ws("Cell Address").ToString()Yukarıdaki satırda, ws WorkSheet'dür, adım 2'de tanımlanmıştır. Bu 'basit' bir yaklaşımdır, ancak daha fazla bilgiyi ve Excel dosyası verilerine nasıl erişileceğine dair farklı örnekleri okuyabilirsiniz.
8. How to Parse Excel Files in C#
Uygulama oluşturma için Excel Elektronik Tabloları kullanırken, sık sık veriye dayalı olarak sonuçları analiz eder ve doğru sonuçları elde etmek için C# içinde Excel dosya verilerini istenen forma ayrıştırmamız gerekir. Verileri farklı formatlara ayrıştırmak, IronXL kullanarak C# ortamında kolaylaştırılmıştır; aşağıdaki adımlara bakın.
using IronXL;
class Program
{
static void Main(string[] args)
{
// Load the workbook and access a specific worksheet
WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
// Parse Excel cell value into string
string str_val = ws["B3"].Value.ToString();
// Parse Excel cell value into Int32
Int32 int32_val = ws["G3"].Int32Value;
// Parse Excel cell value into Decimal
decimal decimal_val = ws["E5"].DecimalValue;
// Output parsed values to the console
Console.WriteLine("Parse B3 Cell Value into String: {0}", str_val);
Console.WriteLine("Parse G3 Cell Value into Int32: {0}", int32_val);
Console.WriteLine("Parse E5 Cell Value into decimal: {0}", decimal_val);
Console.ReadKey();
}
}Imports IronXL
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Load the workbook and access a specific worksheet
Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
' Parse Excel cell value into string
Dim str_val As String = ws("B3").Value.ToString()
' Parse Excel cell value into Int32
Dim int32_val As Int32 = ws("G3").Int32Value
' Parse Excel cell value into Decimal
Dim decimal_val As Decimal = ws("E5").DecimalValue
' Output parsed values to the console
Console.WriteLine("Parse B3 Cell Value into String: {0}", str_val)
Console.WriteLine("Parse G3 Cell Value into Int32: {0}", int32_val)
Console.WriteLine("Parse E5 Cell Value into decimal: {0}", decimal_val)
Console.ReadKey()
End Sub
End Class9. Excel Verilerini Sayısal ve Boolean Değerlere Ayrıştırma
Şimdi Excel dosya verilerini nasıl ayrıştıracağımıza geçiyoruz. Öncelikle, sayısal Excel verileri ile nasıl başa çıkacağımıza ve ardından bunu istediğimiz formata nasıl ayrıştıracağımıza bakıyoruz.
Her Veri Türü İçin Özet Tablo
class Program
{
static void Main(string[] args)
{
// Load the workbook and access a specific worksheet
WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
// Parse Excel cell value into string
string str_val = ws["B3"].Value.ToString();
// Parse Excel cell value into Int32
Int32 int32_val = ws["G3"].Int32Value;
// Parse Excel cell value into Decimal
decimal decimal_val = ws["E5"].DecimalValue;
// Output parsed values to the console
Console.WriteLine("Parse B3 Cell Value into String: {0}", str_val);
Console.WriteLine("Parse G3 Cell Value into Int32: {0}", int32_val);
Console.WriteLine("Parse E5 Cell Value into decimal: {0}", decimal_val);
Console.ReadKey();
}
}Friend Class Program
Shared Sub Main(ByVal args() As String)
' Load the workbook and access a specific worksheet
Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
' Parse Excel cell value into string
Dim str_val As String = ws("B3").Value.ToString()
' Parse Excel cell value into Int32
Dim int32_val As Int32 = ws("G3").Int32Value
' Parse Excel cell value into Decimal
Dim decimal_val As Decimal = ws("E5").DecimalValue
' Output parsed values to the console
Console.WriteLine("Parse B3 Cell Value into String: {0}", str_val)
Console.WriteLine("Parse G3 Cell Value into Int32: {0}", int32_val)
Console.WriteLine("Parse E5 Cell Value into decimal: {0}", decimal_val)
Console.ReadKey()
End Sub
End ClassBu kod aşağıdaki çıktıyı görüntüleyecektir:
Doğru Veri Türü ile Konsol Çıkışı
Ve burada Excel dosyası sample.xlsx'in değerlerini görebiliriz:
Excel'de Doğru Veri Türünü Görüntüleme
Excel dosyası verilerini Boolean veri türüne ayrıştırmak için IronXL, BoolValue işlevini sağlar. Aşağıda görüldüğü gibi kullanılabilir:
// Access a cell value as a boolean
bool Val = ws["Cell Address"].BoolValue;' Access a cell value as a boolean
Dim Val As Boolean = ws("Cell Address").BoolValue10. Excel Dosyalarını C# Koleksiyonlarına Nasıl Ayrıştırılır
class Program
{
static void Main(string[] args)
{
// Load the workbook and access a specific worksheet
WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
// Convert a range into an array
var array = ws["B6:F6"].ToArray();
// Get the count of items in the array
int item = array.Count();
// Get the first item as a string
string total_items = array[0].Value.ToString();
// Output information about the array to the console
Console.WriteLine("First item in the array: {0}", item);
Console.WriteLine("Total items from B6 to F6: {0}", total_items);
Console.ReadKey();
}
}Friend Class Program
Shared Sub Main(ByVal args() As String)
' Load the workbook and access a specific worksheet
Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
' Convert a range into an array
Dim array = ws("B6:F6").ToArray()
' Get the count of items in the array
Dim item As Integer = array.Count()
' Get the first item as a string
Dim total_items As String = array(0).Value.ToString()
' Output information about the array to the console
Console.WriteLine("First item in the array: {0}", item)
Console.WriteLine("Total items from B6 to F6: {0}", total_items)
Console.ReadKey()
End Sub
End Class10.1 Bir Excel WorkSheet'i DataTable'a Nasıl Ayrıştırılır
IronXL'un mükemmel bir özelliği, belirli bir Excel WorkSheet'yi kolayca bir DataTable'a dönüştürebilmemizdir. Bu amaçla, IronXL'un .ToDataTable() işlevini aşağıdaki şekilde kullanabiliriz:
class Program
{
static void Main(string[] args)
{
// Load the workbook and access a specific worksheet
WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
// Parse Sheet1 of sample.xlsx file into DataTable
// Setting 'true' makes the first row in Excel as the column names in DataTable
DataTable dt = ws.ToDataTable(true);
}
}Friend Class Program
Shared Sub Main(ByVal args() As String)
' Load the workbook and access a specific worksheet
Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
' Parse Sheet1 of sample.xlsx file into DataTable
' Setting 'true' makes the first row in Excel as the column names in DataTable
Dim dt As DataTable = ws.ToDataTable(True)
End Sub
End Class10.2 Bir Excel Dosyasını DataSet'e Ayrıştırma
Tam bir Excel dosyasını bir DataSet'e ayrıştırmak istersek, bu amaçla IronXL'deki .ToDataSet() işlevini kullanabiliriz.
class Program
{
static void Main(string[] args)
{
// Load an entire workbook into a DataSet
WorkBook wb = WorkBook.Load("sample.xlsx");
// Convert workbook to DataSet
DataSet ds = wb.ToDataSet();
// We can also get a DataTable from the DataSet which corresponds to a WorkSheet
DataTable dt = ds.Tables[0];
}
}Friend Class Program
Shared Sub Main(ByVal args() As String)
' Load an entire workbook into a DataSet
Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
' Convert workbook to DataSet
Dim ds As DataSet = wb.ToDataSet()
' We can also get a DataTable from the DataSet which corresponds to a WorkSheet
Dim dt As DataTable = ds.Tables(0)
End Sub
End Class10.3 Belirli Bir Aralıkta Excel Verilerini Okuma
IronXL, belirli bir aralıkta Excel dosya verilerini okumak için akıllı bir yöntem sağlar. Aralık hem satırlara hem de sütunlara uygulanabilir.
class Program
{
static void Main(string[] args)
{
// Load the workbook and access a specific worksheet
WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
// Get specified range values by loop
foreach (var item in ws["B3:B8"])
{
Console.WriteLine("Value is: {0}", item);
}
Console.ReadKey();
}
}Friend Class Program
Shared Sub Main(ByVal args() As String)
' Load the workbook and access a specific worksheet
Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
' Get specified range values by loop
For Each item In ws("B3:B8")
Console.WriteLine("Value is: {0}", item)
Next item
Console.ReadKey()
End Sub
End ClassYukarıdaki kod aşağıdaki çıktıyı görüntüler:
B3:B8 Aralığındaki Tüm Değerlere Erişmek İçin Konsol Çıkışı
Ve Excel dosyası sample.xlsx değerlerini üretir:
Sample.xlsx'ten Veri Görüntüleme
Ek olarak, IronXL birçok Excel metodu ile uyumludur ve hücrelerle etkileşime geçirmek için şekillendirme ve kenarlık, matematik fonksiyonları, koşullu biçimlendirme veya mevcut verilerden grafikler oluşturma dahil olmak üzere kullanılabilir.
11. Bir Excel Dosyasında Boolean Veriyi Okuma
Uygulama geliştirmede, Excel dosyalarındaki Boolean veri tipine dayalı olarak kararlar almamız gerekir.
class Program
{
static void Main(string[] args)
{
// Load the workbook and access a specific worksheet
WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
// Traverse a range and output boolean values
foreach (var item in ws["G1:G10"])
{
Console.WriteLine("Condition is: {0}", item.BoolValue);
}
Console.ReadKey();
}
}Module Program
Sub Main(args As String())
' Load the workbook and access a specific worksheet
Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
' Traverse a range and output boolean values
For Each item In ws("G1:G10")
Console.WriteLine("Condition is: {0}", item.BoolValue)
Next
Console.ReadKey()
End Sub
End ModuleBundan, elde edilen çıktı şu şekildedir:
Boolean Veri Alımından Konsol Çıkışı
Ve Excel dosyası sample.xlsx, C1'den C10'a kadar olan değerlerle:
Konsol Çıkışı ile Karşılaştırmak için Excel Örneği
12. Tam Excel Çalışma Sayfası Nasıl Okunur
Tam bir Excel Çalışma Sayfasını satır ve sütun indekslerini kullanarak okumak basittir. Bu amaçla, tüm satırları ve belirli bir satırdaki tüm sütunları dolaşmak için iki döngü kullanırız. Sonra, tüm Excel Çalışma Sayfası içindeki tüm hücre değerlerini kolayca elde edebiliriz.
class Program
{
static void Main(string[] args)
{
// Load the workbook and access a specific worksheet
WorkBook wb = WorkBook.Load("Weather.xlsx"); // Your Excel File Name
WorkSheet ws = wb.GetWorkSheet("Sheet1");
// Traverse all rows of Excel WorkSheet
for (int i = 0; i < ws.Rows.Count(); i++)
{
// Traverse all columns of specific Row
for (int j = 0; j < ws.Columns.Count(); j++)
{
// Get the values
string val = ws.Rows[i].Columns[j].Value.ToString();
Console.WriteLine("Value of Row {0} and Column {1} is: {2}", i, j, val);
}
}
Console.ReadKey();
}
}Friend Class Program
Shared Sub Main(ByVal args() As String)
' Load the workbook and access a specific worksheet
Dim wb As WorkBook = WorkBook.Load("Weather.xlsx") ' Your Excel File Name
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
' Traverse all rows of Excel WorkSheet
For i As Integer = 0 To ws.Rows.Count() - 1
' Traverse all columns of specific Row
For j As Integer = 0 To ws.Columns.Count() - 1
' Get the values
Dim val As String = ws.Rows(i).Columns(j).Value.ToString()
Console.WriteLine("Value of Row {0} and Column {1} is: {2}", i, j, val)
Next j
Next i
Console.ReadKey()
End Sub
End Class
Tüm Değerleri Okumaktan Konsol Çıkışı
13. Interop Olmadan Excel Dosyalarını Okuma
IronXL, geliştiricilerin, XLS ve XLSX Belgelerinden Excel verilerini okumalarını ve düzenlemelerini sağlayan bir Excel Kitaplığıdır ve .NET Microsoft.Office.Interop.Excel kullanmadan çalışır.
API, Excel dosyalarını sezgisel bir şekilde oluşturma, okuma, manipüle etme, kaydetme ve dışa aktarma imkanı sağlar:
- .NET Framework 4.5+
- .NET Core 2+
- .NET Standard
- Xamarin
- Windows Mobile
- Mono
- & Azure Cloud Hosting
- Blazor
- .NET MAUI
Aşağıdaki ad alanlarını ekleyin:
using IronXL;
using System;
using System.Linq;Imports IronXL
Imports System
Imports System.LinqAşağıdaki kodu main fonksiyonunun içine yazınız.
class Program
{
static void Main(string[] args)
{
// Load an Excel file and access the first worksheet
WorkBook workbook = WorkBook.Load("Weather.xlsx");
WorkSheet sheet = workbook.WorkSheets.First();
// Select cells easily in Excel notation and return the calculated value
int cellValue = sheet["A2"].IntValue;
// Read from ranges of cells elegantly
foreach (var cell in sheet["A2:A10"])
{
Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);
}
}
}Friend Class Program
Shared Sub Main(ByVal args() As String)
' Load an Excel file and access the first worksheet
Dim workbook As WorkBook = WorkBook.Load("Weather.xlsx")
Dim sheet As WorkSheet = workbook.WorkSheets.First()
' Select cells easily in Excel notation and return the calculated value
Dim cellValue As Integer = sheet("A2").IntValue
' Read from ranges of cells elegantly
For Each cell In sheet("A2:A10")
Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text)
Next cell
End Sub
End Class
Her hücreden konsol çıktısı
IronXL ayrıca ASP.NET, MVC, Windows, macOS, Linux, iOS ve Android Mobil uygulama geliştirmeyi tam olarak destekler.
14. Sonuç ve IronXL Özel Teklifi
C#'ta CSV ayrıştırmasının yanı sıra, IronXL sadece iki satır kodla CSV dosyalarını Excel'e dönüştürür!
C# veya VB.NET kullanarak, IronXL'in Excel API'sini Interop gereksinimi olmadan kullanmak çok kolaydır. Excel tablolarını okuyabilir, düzenleyebilir ve oluşturabilir veya XLS/XLSX/CSV/TSV gibi diğer Excel formatlarıyla çalışabilirsiniz. Birden fazla çerçeve desteği ile iki ürün fiyatına beş ürün satın alabilirsiniz. Daha fazla bilgi için fiyatlandırma sayfamıza tıklayın.
Iron Suite'in 5 ürünü

Curtis Chau, Bilgisayar Bilimleri alanında Lisans Derecesine (Carleton Üniversitesi) sahip ve Node.js, TypeScript, JavaScript ve React konularında uzmanlaşmış ön uç geliştirmeyle ilgileniyor. Sezgisel ve estetik açıdan hoş kullanıcı arayüzleri oluşturma tutkunu, Curtis modern çerçevelerle çalışmayı ve iyi yapılandırılmış, görsel olarak çekici kılavuzlar oluşturmayı seviyor.
İlgili Makaleler



