在生產環境中測試,無水印。
在任何需要的地方都能運行。
獲得 30 天的全功能產品。
在幾分鐘內上手運行。
試用產品期間完全訪問我們的支援工程團隊
處理各種 Excel 格式通常需要讀取數據,然後以編程方式重新配置。 在本文中,我們將學習如何使用IronXL在C#中讀取CSV文件和解析Excel電子表格中的數據,這是完成此工作的理想工具。
CSV 是一種簡單的數據格式,但在我們的 C# 專案中,程式上讀取 CSV 可能很困難,因為它使用多種分隔符來區分資料的行和列,這之間會有很多不同之處。 本文將向您展示如何使用IronXL 庫來讀取 CSV 檔案。
在您使用 IronXL 在 MVC、ASP.NET 或 .NET Core 中讀取 CSV 文件之前,您需要先安裝它。 以下是一個快速導覽。
在 Visual Studio 中,選擇「專案」選單
管理 NuGet 套件
搜索 IronXL.Excel
安裝
在 Visual Studio 的 NuGet 套件管理器中搜尋 IronXL
當您需要在 C# 中讀取 CSV 文件時,IronXL 是完美的工具。 您可以讀取以逗號或其他分隔符分隔的 CSV 檔案,如下面的代碼片段所示。
WorkBook workbook = WorkBook.LoadCSV("Weather.csv", fileFormat: ExcelFileFormat.XLSX, ListDelimiter: ",");
WorkSheet ws = workbook.DefaultWorkSheet;
workbook.SaveAs("Csv_To_Excel.xlsx");
WorkBook workbook = WorkBook.LoadCSV("Weather.csv", fileFormat: ExcelFileFormat.XLSX, ListDelimiter: ",");
WorkSheet ws = workbook.DefaultWorkSheet;
workbook.SaveAs("Csv_To_Excel.xlsx");
Dim workbook As WorkBook = WorkBook.LoadCSV("Weather.csv", fileFormat:= ExcelFileFormat.XLSX, ListDelimiter:= ",")
Dim ws As WorkSheet = workbook.DefaultWorkSheet
workbook.SaveAs("Csv_To_Excel.xlsx")
輸出:
輸出以逗號為分隔符的 CSV 檔案
程式碼解釋:
一個WorkBook
物件已被建立。 LoadCSV
方法用於 WorkBook 對象,然後用於指定 CSV 的名稱及其格式,識別在讀取的 CSV 文件中使用了哪些分隔符。 在這種情況下,逗號用作分隔符。
接著建立一個WorkSheet
物件。 這是將放置 CSV 檔案內容的地方。 然後將檔案以新的名稱和格式儲存。
顯示在 Microsoft Excel 中的數據
使用 IronXL 為您的專案提供一個簡化方式來處理 C# 中的 Excel 檔案格式。 您可以通過直接下載安裝IronXL。 或者,您可以使用NuGet Install for Visual Studio。 該軟體可免費用於開發。
Install-Package IronXL.Excel
WorkBook
並訪問 WorkSheet
WorkBook
是 IronXL 的類別,其物件提供完整訪問 Excel 檔案及其所有功能的權限。 例如,如果我們想要存取 Excel 檔案,我們會使用以下代碼:
WorkBook wb = WorkBook.Load("sample.xlsx");//Excel file path
WorkBook wb = WorkBook.Load("sample.xlsx");//Excel file path
Dim wb As WorkBook = WorkBook.Load("sample.xlsx") 'Excel file path
要訪問 Excel 文件的特定工作表,IronXL 提供了WorkSheet
類。
WorkSheet ws = wb.GetWorkSheet("Sheet1"); //by sheet name
WorkSheet ws = wb.GetWorkSheet("Sheet1"); //by sheet name
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1") 'by sheet name
一旦獲得 Excel 工作表 ws
,您就可以從中提取任何類型的數據,並在其上執行所有 Excel 功能。 可以通过此过程中的 Excel 工作表ws
访问数据:
using IronXL;
static void Main(string [] args)
{
WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
foreach (var cell in ws ["A2:A10"])
{
Console.WriteLine("value is: {0}", cell.Text);
}
Console.ReadKey();
}
using IronXL;
static void Main(string [] args)
{
WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
foreach (var cell in ws ["A2:A10"])
{
Console.WriteLine("value is: {0}", cell.Text);
}
Console.ReadKey();
}
Imports IronXL
Shared Sub Main(ByVal args() As String)
Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
For Each cell In ws ("A2:A10")
Console.WriteLine("value is: {0}", cell.Text)
Next cell
Console.ReadKey()
End Sub
使用 IronXL,將 Excel WorkSheet
作為 DataTable 操作非常容易。
DataTable dt = WorkSheet.ToDataTable();
DataTable dt = WorkSheet.ToDataTable();
Dim dt As DataTable = WorkSheet.ToDataTable()
使用以下命名空間
using IronXL;
using System.Data;
using IronXL;
using System.Data;
Imports IronXL
Imports System.Data
撰寫以下代碼:
static void Main(string [] args)
{
WorkBook wb = WorkBook.Load("Weather.xlsx"); // Your Excel file Name
WorkSheet ws = wb.GetWorkSheet("Sheet1");
DataTable dt = ws.ToDataTable(true);//parse sheet1 of sample.xlsx file into datatable
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();
}
}
static void Main(string [] args)
{
WorkBook wb = WorkBook.Load("Weather.xlsx"); // Your Excel file Name
WorkSheet ws = wb.GetWorkSheet("Sheet1");
DataTable dt = ws.ToDataTable(true);//parse sheet1 of sample.xlsx file into datatable
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();
}
}
Shared Sub Main(ByVal args() As String)
Dim wb As WorkBook = WorkBook.Load("Weather.xlsx") ' Your Excel file Name
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
Dim dt As DataTable = ws.ToDataTable(True) 'parse sheet1 of sample.xlsx file into datatable
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
從 DataTable 物件輸出的控制台輸出
在此示例中,我們將探討如何將 Excel 文件用作DataSet
。
static void Main(string [] args)
{
WorkBook wb = WorkBook.Load("sample.xlsx");
DataSet ds = wb.ToDataSet(); //Parse WorkBook wb into DataSet
foreach (DataTable dt in ds.Tables)
{
Console.WriteLine(dt.TableName);
}
}
static void Main(string [] args)
{
WorkBook wb = WorkBook.Load("sample.xlsx");
DataSet ds = wb.ToDataSet(); //Parse WorkBook wb into DataSet
foreach (DataTable dt in ds.Tables)
{
Console.WriteLine(dt.TableName);
}
}
Shared Sub Main(ByVal args() As String)
Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
Dim ds As DataSet = wb.ToDataSet() 'Parse WorkBook wb into DataSet
For Each dt As DataTable In ds.Tables
Console.WriteLine(dt.TableName)
Next dt
End Sub
從 DataSet 物件中存取表單名稱
讓我們看看另一個範例,如何存取所有 Excel 工作表中的每一個儲存格值。 在這裡,我們可以存取 Excel 文件中每個工作表的每個單元格的值。
static void Main(string [] args)
{
WorkBook wb = WorkBook.Load("Weather.xlsx");
DataSet ds = wb.ToDataSet();//behave complete Excel file as DataSet
foreach (DataTable dt in ds.Tables)//behave 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();
}
}
}
static void Main(string [] args)
{
WorkBook wb = WorkBook.Load("Weather.xlsx");
DataSet ds = wb.ToDataSet();//behave complete Excel file as DataSet
foreach (DataTable dt in ds.Tables)//behave 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();
}
}
}
Shared Sub Main(ByVal args() As String)
Dim wb As WorkBook = WorkBook.Load("Weather.xlsx")
Dim ds As DataSet = wb.ToDataSet() 'behave complete Excel file as DataSet
For Each dt As DataTable In ds.Tables 'behave 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
資料集物件的控制台輸出
CSV 有大量關於如何處理欄位中的換行符,以及如何將欄位包含在引號中的問題,這使得簡單的字串分割方法無法使用。 近期我發現了在 C# .NET 中轉換 CSV 時可以選擇以下選項,可以指定一個可自定義的分隔符,而不是使用 string.Split(',')
來用逗號分隔值。
此過程使讀者進入下一個檔案。我們在 trygetfield 中讀取 CSV 欄位檔案。 我們將 CSV 文件的欄位使用讀取功能作為記錄欄位。
WorkBook workbook = WorkBook.LoadCSV("Weather.csv", fileFormat: ExcelFileFormat.XLSX, ListDelimiter: ",");
WorkSheet ws = workbook.DefaultWorkSheet;
DataTable dt = ws.ToDataTable(true);//parse sheet1 of sample.xlsx file into datatable
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();
}
WorkBook workbook = WorkBook.LoadCSV("Weather.csv", fileFormat: ExcelFileFormat.XLSX, ListDelimiter: ",");
WorkSheet ws = workbook.DefaultWorkSheet;
DataTable dt = ws.ToDataTable(true);//parse sheet1 of sample.xlsx file into datatable
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();
}
Dim workbook As WorkBook = WorkBook.LoadCSV("Weather.csv", fileFormat:= ExcelFileFormat.XLSX, ListDelimiter:= ",")
Dim ws As WorkSheet = workbook.DefaultWorkSheet
Dim dt As DataTable = ws.ToDataTable(True) 'parse sheet1 of sample.xlsx file into datatable
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 的控制台輸出
現在我們可以輕鬆地使用各種方法從開放的 Excel 工作表獲取任何類型的數據。 在以下範例中,我們可以看到如何存取特定的儲存格值並將其解析為string
:
//Access the Data by Cell Addressing
string val = ws ["Cell Address"].ToString();
//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()
在上面的行中,ws 是在第 2 步中定義的工作表。這是「簡單」的方法,但您可以閱讀更多內容並查看不同的示例,了解如何訪問 Excel 檔案數據。
在使用 Excel 試算表進行應用程式構建時,我們通常基於數據分析結果,需要將 Excel 文件中的數據在 C# 中解析成所需格式以獲得正確的結果。 在 C# 環境中使用 IronXL 可以輕鬆將數據解析成不同格式; 查看以下步驟。
using IronXL;
static void Main(string [] args)
{
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;
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();
}
using IronXL;
static void Main(string [] args)
{
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;
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
Shared Sub Main(ByVal args() As String)
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
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
現在我們來談談如何解析Excel檔案數據。 首先,我們查看如何處理數字 Excel 資料,然後解析成我們所需的格式。
每種數據類型的摘要表
static void Main(string [] args)
{
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;
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();
}
static void Main(string [] args)
{
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;
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();
}
Shared Sub Main(ByVal args() As String)
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
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
此代碼將顯示以下輸出:
使用正確資料類型的控制台輸出
我們可以在這裡看到 Excel 檔案 sample.xlsx 的值:
在 Excel 中顯示正確的數據類型
若要將 Excel 文件數據解析為布林數據類型,IronXL 提供了BoolValue
函數。 它可以如下使用:
bool Val = ws ["Cell Address"].BoolValue;
bool Val = ws ["Cell Address"].BoolValue;
Dim Val As Boolean = ws ("Cell Address").BoolValue
static void Main(string [] args)
{
WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
var array = ws ["B6:F6"].ToArray();
int item = array.Count();
string total_items = array [0].Value.ToString();
Console.WriteLine("First item in the array: {0}", item);
Console.WriteLine("Total items from B6 to F6: {0}",total_items);
Console.ReadKey();
}
static void Main(string [] args)
{
WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
var array = ws ["B6:F6"].ToArray();
int item = array.Count();
string total_items = array [0].Value.ToString();
Console.WriteLine("First item in the array: {0}", item);
Console.WriteLine("Total items from B6 to F6: {0}",total_items);
Console.ReadKey();
}
Shared Sub Main(ByVal args() As String)
Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
Dim array = ws ("B6:F6").ToArray()
Dim item As Integer = array.Count()
Dim total_items As String = array (0).Value.ToString()
Console.WriteLine("First item in the array: {0}", item)
Console.WriteLine("Total items from B6 to F6: {0}",total_items)
Console.ReadKey()
End Sub
WorkSheet
解析為 DataTableIronXL 的一個出色功能是我們可以輕鬆地將特定的 Excel WorkSheet
轉換為 DataTable。 為此,我們可以使用 IronXL 的.ToDataTable()
函數如下:
static void Main(string [] args)
{
WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
//parse sheet1 of sample.xlsx file into DataTable.
//we set parameter true of ToDataTable() function,so first row of Excel file becomes columnname of DataTable
DataTable dt = ws.ToDataTable(true);
}
static void Main(string [] args)
{
WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
//parse sheet1 of sample.xlsx file into DataTable.
//we set parameter true of ToDataTable() function,so first row of Excel file becomes columnname of DataTable
DataTable dt = ws.ToDataTable(true);
}
Shared Sub Main(ByVal args() As String)
Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
'parse sheet1 of sample.xlsx file into DataTable.
'we set parameter true of ToDataTable() function,so first row of Excel file becomes columnname of DataTable
Dim dt As DataTable = ws.ToDataTable(True)
End Sub
如果我們想將整個 Excel 文件解析為 DataSet,則為此目的我們可以在 IronXL 中使用.ToDataSet()
函數。
static void Main(string [] args)
{
WorkBook wb = WorkBook.Load("sample.xlsx");
//parse WorkBook wb into DataSet
DataSet ds = wb.ToDataSet();
//we also can get DataTable from ds which is actually WorkSheet as:
DataTable dt=ds.Tables [0];
}
static void Main(string [] args)
{
WorkBook wb = WorkBook.Load("sample.xlsx");
//parse WorkBook wb into DataSet
DataSet ds = wb.ToDataSet();
//we also can get DataTable from ds which is actually WorkSheet as:
DataTable dt=ds.Tables [0];
}
Shared Sub Main(ByVal args() As String)
Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
'parse WorkBook wb into DataSet
Dim ds As DataSet = wb.ToDataSet()
'we also can get DataTable from ds which is actually WorkSheet as:
Dim dt As DataTable=ds.Tables (0)
End Sub
IronXL 提供了一種在特定範圍內讀取 Excel 文件數據的智能方法。 範圍可以應用於行和列。
static void Main(string [] args)
{
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();
}
static void Main(string [] args)
{
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();
}
Shared Sub Main(ByVal args() As String)
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
上述程式碼顯示以下輸出:
控制台輸出以訪問範圍 B3:B8 的所有值
並生成 Excel 文件 sample.xlsx 的值:
來自 sample.xlsx 的數據顯示
此外,IronXL 也相容於許多 Excel 方法以與儲存格互動,包括樣式和邊框、數學函數、條件格式或從可用的數據創建圖表。
在應用程式開發中,我們需要根據 Excel 文件中的布林數據類型做出決策。
static void Main(string [] args)
{
WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
foreach (var item in ws ["G1:G10"])
{
Console.WriteLine(" Condition is: {0}", item.BoolValue);
}
Console.ReadKey();
}
static void Main(string [] args)
{
WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
foreach (var item in ws ["G1:G10"])
{
Console.WriteLine(" Condition is: {0}", item.BoolValue);
}
Console.ReadKey();
}
Shared Sub Main(ByVal args() As String)
Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
For Each item In ws ("G1:G10")
Console.WriteLine(" Condition is: {0}", item.BoolValue)
Next item
Console.ReadKey()
End Sub
從這裡,我們得到輸出:
從獲取布林數據的控制台輸出
以及包含從 C1 到 C10 值的 Excel 文件 sample.xlsx:
Excel 範例以進行與控制台輸出的比較
使用行和列索引可以輕鬆讀取完整的Excel工作表。 為此,我們使用兩個循環:一個用於遍歷所有行,另一個用於遍歷特定行的所有列。 然後我們可以輕鬆獲取整個 Excel 工作表中的所有單元格值。
static void Main(string [] args)
{
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();
}
static void Main(string [] args)
{
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();
}
Shared Sub Main(ByVal args() As String)
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
從讀取所有值的控制台輸出
IronXL 是一個用於 C# 的Excel 庫和 .NET,它允許開發者從XLS 和 XLSX文件讀取和編輯 Excel 資料而不需使用 Microsoft.Office.Interop.Excel。
該 API 允許我們直觀地創建、讀取、操作、保存和導出 Excel 文件,用於:
.NET Framework 4.5+
.NET Core 2+
.NET 標準
Xamarin
Windows Mobile
Mono
& Azure 雲端托管
添加以下命名空間:
using IronXL;
using System;
using System.Linq;
using IronXL;
using System;
using System.Linq;
Imports IronXL
Imports System
Imports System.Linq
現在在主函數內寫下以下代碼。
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);
}
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);
}
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
每個單元格的控制台輸出
IronXL 也完全支持 ASP.NET、MVC、Windows、macOS、Linux、iOS 和 Android 行動應用程式開發。
除了在 C# 中進行CSV 解析,IronXL 只需兩行代碼即可將 CSV 文件轉換為 Excel!
使用C#或VB.NET,可以很輕鬆地使用IronXL的Excel API,而不需要Interop。您可以讀取、編輯和創建Excel試算表,或處理其他Excel格式,例如XLS/XLSX/CSV/TSV。 在多個框架支持下,您可以以兩個產品的價格購買五個產品。 點擊我們的定價頁面以獲取更多資訊。
Iron Suite 的 5 個產品