Sử dụng trình đọc dữ liệu excel C#

#1. NET BDD Framework. SpecFlow tự động hóa thử nghiệm của bạn và hoạt động với mã hiện tại của bạn. Tìm lỗi trước khi chúng xảy ra. Phát triển theo định hướng hành vi giúp các nhà phát triển, người thử nghiệm và đại diện doanh nghiệp hiểu rõ hơn về sự hợp tác của họ

focus-creative-games/luban

你的最佳游戏配置解决方案 {excel, csv, xls, xlsx, json, bson, xml, yaml, lua, unity scriptableobject} => {json, bson, xml, lua, yaml, protobuf(pb), msgpack, flatbuffers, erlang, custom template} data + {c++, java, c#, go(golang), lua, javascript(js), typescript(ts), erlang, rust, gdscript, protobuf schema, flatbuffers schema, custom template} code。

Cách cài đặt ExcelDataReader được đề xuất là từ NuGet, Vui lòng sử dụng lệnh sau để cài đặt nhanh hơn

Cài đặt ExcelDataReader từ NuGet

 Install-Package ExcelDataReader -Version 3.6.0

Đọc tệp Excel qua. NET API

ExcelDataReader cho phép C#. NET để đọc các tệp Microsoft Excel một cách dễ dàng và hiệu quả. Phương thức mở rộng AsDataSet() là một công cụ trợ giúp thuận tiện để nhanh chóng lấy dữ liệu. IExcelDataReader mở rộng Hệ thống. Dữ liệu. Giao diện IDataReader và IDataRecord để điều hướng và truy xuất dữ liệu ở mức thấp hơn

Đọc Đầu trang và Chân trang của Tệp Excel qua C#. NET

sing System;
using System.Text;

namespace ExcelDataReader.Core.BinaryFormat
{
    /// 
    /// Represents a string value of a header or footer.
    /// 
    internal sealed class XlsBiffHeaderFooterString : XlsBiffRecord
    {
        private readonly IXlsString _xlsString;

        internal XlsBiffHeaderFooterString(byte[] bytes, uint offset, int biffVersion)
            : base(bytes, offset)
        {
            if (biffVersion < 8)
                _xlsString = new XlsShortByteString(bytes, offset + 4);
            else if (biffVersion == 8)
                _xlsString = new XlsUnicodeString(bytes, offset + 4);
            else
                throw new ArgumentException("Unexpected BIFF version " + biffVersion, nameof(biffVersion));
        }

        /// 
        /// Gets the string value.
        /// 
        public string GetValue(Encoding encoding)
        {
            return _xlsString.GetValue(encoding);
        }
    }
}

Đọc sổ làm việc được bảo vệ qua. NET API

mã nguồn mở. NET API ExcelDataReader cũng cho phép bạn đọc các tài liệu Microsoft Excel được bảo vệ bằng mật khẩu. Bạn có thể đọc các tệp được bảo vệ bằng mật khẩu bằng cách sử dụng cài đặt mật khẩu trong cấu hình ExcelReaderConfiguration và mở nó bằng phương thức CreateOpenXmlReader()

Cách áp dụng định dạng cho ô bảng tính thông qua API C#

// Use the following code to Access your protected Spreadsheet file 
var conf = new ExcelReaderConfiguration { Password = "yourPassword" };
excelReader = ExcelReaderFactory.CreateOpenXmlReader(excelStream, conf);

Áp dụng định dạng cho các ô bảng tính Excel bằng C#

Thư viện ExcelDataReader mã nguồn mở cho phép các lập trình viên phần mềm áp dụng định dạng cho các ô excel của họ chỉ bằng một vài dòng mã C#. Xin lưu ý rằng ExcelDataReader không hỗ trợ trực tiếp các tính năng định dạng. Bạn cần truy xuất số của ô chứa chuỗi định dạng và sử dụng thư viện ExcelNumberFormat của bên thứ ba cho mục đích định dạng. Các ví dụ sau đây sẽ giúp bạn hiểu làm thế nào để đạt được nó

Hướng dẫn này giải thích cách đọc tệp Excel trong C# và cách sử dụng thư viện cho các tác vụ hàng ngày như xác thực, chuyển đổi sang cơ sở dữ liệu, lưu dữ liệu từ API Web và sửa đổi công thức trong bảng tính. Bài viết này tham khảo các mẫu mã IronXL, đây là một. Ứng dụng bảng điều khiển NET Core

Đọc và tạo các tệp Excel trong C# và tất cả các tệp khác. NET rất dễ sử dụng thư viện phần mềm IronXL

Nó không yêu cầu cài đặt Excel trên máy chủ của bạn hoặc Interop. IronXL cung cấp API nhanh hơn và trực quan hơn Microsoft. Văn phòng. tương tác. Excel


Tổng quan

Cách đọc tệp Excel trong C#

  1. Tải Thư viện C# để đọc file Excel
  2. Tải và đọc tệp Excel (sổ làm việc)
  3. Tạo sổ làm việc Excel trong CSV hoặc XLSX
  4. Chỉnh sửa giá trị ô trong một dải ô
  5. Xác thực dữ liệu bảng tính
  6. Xuất dữ liệu bằng Entity Framework

Đọc dữ liệu từ tệp Excel bằng IronXL

SắtXL là một. NET hỗ trợ đọc và chỉnh sửa tài liệu Microsoft Excel bằng C#. Hướng dẫn này sẽ hướng dẫn bạn cách sử dụng mã sắc nét C để đọc tệp Excel

  1. Cài đặt Thư viện IronXL Excel. Chúng tôi có thể thực hiện việc này bằng gói NuGet của mình hoặc bằng cách tải xuống. Net Excel DLL
  2. Sử dụng Sổ làm việc. Phương pháp tải để đọc bất kỳ tài liệu XLS, XLSX hoặc CSV nào
  3. Nhận các giá trị ô bằng cú pháp trực quan.
     PM > Install-Package IronXL.Excel
    7

SắtXL bao gồm

  • Hỗ trợ sản phẩm chuyên dụng từ chúng tôi. kỹ sư NET
  • Cài đặt dễ dàng qua Microsoft Visual Studio
  • Dùng thử miễn phí 30 ngày để phát triển. Giấy phép từ $749

Chúng ta sẽ thấy việc đọc tệp Excel bằng C# hoặc VB dễ dàng như thế nào. NET bằng thư viện IronXL. Các mẫu chứa ba bảng Excel

Sử dụng trình đọc dữ liệu excel C#

Đọc tệp XLS hoặc XLSX. Mã nhanh

Trong ví dụ này, chúng ta có thể thấy cách đọc tệp excel hiệu quả mà không cần Interop trong C#. Hoạt động nâng cao cuối cùng cho thấy khả năng tương thích Linq và toán học phạm vi tổng hợp

/**
Read XLS or XLSX File
anchor-read-an-xls-or-xlsx-file
**/
using IronXL;
using System.Linq;

//Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
WorkBook workbook = WorkBook.Load("test.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);
}

///Advanced Operations

//Calculate aggregate values such as Min, Max and Sum
decimal sum = sheet["A2:A10"].Sum();
//Linq compatible
decimal max = sheet["A2:A10"].Max(c => c.DecimalValue);

/**
Read XLS or XLSX File
anchor-read-an-xls-or-xlsx-file
**/
using IronXL;
using System.Linq;

//Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
WorkBook workbook = WorkBook.Load("test.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);
}

///Advanced Operations

//Calculate aggregate values such as Min, Max and Sum
decimal sum = sheet["A2:A10"].Sum();
//Linq compatible
decimal max = sheet["A2:A10"].Max(c => c.DecimalValue);
'''
'''Read XLS or XLSX File
'''anchor-read-an-xls-or-xlsx-file
'''*
Imports IronXL
Imports System.Linq

'Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
Private workbook As WorkBook = WorkBook.Load("test.xlsx")
Private sheet As WorkSheet = workbook.WorkSheets.First()
'Select cells easily in Excel notation and return the calculated value
Private 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

'''Advanced Operations

'Calculate aggregate values such as Min, Max and Sum
Dim sum As Decimal = sheet("A2:A10").Sum()
'Linq compatible
Dim max As Decimal = sheet("A2:A10").Max(Function(c) c.DecimalValue)

VB   C#


Bước 1

1. Tải xuống Thư viện IronXL C# MIỄN PHÍ

Sử dụng trình đọc dữ liệu excel C#

Tải xuống DLL

Tải xuống DLL

Cài đặt thủ công vào dự án của bạn

hoặc là

Sử dụng trình đọc dữ liệu excel C#

Cài đặt bằng NuGet

Install-Package IronXL.Excel

nuget. org/gói/IronXL. Excel/

Điều đầu tiên chúng ta cần làm là cài đặt IronXL. Thư viện Excel, thêm chức năng Excel vào. Nền tảng NET

Cài đặt IronXL. Excel, đạt được dễ dàng nhất bằng cách sử dụng gói NuGet của chúng tôi, mặc dù bạn cũng có thể chọn cài đặt DLL theo cách thủ công cho dự án của mình hoặc vào bộ đệm lắp ráp chung của bạn

Cài đặt Gói NuGet IronXL

  1. Trong Visual Studio, nhấp chuột phải vào dự án chọn "Quản lý gói NuGet. "
  2. Tìm kiếm IronXL. Gói và cài đặt Excel

    Sử dụng trình đọc dữ liệu excel C#


Một cách khác để cài đặt là

  1. Nhập Bảng điều khiển quản lý gói
  2. Nhập > Gói cài đặt IronXL. Excel
 PM > Install-Package IronXL.Excel


Ngoài ra, bạn có thể xem gói trên trang NuGet tại đây

Cài đặt tải xuống trực tiếp

Ngoài ra, chúng ta có thể bắt đầu bằng cách tải xuống IronXL. NET Excel DLL và cài đặt thủ công vào Visual Studio


Làm thế nào để hướng dẫn

2. Tải một WorkBook

Lớp

 PM > Install-Package IronXL.Excel
8 đại diện cho một trang tính Excel. Để mở một File Excel bằng C#, chúng ta sử dụng WorkBook. Tải và chỉ định đường dẫn của tệp Excel (. xlsx)

/**
Load WorkBook
anchor-load-a-workbook
**/
var workbook = WorkBook.Load(@"Spreadsheets\\GDP.xlsx");

/**
Load WorkBook
anchor-load-a-workbook
**/
var workbook = WorkBook.Load(@"Spreadsheets\\GDP.xlsx");
'''
'''Load WorkBook
'''anchor-load-a-workbook
'''*
Dim workbook = WorkBook.Load("Spreadsheets\\GDP.xlsx")

VB   C#

Vật mẫu. ExcelToDBBộ xử lý

Mỗi WorkBook có thể có nhiều đối tượng

 PM > Install-Package IronXL.Excel
9. Chúng đại diện cho các trang tính trong tài liệu Excel. Nếu trang tính chứa các trang tính, hãy truy xuất chúng theo tên
/**
Load WorkBook
anchor-load-a-workbook
**/
var workbook = WorkBook.Load(@"Spreadsheets\\GDP.xlsx");
0

var worksheet = workbook.GetWorkSheet("GDPByCountry");

var worksheet = workbook.GetWorkSheet("GDPByCountry");
/**
Read XLS or XLSX File
anchor-read-an-xls-or-xlsx-file
**/
using IronXL;
using System.Linq;

//Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
WorkBook workbook = WorkBook.Load("test.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);
}

///Advanced Operations

//Calculate aggregate values such as Min, Max and Sum
decimal sum = sheet["A2:A10"].Sum();
//Linq compatible
decimal max = sheet["A2:A10"].Max(c => c.DecimalValue);
0

VB   C#

Vật mẫu. ExcelToDB


3. Tạo Sổ làm việc

Để tạo một Sổ làm việc mới trong bộ nhớ, hãy tạo một Sổ làm việc mới với loại trang tính

/**
Read XLS or XLSX File
anchor-read-an-xls-or-xlsx-file
**/
using IronXL;
using System.Linq;

//Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
WorkBook workbook = WorkBook.Load("test.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);
}

///Advanced Operations

//Calculate aggregate values such as Min, Max and Sum
decimal sum = sheet["A2:A10"].Sum();
//Linq compatible
decimal max = sheet["A2:A10"].Max(c => c.DecimalValue);
1

/**
Read XLS or XLSX File
anchor-read-an-xls-or-xlsx-file
**/
using IronXL;
using System.Linq;

//Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
WorkBook workbook = WorkBook.Load("test.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);
}

///Advanced Operations

//Calculate aggregate values such as Min, Max and Sum
decimal sum = sheet["A2:A10"].Sum();
//Linq compatible
decimal max = sheet["A2:A10"].Max(c => c.DecimalValue);
1
/**
Read XLS or XLSX File
anchor-read-an-xls-or-xlsx-file
**/
using IronXL;
using System.Linq;

//Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
WorkBook workbook = WorkBook.Load("test.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);
}

///Advanced Operations

//Calculate aggregate values such as Min, Max and Sum
decimal sum = sheet["A2:A10"].Sum();
//Linq compatible
decimal max = sheet["A2:A10"].Max(c => c.DecimalValue);
3

VB   C#

Vật mẫu. Bộ xử lý ApiToExcel

Ghi chú. Sử dụng ExcelFileFormat. XLS dành cho kế thừa cho bảng tính Microsoft Excel (95 trở về trước)


4. Tạo một bảng tính

Mỗi "WorkBook" có thể có nhiều WorkSheet. "WorkSheet" là một trang tính dữ liệu, trong khi WorkBook đại diện cho một tập hợp các WorkSheet. Đây là giao diện của một sổ làm việc có hai trang tính trong Excel

Sử dụng trình đọc dữ liệu excel C#

Để tạo một WorkSheet mới, hãy gọi

/**
Load WorkBook
anchor-load-a-workbook
**/
var workbook = WorkBook.Load(@"Spreadsheets\\GDP.xlsx");
1 và chuyển tên của trang tính

/**
Read XLS or XLSX File
anchor-read-an-xls-or-xlsx-file
**/
using IronXL;
using System.Linq;

//Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
WorkBook workbook = WorkBook.Load("test.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);
}

///Advanced Operations

//Calculate aggregate values such as Min, Max and Sum
decimal sum = sheet["A2:A10"].Sum();
//Linq compatible
decimal max = sheet["A2:A10"].Max(c => c.DecimalValue);
4

/**
Read XLS or XLSX File
anchor-read-an-xls-or-xlsx-file
**/
using IronXL;
using System.Linq;

//Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
WorkBook workbook = WorkBook.Load("test.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);
}

///Advanced Operations

//Calculate aggregate values such as Min, Max and Sum
decimal sum = sheet["A2:A10"].Sum();
//Linq compatible
decimal max = sheet["A2:A10"].Max(c => c.DecimalValue);
4_______4_______6

VB   C#


5. Nhận phạm vi ô

Lớp "Phạm vi" đại diện cho một tập hợp hai chiều của các đối tượng "Ô". Nó đại diện cho một phạm vi ô Excel theo nghĩa đen. Lấy phạm vi bằng cách sử dụng bộ chỉ mục chuỗi trên đối tượng WorkSheet

Văn bản đối số là tọa độ của một ô (e. g. "A1") hoặc một dãy ô từ trái sang phải từ trên xuống dưới (e. g. "B2. E5"). Cũng có thể gọi

/**
Load WorkBook
anchor-load-a-workbook
**/
var workbook = WorkBook.Load(@"Spreadsheets\\GDP.xlsx");
2 trên WorkSheet

/**
Read XLS or XLSX File
anchor-read-an-xls-or-xlsx-file
**/
using IronXL;
using System.Linq;

//Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
WorkBook workbook = WorkBook.Load("test.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);
}

///Advanced Operations

//Calculate aggregate values such as Min, Max and Sum
decimal sum = sheet["A2:A10"].Sum();
//Linq compatible
decimal max = sheet["A2:A10"].Max(c => c.DecimalValue);
7

/**
Read XLS or XLSX File
anchor-read-an-xls-or-xlsx-file
**/
using IronXL;
using System.Linq;

//Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
WorkBook workbook = WorkBook.Load("test.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);
}

///Advanced Operations

//Calculate aggregate values such as Min, Max and Sum
decimal sum = sheet["A2:A10"].Sum();
//Linq compatible
decimal max = sheet["A2:A10"].Max(c => c.DecimalValue);
7_______4_______9

VB   C#

Vật mẫu. Xác nhận dữ liệu


6. Chỉnh sửa giá trị ô trong phạm vi

Có một số cách để đọc hoặc chỉnh sửa giá trị của các ô trong Phạm vi. Nếu đã biết số đếm, hãy sử dụng vòng lặp For

'''
'''Read XLS or XLSX File
'''anchor-read-an-xls-or-xlsx-file
'''*
Imports IronXL
Imports System.Linq

'Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
Private workbook As WorkBook = WorkBook.Load("test.xlsx")
Private sheet As WorkSheet = workbook.WorkSheets.First()
'Select cells easily in Excel notation and return the calculated value
Private 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

'''Advanced Operations

'Calculate aggregate values such as Min, Max and Sum
Dim sum As Decimal = sheet("A2:A10").Sum()
'Linq compatible
Dim max As Decimal = sheet("A2:A10").Max(Function(c) c.DecimalValue)
0

'''
'''Read XLS or XLSX File
'''anchor-read-an-xls-or-xlsx-file
'''*
Imports IronXL
Imports System.Linq

'Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
Private workbook As WorkBook = WorkBook.Load("test.xlsx")
Private sheet As WorkSheet = workbook.WorkSheets.First()
'Select cells easily in Excel notation and return the calculated value
Private 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

'''Advanced Operations

'Calculate aggregate values such as Min, Max and Sum
Dim sum As Decimal = sheet("A2:A10").Sum()
'Linq compatible
Dim max As Decimal = sheet("A2:A10").Max(Function(c) c.DecimalValue)
0
'''
'''Read XLS or XLSX File
'''anchor-read-an-xls-or-xlsx-file
'''*
Imports IronXL
Imports System.Linq

'Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
Private workbook As WorkBook = WorkBook.Load("test.xlsx")
Private sheet As WorkSheet = workbook.WorkSheets.First()
'Select cells easily in Excel notation and return the calculated value
Private 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

'''Advanced Operations

'Calculate aggregate values such as Min, Max and Sum
Dim sum As Decimal = sheet("A2:A10").Sum()
'Linq compatible
Dim max As Decimal = sheet("A2:A10").Max(Function(c) c.DecimalValue)
2

VB   C#

Vật mẫu. Xác nhận dữ liệu


7. Xác thực dữ liệu bảng tính

Sử dụng IronXL để xác thực bảng dữ liệu. Mẫu DataValidation sử dụng

/**
Load WorkBook
anchor-load-a-workbook
**/
var workbook = WorkBook.Load(@"Spreadsheets\\GDP.xlsx");
3 để xác thực số điện thoại và sử dụng API C# tiêu chuẩn để xác thực địa chỉ email và ngày tháng

'''
'''Read XLS or XLSX File
'''anchor-read-an-xls-or-xlsx-file
'''*
Imports IronXL
Imports System.Linq

'Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
Private workbook As WorkBook = WorkBook.Load("test.xlsx")
Private sheet As WorkSheet = workbook.WorkSheets.First()
'Select cells easily in Excel notation and return the calculated value
Private 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

'''Advanced Operations

'Calculate aggregate values such as Min, Max and Sum
Dim sum As Decimal = sheet("A2:A10").Sum()
'Linq compatible
Dim max As Decimal = sheet("A2:A10").Max(Function(c) c.DecimalValue)
3

'''
'''Read XLS or XLSX File
'''anchor-read-an-xls-or-xlsx-file
'''*
Imports IronXL
Imports System.Linq

'Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
Private workbook As WorkBook = WorkBook.Load("test.xlsx")
Private sheet As WorkSheet = workbook.WorkSheets.First()
'Select cells easily in Excel notation and return the calculated value
Private 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

'''Advanced Operations

'Calculate aggregate values such as Min, Max and Sum
Dim sum As Decimal = sheet("A2:A10").Sum()
'Linq compatible
Dim max As Decimal = sheet("A2:A10").Max(Function(c) c.DecimalValue)
3_______6_______5

VB   C#

Đoạn mã trên lặp qua từng hàng trong bảng tính và lấy các ô dưới dạng danh sách. Mỗi phương thức xác thực sẽ kiểm tra giá trị của một ô và trả về thông báo lỗi nếu giá trị không hợp lệ

Mã này tạo một trang tính mới, chỉ định tiêu đề và xuất kết quả thông báo lỗi để có nhật ký dữ liệu không hợp lệ

'''
'''Read XLS or XLSX File
'''anchor-read-an-xls-or-xlsx-file
'''*
Imports IronXL
Imports System.Linq

'Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
Private workbook As WorkBook = WorkBook.Load("test.xlsx")
Private sheet As WorkSheet = workbook.WorkSheets.First()
'Select cells easily in Excel notation and return the calculated value
Private 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

'''Advanced Operations

'Calculate aggregate values such as Min, Max and Sum
Dim sum As Decimal = sheet("A2:A10").Sum()
'Linq compatible
Dim max As Decimal = sheet("A2:A10").Max(Function(c) c.DecimalValue)
6

'''
'''Read XLS or XLSX File
'''anchor-read-an-xls-or-xlsx-file
'''*
Imports IronXL
Imports System.Linq

'Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
Private workbook As WorkBook = WorkBook.Load("test.xlsx")
Private sheet As WorkSheet = workbook.WorkSheets.First()
'Select cells easily in Excel notation and return the calculated value
Private 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

'''Advanced Operations

'Calculate aggregate values such as Min, Max and Sum
Dim sum As Decimal = sheet("A2:A10").Sum()
'Linq compatible
Dim max As Decimal = sheet("A2:A10").Max(Function(c) c.DecimalValue)
6
'''
'''Read XLS or XLSX File
'''anchor-read-an-xls-or-xlsx-file
'''*
Imports IronXL
Imports System.Linq

'Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
Private workbook As WorkBook = WorkBook.Load("test.xlsx")
Private sheet As WorkSheet = workbook.WorkSheets.First()
'Select cells easily in Excel notation and return the calculated value
Private 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

'''Advanced Operations

'Calculate aggregate values such as Min, Max and Sum
Dim sum As Decimal = sheet("A2:A10").Sum()
'Linq compatible
Dim max As Decimal = sheet("A2:A10").Max(Function(c) c.DecimalValue)
8

VB   C#


8. Xuất dữ liệu bằng Entity Framework

Sử dụng IronXL để xuất dữ liệu sang cơ sở dữ liệu hoặc chuyển đổi bảng tính Excel sang cơ sở dữ liệu. Mẫu

/**
Load WorkBook
anchor-load-a-workbook
**/
var workbook = WorkBook.Load(@"Spreadsheets\\GDP.xlsx");
4 đọc bảng tính có GDP theo quốc gia rồi xuất dữ liệu đó sang SQLite

Nó sử dụng EntityFramework để xây dựng cơ sở dữ liệu và sau đó xuất dữ liệu theo từng dòng

Thêm các gói NuGet của SQLite Entity Framework

Sử dụng trình đọc dữ liệu excel C#

EntityFramework cho phép bạn tạo một đối tượng mô hình có thể xuất dữ liệu vào cơ sở dữ liệu

'''
'''Read XLS or XLSX File
'''anchor-read-an-xls-or-xlsx-file
'''*
Imports IronXL
Imports System.Linq

'Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
Private workbook As WorkBook = WorkBook.Load("test.xlsx")
Private sheet As WorkSheet = workbook.WorkSheets.First()
'Select cells easily in Excel notation and return the calculated value
Private 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

'''Advanced Operations

'Calculate aggregate values such as Min, Max and Sum
Dim sum As Decimal = sheet("A2:A10").Sum()
'Linq compatible
Dim max As Decimal = sheet("A2:A10").Max(Function(c) c.DecimalValue)
9

'''
'''Read XLS or XLSX File
'''anchor-read-an-xls-or-xlsx-file
'''*
Imports IronXL
Imports System.Linq

'Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
Private workbook As WorkBook = WorkBook.Load("test.xlsx")
Private sheet As WorkSheet = workbook.WorkSheets.First()
'Select cells easily in Excel notation and return the calculated value
Private 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

'''Advanced Operations

'Calculate aggregate values such as Min, Max and Sum
Dim sum As Decimal = sheet("A2:A10").Sum()
'Linq compatible
Dim max As Decimal = sheet("A2:A10").Max(Function(c) c.DecimalValue)
9
Install-Package IronXL.Excel
1

VB   C#

Để sử dụng cơ sở dữ liệu khác, hãy cài đặt gói NuGet tương ứng và tìm gói tương đương với

/**
Load WorkBook
anchor-load-a-workbook
**/
var workbook = WorkBook.Load(@"Spreadsheets\\GDP.xlsx");
5

Install-Package IronXL.Excel
2

Install-Package IronXL.Excel
2
Install-Package IronXL.Excel
4

VB   C#

Tạo một

/**
Load WorkBook
anchor-load-a-workbook
**/
var workbook = WorkBook.Load(@"Spreadsheets\\GDP.xlsx");
6, lặp qua phạm vi để tạo từng bản ghi, sau đó
/**
Load WorkBook
anchor-load-a-workbook
**/
var workbook = WorkBook.Load(@"Spreadsheets\\GDP.xlsx");
7 để chuyển giao dữ liệu vào cơ sở dữ liệu

Install-Package IronXL.Excel
5

Install-Package IronXL.Excel
5
Install-Package IronXL.Excel
7

VB   C#

Vật mẫu. ExcelToDB


9. Thêm công thức vào bảng tính

Đặt công thức của

/**
Load WorkBook
anchor-load-a-workbook
**/
var workbook = WorkBook.Load(@"Spreadsheets\\GDP.xlsx");
8 với thuộc tính
/**
Load WorkBook
anchor-load-a-workbook
**/
var workbook = WorkBook.Load(@"Spreadsheets\\GDP.xlsx");
9

Mã bên dưới lặp qua từng trạng thái và đặt tổng tỷ lệ phần trăm vào cột C

Install-Package IronXL.Excel
8

Install-Package IronXL.Excel
8
 PM > Install-Package IronXL.Excel
0

VB   C#

Vật mẫu. AddFormulaeBộ xử lý


10. Tải dữ liệu từ API xuống bảng tính

Cuộc gọi sau thực hiện cuộc gọi REST với RestClient. Net. Nó tải xuống JSON và chuyển đổi nó thành một "Danh sách" thuộc loại

/**
Load WorkBook
anchor-load-a-workbook
**/
var workbook = WorkBook.Load(@"Spreadsheets\\GDP.xlsx");
0. Sau đó, có thể dễ dàng lặp lại qua từng quốc gia và lưu dữ liệu từ API REST vào bảng tính Excel

 PM > Install-Package IronXL.Excel
1

 PM > Install-Package IronXL.Excel
1____3_______3

VB   C#

Vật mẫu. ApiToExcel

Đây là dữ liệu API JSON trông như thế nào

Sử dụng trình đọc dữ liệu excel C#

Đoạn mã sau lặp qua các quốc gia và đặt Tên, Dân số, Vùng, Mã số và 3 Ngôn ngữ hàng đầu trong bảng tính

 PM > Install-Package IronXL.Excel
4

 PM > Install-Package IronXL.Excel
4_______3_______6

VB   C#


Tài nguyên và tham chiếu đối tượng

Bạn cũng có thể tìm thấy tài liệu về lớp IronXL trong Tham chiếu đối tượng có giá trị lớn

Ngoài ra, còn có các hướng dẫn khác có thể làm sáng tỏ các khía cạnh khác của IronXL. Excel bao gồm Tạo, Mở, Viết, Chỉnh sửa, Lưu và Xuất các tệp XLS, XLSX và CSV mà không cần sử dụng Excel Interop

Tóm lược

sắtXL. Excel chỉ có một mình. NET để đọc nhiều định dạng bảng tính. Nó không yêu cầu cài đặt Microsoft Excel và không phụ thuộc vào Interop


Hướng dẫn Truy cập nhanh

Tải xuống Hướng dẫn này dưới dạng Mã nguồn C#

Mã nguồn C# cho Excel đầy đủ miễn phí cho hướng dẫn này có sẵn để tải xuống dưới dạng tệp dự án Visual Studio 2017 được nén

Tải xuống

Khám phá Hướng dẫn này trên GitHub

Mã nguồn cho dự án này có sẵn trong C# và VB. NET trên GitHub

Sử dụng mã này như một cách dễ dàng để thiết lập và chạy chỉ trong vài phút. Dự án được lưu dưới dạng dự án Microsoft Visual Studio 2017, nhưng tương thích với bất kỳ. NET IDE

Cách đọc tệp Excel trong C# trên GitHub

Xem tham chiếu API

Khám phá Tài liệu tham khảo API cho IronXL, phác thảo chi tiết về tất cả các tính năng, không gian tên, lớp, trường phương thức và enum của IronXL

Xem tham chiếu API

Sử dụng trình đọc dữ liệu excel C#

Christian Findlay

Trưởng nhóm phát triển phần mềm

Christian xây dựng phần mềm cho ngành y tế và lãnh đạo một nhóm. Christian có nhiều năm kinh nghiệm tích hợp với các loại hệ thống. IronXL cho phép Christian nhập và thao tác dữ liệu từ các nguồn khác nhau để tự động hóa các tác vụ lặp đi lặp lại và xác thực dữ liệu đầu vào từ các nguồn của bên thứ 3

Làm cách nào để đọc tệp Excel trong C?

Cách đọc tệp Excel trong C Sharp .
Cài đặt Thư viện IronXL Excel từ NuGet hoặc bản tải xuống DLL
Sử dụng Sổ làm việc. Phương pháp tải để đọc bất kỳ tài liệu XLS, XLSX hoặc CSV nào
Nhận các giá trị ô bằng cú pháp trực quan. tờ[“A11”]. Giá trị thập phân

Chúng ta có thể viết Excel bằng ExcelDataReader không?

ExcelDataReader là để đọc, không phải để viết . Nếu bạn đang tìm lib để viết, bạn có thể bắt đầu tại đây - stackoverflow. com/câu hỏi/151005/…

Làm cách nào để đọc dữ liệu từ trang tính Excel và chèn vào bảng cơ sở dữ liệu bằng C#?

Tạo hàm đọc và chèn File Excel vào cơ sở dữ liệu dưới dạng. .
private void InsertExcelRecords(chuỗi FilePath)
ExcelConn(Đường dẫn tệp);
Truy vấn = chuỗi. .
OleDbCommand Ecom = new OleDbCommand(Query, Econ);
kinh tế. .
Bộ dữ liệu ds=Bộ dữ liệu mới();
OleDbDataAdapter oda = new OleDbDataAdapter(Query, Econ);

Làm cách nào để lấy dữ liệu từ trang tính Excel trong C#?

Truy xuất dữ liệu từ một trang tính excel và trích xuất sang tệp excel mới trong C# .
Bước 1. Tạo một phiên bản sổ làm việc mới và nhận trang tính đầu tiên. .
Bước 2. Tạo một phiên bản sổ làm việc mới và tải tệp excel mẫu. .
Bước 3. Lấy trang tính mà bạn muốn truy xuất và trích xuất dữ liệu từ đó