// Import WorkSheet by various methods// by sheet indexingWorkSheet mySheet = wb.WorkSheets[SheetIndex];// get default WorkSheetWorkSheet defaultSheet = wb.DefaultWorkSheet;// get first WorkSheetWorkSheet firstSheet = wb.WorkSheets.First();// for the first or default sheetWorkSheet firstOrDefaultSheet = wb.WorkSheets.FirstOrDefault();
// Import WorkSheet by various methods
// by sheet indexing
WorkSheet mySheet = wb.WorkSheets[SheetIndex];
// get default WorkSheet
WorkSheet defaultSheet = wb.DefaultWorkSheet;
// get first WorkSheet
WorkSheet firstSheet = wb.WorkSheets.First();
// for the first or default sheet
WorkSheet firstOrDefaultSheet = wb.WorkSheets.FirstOrDefault();
' Import WorkSheet by various methods' by sheet indexingDim mySheet AsWorkSheet = wb.WorkSheets(SheetIndex)' get default WorkSheetDim defaultSheet AsWorkSheet = wb.DefaultWorkSheet' get first WorkSheetDim firstSheet AsWorkSheet = wb.WorkSheets.First()' for the first or default sheetDim firstOrDefaultSheet AsWorkSheet = wb.WorkSheets.FirstOrDefault()
' Import WorkSheet by various methods
' by sheet indexing
Dim mySheet As WorkSheet = wb.WorkSheets(SheetIndex)
' get default WorkSheet
Dim defaultSheet As WorkSheet = wb.DefaultWorkSheet
' get first WorkSheet
Dim firstSheet As WorkSheet = wb.WorkSheets.First()
' for the first or default sheet
Dim firstOrDefaultSheet As WorkSheet = wb.WorkSheets.FirstOrDefault()
// Import Data by Cell Address// by cell addressingstring val = ws["Cell Address"].ToString();// by row and column indexingstring valWithIndexing = ws.Rows[RowIndex].Columns[ColumnIndex].Value.ToString();// for numeric valuesdecimal numericValue = ws["B2"].DecimalValue;// for date valuesDateTime dateValue = ws["C2"].DateTimeValue;
// Import Data by Cell Address
// by cell addressing
string val = ws["Cell Address"].ToString();
// by row and column indexing
string valWithIndexing = ws.Rows[RowIndex].Columns[ColumnIndex].Value.ToString();
// for numeric values
decimal numericValue = ws["B2"].DecimalValue;
// for date values
DateTime dateValue = ws["C2"].DateTimeValue;
' Import Data by Cell Address' by cell addressingDim val AsString = ws("Cell Address").ToString()' by row and column indexingDim valWithIndexing AsString = ws.Rows(RowIndex).Columns(ColumnIndex).Value.ToString()' for numeric valuesDim numericValue AsDecimal = ws("B2").DecimalValue' for date valuesDim dateValue AsDateTime = ws("C2").DateTimeValue
' Import Data by Cell Address
' by cell addressing
Dim val As String = ws("Cell Address").ToString()
' by row and column indexing
Dim valWithIndexing As String = ws.Rows(RowIndex).Columns(ColumnIndex).Value.ToString()
' for numeric values
Dim numericValue As Decimal = ws("B2").DecimalValue
' for date values
Dim dateValue As DateTime = ws("C2").DateTimeValue
using IronXL;using System;// Import Excel WorkBookWorkBook wb = WorkBook.Load("sample.xlsx");// Specify WorkSheetWorkSheet ws = wb.GetWorkSheet("Sheet1");// Import data of specific cellstring val = ws["A4"].Value.ToString();Console.WriteLine("Import Value of A4 Cell address: {0}", val);Console.WriteLine("import Values in Range From B3 To B9 :\n");// Import data in specific rangeforeach (var item in ws["B3:B9"]){Console.WriteLine(item.Value.ToString());}Console.ReadKey();
using IronXL;
using System;
// Import Excel WorkBook
WorkBook wb = WorkBook.Load("sample.xlsx");
// Specify WorkSheet
WorkSheet ws = wb.GetWorkSheet("Sheet1");
// Import data of specific cell
string val = ws["A4"].Value.ToString();
Console.WriteLine("Import Value of A4 Cell address: {0}", val);
Console.WriteLine("import Values in Range From B3 To B9 :\n");
// Import data in specific range
foreach (var item in ws["B3:B9"])
{
Console.WriteLine(item.Value.ToString());
}
Console.ReadKey();
ImportsIronXLImportsSystem' Import Excel WorkBookDim wb AsWorkBook = WorkBook.Load("sample.xlsx")' Specify WorkSheetDim ws AsWorkSheet = wb.GetWorkSheet("Sheet1")' Import data of specific cellDim val AsString = ws("A4").Value.ToString()Console.WriteLine("Import Value of A4 Cell address: {0}", val)Console.WriteLine("import Values in Range From B3 To B9 :" & vbCrLf)' Import data in specific rangeFor Each item In ws("B3:B9")Console.WriteLine(item.Value.ToString())NextConsole.ReadKey()
Imports IronXL
Imports System
' Import Excel WorkBook
Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
' Specify WorkSheet
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
' Import data of specific cell
Dim val As String = ws("A4").Value.ToString()
Console.WriteLine("Import Value of A4 Cell address: {0}", val)
Console.WriteLine("import Values in Range From B3 To B9 :" & vbCrLf)
' Import data in specific range
For Each item In ws("B3:B9")
Console.WriteLine(item.Value.ToString())
Next
Console.ReadKey()
// To find the sum of a specific cell rangevar sum = ws["Starting Cell Address:Ending Cell Address"].Sum();
// To find the sum of a specific cell range
var sum = ws["Starting Cell Address:Ending Cell Address"].Sum();
' To find the sum of a specific cell rangeDim sum = ws("Starting Cell Address:Ending Cell Address").Sum()
' To find the sum of a specific cell range
Dim sum = ws("Starting Cell Address:Ending Cell Address").Sum()
Average()
// To find the average of a specific cell rangevar average = ws["Starting Cell Address:Ending Cell Address"].Avg();
// To find the average of a specific cell range
var average = ws["Starting Cell Address:Ending Cell Address"].Avg();
' To find the average of a specific cell rangeDim average = ws("Starting Cell Address:Ending Cell Address").Avg()
' To find the average of a specific cell range
Dim average = ws("Starting Cell Address:Ending Cell Address").Avg()
Min()
// To find the minimum in a specific cell rangevar minimum = ws["Starting Cell Address:Ending Cell Address"].Min();
// To find the minimum in a specific cell range
var minimum = ws["Starting Cell Address:Ending Cell Address"].Min();
' To find the minimum in a specific cell rangeDim minimum = ws("Starting Cell Address:Ending Cell Address").Min()
' To find the minimum in a specific cell range
Dim minimum = ws("Starting Cell Address:Ending Cell Address").Min()
Max()
// To find the maximum in a specific cell rangevar maximum = ws["Starting Cell Address:Ending Cell Address"].Max();
// To find the maximum in a specific cell range
var maximum = ws["Starting Cell Address:Ending Cell Address"].Max();
' To find the maximum in a specific cell rangeDim maximum = ws("Starting Cell Address:Ending Cell Address").Max()
' To find the maximum in a specific cell range
Dim maximum = ws("Starting Cell Address:Ending Cell Address").Max()
同時使用多個聚合函式
深入了解在Excel for C#中使用聚合函式的操作,並了解更多關於以不同方法提取資料的資訊。 這些函式對於生成總結統計資料或驗證匯入資料特別有用。
查看應用這些函式匯入Excel檔案資料的範例:
using IronXL;using System;// Import Excel fileWorkBook wb = WorkBook.Load("sample.xlsx");// Specify WorkSheetWorkSheet ws = wb.GetWorkSheet("Sheet1");// Import Excel file data by applying aggregate functionsdecimal sum = ws["D2:D9"].Sum();decimal avg = ws["D2:D9"].Avg();decimal min = ws["D2:D9"].Min();decimal max = ws["D2:D9"].Max();Console.WriteLine("Sum From D2 To D9: {0}", sum);Console.WriteLine("Avg From D2 To D9: {0}", avg);Console.WriteLine("Min From D2 To D9: {0}", min);Console.WriteLine("Max From D2 To D9: {0}", max);Console.ReadKey();
using IronXL;
using System;
// Import Excel file
WorkBook wb = WorkBook.Load("sample.xlsx");
// Specify WorkSheet
WorkSheet ws = wb.GetWorkSheet("Sheet1");
// Import Excel file data by applying aggregate functions
decimal sum = ws["D2:D9"].Sum();
decimal avg = ws["D2:D9"].Avg();
decimal min = ws["D2:D9"].Min();
decimal max = ws["D2:D9"].Max();
Console.WriteLine("Sum From D2 To D9: {0}", sum);
Console.WriteLine("Avg From D2 To D9: {0}", avg);
Console.WriteLine("Min From D2 To D9: {0}", min);
Console.WriteLine("Max From D2 To D9: {0}", max);
Console.ReadKey();
ImportsIronXLImportsSystem' Import Excel fileDim wb AsWorkBook = WorkBook.Load("sample.xlsx")' Specify WorkSheetDim ws AsWorkSheet = wb.GetWorkSheet("Sheet1")' Import Excel file data by applying aggregate functionsDim sum AsDecimal = ws("D2:D9").Sum()Dim avg AsDecimal = ws("D2:D9").Avg()Dim min AsDecimal = ws("D2:D9").Min()Dim max AsDecimal = ws("D2:D9").Max()Console.WriteLine("Sum From D2 To D9: {0}", sum)Console.WriteLine("Avg From D2 To D9: {0}", avg)Console.WriteLine("Min From D2 To D9: {0}", min)Console.WriteLine("Max From D2 To D9: {0}", max)Console.ReadKey()
Imports IronXL
Imports System
' Import Excel file
Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
' Specify WorkSheet
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
' Import Excel file data by applying aggregate functions
Dim sum As Decimal = ws("D2:D9").Sum()
Dim avg As Decimal = ws("D2:D9").Avg()
Dim min As Decimal = ws("D2:D9").Min()
Dim max As Decimal = ws("D2:D9").Max()
Console.WriteLine("Sum From D2 To D9: {0}", sum)
Console.WriteLine("Avg From D2 To D9: {0}", avg)
Console.WriteLine("Min From D2 To D9: {0}", min)
Console.WriteLine("Max From D2 To D9: {0}", max)
Console.ReadKey()
using IronXL;using System;using System.Data;WorkBook wb = WorkBook.Load("sample.xlsx");WorkSheet ws = wb.GetWorkSheet("Sheet1");// Import Excel data into a DataSetDataSet ds = wb.ToDataSet(true);Console.WriteLine("Excel file data imported to dataset successfully.");Console.ReadKey();
using IronXL;
using System;
using System.Data;
WorkBook wb = WorkBook.Load("sample.xlsx");
WorkSheet ws = wb.GetWorkSheet("Sheet1");
// Import Excel data into a DataSet
DataSet ds = wb.ToDataSet(true);
Console.WriteLine("Excel file data imported to dataset successfully.");
Console.ReadKey();
ImportsIronXLImportsSystemImportsSystem.DataDim wb AsWorkBook = WorkBook.Load("sample.xlsx")Dim ws AsWorkSheet = wb.GetWorkSheet("Sheet1")' Import Excel data into a DataSetDim ds AsDataSet = wb.ToDataSet(True)Console.WriteLine("Excel file data imported to dataset successfully.")Console.ReadKey()
Imports IronXL
Imports System
Imports System.Data
Dim wb As WorkBook = WorkBook.Load("sample.xlsx")
Dim ws As WorkSheet = wb.GetWorkSheet("Sheet1")
' Import Excel data into a DataSet
Dim ds As DataSet = wb.ToDataSet(True)
Console.WriteLine("Excel file data imported to dataset successfully.")
Console.ReadKey()
Can the first row of an Excel file be used as column names in IronXL?
Yes, when importing Excel data into a `DataSet`, you can set the first row as column names by using the `ToDataSet(true)` method in IronXL.
How do I import numeric or date values from Excel cells using IronXL?
In IronXL, you can import numeric values using `.DecimalValue` and date values using `.DateTimeValue` from specific Excel cells to ensure they are in the correct data type.
What is the advantage of using IronXL for Excel import in C# over Interop?
IronXL requires no Interop dependencies, making it ideal for server environments and cloud deployments where Microsoft Office installation is not feasible.
Is IronXL available for free for development purposes?
Yes, IronXL is available for free for development purposes, providing a comprehensive C# Excel API to simplify working with Excel files.