Cách đọc ghi file excel trong c++

Đọc và ghi tệp Excel trong hướng dẫn C# trình bày cách ghi và đọc từ tệp Excel từ ứng dụng của bạn bằng SDK Speaksheet. Sử dụng mã nguồn C# mẫu để đọc và viết Excel. Tận dụng tối đa hướng dẫn này bằng cách tìm hiểu các mẫu nguồn. Đọc tệp Excel trong C# với sự trợ giúp của mã bên dưới

Ghi vào tệp Excel từ ứng dụng của bạn

using System;
using System.Collections.Generic;
using System.Text;
using Bytescout.Spreadsheet;
using System.Diagnostics;
using System.IO;

namespace HelloWorld
{
   class Program
   {
       static void Main(string[] args)
       {
           // Create new Spreadsheet
           Spreadsheet document = new Spreadsheet();

           // add new worksheet
           Worksheet Sheet = document.Workbook.Worksheets.Add("FormulaDemo");           

           // headers to indicate purpose of the column
           Sheet.Cell("A1").Value = "Formula (as text)";
           // set A column width
           Sheet.Columns[0].Width = 250;

           Sheet.Cell("B1").Value = "Formula (calculated)";
           // set B column width
           Sheet.Columns[1].Width = 250;


           // write formula as text 
           Sheet.Cell("A2").Value = "7*3+2";
           // write formula as formula
           Sheet.Cell("B2").Value = "=7*3+2";

           // delete output file if exists already
           if (File.Exists("Output.xls")){
               File.Delete("Output.xls");
           }

           // Save document
           document.SaveAs("Output.xls");

           // Close Spreadsheet
           document.Close();

           // open generated XLS document in default program
           Process.Start("Output.xls");

       }

   }
}

Cách đọc tệp excel trong ứng dụng bảng điều khiển C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Bytescout.Spreadsheet;
using System.IO;

namespace HelloWorld
{
   class Program
   {
       static void Main(string[] args)
       {
           // Create new Spreadsheet
           Spreadsheet document = new Spreadsheet();
document.LoadFromFile("Sample.xls");

           // Get worksheet by name
           Worksheet worksheet = document.Workbook.Worksheets.ByName("Sheet1");

           // Check dates
           for (int i = 0; i < 4; i++)
           {
               // Set current cell
               Cell currentCell = worksheet.Cell(i, 0);

               DateTime date = currentCell.ValueAsDateTime;

               // Write Date
               Console.WriteLine("{0}", date.ToShortDateString());
           }

           // Close document
           document.Close();

           // Write message
           Console.Write("Press any key to continue...");

           // Wait user input
           Console.ReadKey();

       }
   }
}

Microsoft Excel là một phần mềm mạnh mẽ với rất nhiều tính năng và chủ yếu được sử dụng để phân tích dữ liệu. Nhiều lần khách hàng của bạn yêu cầu bạn đọc hoặc viết vào tệp Excel. Trong những ngày trước, chúng tôi thường làm điều đó bằng cách sử dụng một đối tượng interop, nhưng nó không được quản lý và giết chết hiệu suất ứng dụng của bạn

ByteScout Spreadsheet SDK là một thư viện hiệu quả sử dụng công cụ tùy chỉnh riêng để dễ dàng đọc hoặc ghi các tệp Excel. Trước đây, thao tác với các tệp Microsoft Excel trong các ứng dụng web hoặc windows thực sự là một công việc tẻ nhạt. Nhưng SDK bảng tính ByteScout giúp cuộc sống của chúng tôi dễ dàng hơn nhiều. Nó đơn giản hóa quá trình đọc và ghi vào các tệp excel và ẩn đi những phức tạp liên quan mà không cần phải xử lý Interop. Thậm chí không cần cài đặt Excel khi chúng ta cần viết hoặc đọc các tệp Excel bằng thư viện này

Vì vậy, bài viết này sẽ trình bày cách tiến hành với ByteScout Spreadsheet SDK trong C#

Ghi chú. Trước khi chúng tôi bắt đầu, vui lòng tải xuống và cài đặt chính xác ByteScout Suites và SDK

Vậy chúng ta đang đợi ai?

Các bước đọc dữ liệu từ file Excel bằng c#

Bước 1. Tạo một dự án ứng dụng bảng điều khiển C# mới

Read Write Excel File

Bước 2. Sau đó thêm Bytescout. bảng tính. dll làm tài liệu tham khảo cho dự án của bạn

Read Excel File

Bước 3. Bao gồm các không gian tên sau trong Chương trình. tập tin cs

using System;

using Bytescout.Spreadsheet;

Bước 4. Thêm đoạn mã sau để đọc dữ liệu từ tệp Excel

                class Program
			    {
				   static void Main(string[] args)
				   {
					   // Create new Spreadsheet
					   Spreadsheet document = new Spreadsheet();
					   document.LoadFromFile("Sample.xls");
			 
					   // Get worksheet by name
					   Worksheet worksheet = document.Workbook.Worksheets.ByName("Sheet1");
			 
					   // Check dates
					   for (int i = 0; i < 4; i++)
					   {
						   // Set current cell
						   Cell currentCell = worksheet.Cell(i, 0);
			 
						   DateTime date = currentCell.ValueAsDateTime;
			 
						   // Write Date
						   Console.WriteLine("{0}", date.ToShortDateString());
					   }
			 
					   // Close document
					   document.Close();
			 
					   // Write message
					   Console.Write("Press any key to continue...");
			 
					   // Wait user input
					   Console.ReadKey();
			 
				   }
			   }

Và đây là đầu ra

Read Excel C#

Tôi nghĩ không cần giải thích đoạn mã trên vì chúng ta chỉ cần tạo một thể hiện của đối tượng Bảng tính và tải tệp excel vật lý (nằm trong thư mục bin\Debug) vào đó và đọc nội dung của trang tính bằng cách chỉ định tên của nó. Chà, mã mẫu ở trên bằng C#, nhưng bạn có thể dễ dàng đạt được điều tương tự trong VB. mạng cũng

Các bước ghi dữ liệu vào file Excel bằng c#

Hãy xem từng bước này

Bước 1. Tạo một dự án ứng dụng bảng điều khiển C# mới

Read Write From Excel

Bước 2. Sau đó thêm Bytescout. bảng tính. dll làm tài liệu tham khảo cho dự án của bạn

Bước 3. Bao gồm các không gian tên sau trong Chương trình. tập tin cs

using Bytescout.Spreadsheet;
using System.Diagnostics;
using System.IO;

          		
				class Program
			    {
				   static void Main(string[] args)
				   {
					   // Create new Spreadsheet
					   Spreadsheet document = new Spreadsheet();
			 
					   // add new worksheet
					   Worksheet Sheet = document.Workbook.Worksheets.Add("FormulaDemo");           
			 
					   // headers to indicate purpose of the column
					   Sheet.Cell("A1").Value = "Formula (as text)";
					   // set A column width
					   Sheet.Columns[0].Width = 250;
			 
					   Sheet.Cell("B1").Value = "Formula (calculated)";
					   // set B column width
					   Sheet.Columns[1].Width = 250;
			 
					   // write formula as text 
					   Sheet.Cell("A2").Value = "7*3+2";
					   // write formula as formula
					   Sheet.Cell("B2").Value = "=7*3+2";
			 
					   // delete output file if exists already
					   if (File.Exists("Output.xls")){
						   File.Delete("Output.xls");
					   }
			 
					   // Save document
					   document.SaveAs("Output.xls");
			 
					   // Close Spreadsheet
					   document.Close();
			 
					   // open generated XLS document in default program
					   Process.Start("Output.xls");
				   }
			   }

Đầu ra như sau

Write Excel C#

Hãy hiểu đoạn mã trên từng bước

Đầu tiên, chúng ta tạo một thể hiện của đối tượng Bảng tính

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Bytescout.Spreadsheet;
using System.IO;

namespace HelloWorld
{
   class Program
   {
       static void Main(string[] args)
       {
           // Create new Spreadsheet
           Spreadsheet document = new Spreadsheet();
document.LoadFromFile("Sample.xls");

           // Get worksheet by name
           Worksheet worksheet = document.Workbook.Worksheets.ByName("Sheet1");

           // Check dates
           for (int i = 0; i &amp;amp;amp;amp;lt; 4; i++)
           {
               // Set current cell
               Cell currentCell = worksheet.Cell(i, 0);

               DateTime date = currentCell.ValueAsDateTime;

               // Write Date
               Console.WriteLine("{0}", date.ToShortDateString());
           }

           // Close document
           document.Close();

           // Write message
           Console.Write("Press any key to continue...");

           // Wait user input
           Console.ReadKey();

       }
   }
}
0

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Bytescout.Spreadsheet;
using System.IO;

namespace HelloWorld
{
   class Program
   {
       static void Main(string[] args)
       {
           // Create new Spreadsheet
           Spreadsheet document = new Spreadsheet();
document.LoadFromFile("Sample.xls");

           // Get worksheet by name
           Worksheet worksheet = document.Workbook.Worksheets.ByName("Sheet1");

           // Check dates
           for (int i = 0; i &amp;amp;amp;amp;lt; 4; i++)
           {
               // Set current cell
               Cell currentCell = worksheet.Cell(i, 0);

               DateTime date = currentCell.ValueAsDateTime;

               // Write Date
               Console.WriteLine("{0}", date.ToShortDateString());
           }

           // Close document
           document.Close();

           // Write message
           Console.Write("Press any key to continue...");

           // Wait user input
           Console.ReadKey();

       }
   }
}
1

Và sau đó, chúng tôi thêm trang tính mới vào đối tượng Bảng tính mới được tạo bằng mã sau

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Bytescout.Spreadsheet;
using System.IO;

namespace HelloWorld
{
   class Program
   {
       static void Main(string[] args)
       {
           // Create new Spreadsheet
           Spreadsheet document = new Spreadsheet();
document.LoadFromFile("Sample.xls");

           // Get worksheet by name
           Worksheet worksheet = document.Workbook.Worksheets.ByName("Sheet1");

           // Check dates
           for (int i = 0; i &amp;amp;amp;amp;lt; 4; i++)
           {
               // Set current cell
               Cell currentCell = worksheet.Cell(i, 0);

               DateTime date = currentCell.ValueAsDateTime;

               // Write Date
               Console.WriteLine("{0}", date.ToShortDateString());
           }

           // Close document
           document.Close();

           // Write message
           Console.Write("Press any key to continue...");

           // Wait user input
           Console.ReadKey();

       }
   }
}
2

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Bytescout.Spreadsheet;
using System.IO;

namespace HelloWorld
{
   class Program
   {
       static void Main(string[] args)
       {
           // Create new Spreadsheet
           Spreadsheet document = new Spreadsheet();
document.LoadFromFile("Sample.xls");

           // Get worksheet by name
           Worksheet worksheet = document.Workbook.Worksheets.ByName("Sheet1");

           // Check dates
           for (int i = 0; i &amp;amp;amp;amp;lt; 4; i++)
           {
               // Set current cell
               Cell currentCell = worksheet.Cell(i, 0);

               DateTime date = currentCell.ValueAsDateTime;

               // Write Date
               Console.WriteLine("{0}", date.ToShortDateString());
           }

           // Close document
           document.Close();

           // Write message
           Console.Write("Press any key to continue...");

           // Wait user input
           Console.ReadKey();

       }
   }
}
3

  • Sổ làm việc – Đại diện cho một sổ làm việc trong đối tượng Bảng tính
  • Worksheet – Một thành viên của tập hợp Worksheets trong đối tượng Workbook

Và sau đó chúng tôi đặt tên tiêu đề hai cột và chiều rộng của nó, sau đó viết công thức vào các cột được tôn trọng

________số 8_______
  • Và sau đó chúng tôi kiểm tra xem tên tệp giống như vậy đã tồn tại trong
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Diagnostics;
    using Bytescout.Spreadsheet;
    using System.IO;
    
    namespace HelloWorld
    {
       class Program
       {
           static void Main(string[] args)
           {
               // Create new Spreadsheet
               Spreadsheet document = new Spreadsheet();
    document.LoadFromFile("Sample.xls");
    
               // Get worksheet by name
               Worksheet worksheet = document.Workbook.Worksheets.ByName("Sheet1");
    
               // Check dates
               for (int i = 0; i &amp;amp;amp;amp;lt; 4; i++)
               {
                   // Set current cell
                   Cell currentCell = worksheet.Cell(i, 0);
    
                   DateTime date = currentCell.ValueAsDateTime;
    
                   // Write Date
                   Console.WriteLine("{0}", date.ToShortDateString());
               }
    
               // Close document
               document.Close();
    
               // Write message
               Console.Write("Press any key to continue...");
    
               // Wait user input
               Console.ReadKey();
    
           }
       }
    }
    4 của ứng dụng chưa. Nếu tồn tại thì trước tiên hãy xóa nó

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Bytescout.Spreadsheet;
using System.IO;

namespace HelloWorld
{
   class Program
   {
       static void Main(string[] args)
       {
           // Create new Spreadsheet
           Spreadsheet document = new Spreadsheet();
document.LoadFromFile("Sample.xls");

           // Get worksheet by name
           Worksheet worksheet = document.Workbook.Worksheets.ByName("Sheet1");

           // Check dates
           for (int i = 0; i &amp;amp;amp;amp;lt; 4; i++)
           {
               // Set current cell
               Cell currentCell = worksheet.Cell(i, 0);

               DateTime date = currentCell.ValueAsDateTime;

               // Write Date
               Console.WriteLine("{0}", date.ToShortDateString());
           }

           // Close document
           document.Close();

           // Write message
           Console.Write("Press any key to continue...");

           // Wait user input
           Console.ReadKey();

       }
   }
}
5

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Bytescout.Spreadsheet;
using System.IO;

namespace HelloWorld
{
   class Program
   {
       static void Main(string[] args)
       {
           // Create new Spreadsheet
           Spreadsheet document = new Spreadsheet();
document.LoadFromFile("Sample.xls");

           // Get worksheet by name
           Worksheet worksheet = document.Workbook.Worksheets.ByName("Sheet1");

           // Check dates
           for (int i = 0; i &amp;amp;amp;amp;lt; 4; i++)
           {
               // Set current cell
               Cell currentCell = worksheet.Cell(i, 0);

               DateTime date = currentCell.ValueAsDateTime;

               // Write Date
               Console.WriteLine("{0}", date.ToShortDateString());
           }

           // Close document
           document.Close();

           // Write message
           Console.Write("Press any key to continue...");

           // Wait user input
           Console.ReadKey();

       }
   }
}
6

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Bytescout.Spreadsheet;
using System.IO;

namespace HelloWorld
{
   class Program
   {
       static void Main(string[] args)
       {
           // Create new Spreadsheet
           Spreadsheet document = new Spreadsheet();
document.LoadFromFile("Sample.xls");

           // Get worksheet by name
           Worksheet worksheet = document.Workbook.Worksheets.ByName("Sheet1");

           // Check dates
           for (int i = 0; i &amp;amp;amp;amp;lt; 4; i++)
           {
               // Set current cell
               Cell currentCell = worksheet.Cell(i, 0);

               DateTime date = currentCell.ValueAsDateTime;

               // Write Date
               Console.WriteLine("{0}", date.ToShortDateString());
           }

           // Close document
           document.Close();

           // Write message
           Console.Write("Press any key to continue...");

           // Wait user input
           Console.ReadKey();

       }
   }
}
7

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Bytescout.Spreadsheet;
using System.IO;

namespace HelloWorld
{
   class Program
   {
       static void Main(string[] args)
       {
           // Create new Spreadsheet
           Spreadsheet document = new Spreadsheet();
document.LoadFromFile("Sample.xls");

           // Get worksheet by name
           Worksheet worksheet = document.Workbook.Worksheets.ByName("Sheet1");

           // Check dates
           for (int i = 0; i &amp;amp;amp;amp;lt; 4; i++)
           {
               // Set current cell
               Cell currentCell = worksheet.Cell(i, 0);

               DateTime date = currentCell.ValueAsDateTime;

               // Write Date
               Console.WriteLine("{0}", date.ToShortDateString());
           }

           // Close document
           document.Close();

           // Write message
           Console.Write("Press any key to continue...");

           // Wait user input
           Console.ReadKey();

       }
   }
}
8

  • Và sau đó chúng tôi lưu tệp mới được tạo

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Bytescout.Spreadsheet;
using System.IO;

namespace HelloWorld
{
   class Program
   {
       static void Main(string[] args)
       {
           // Create new Spreadsheet
           Spreadsheet document = new Spreadsheet();
document.LoadFromFile("Sample.xls");

           // Get worksheet by name
           Worksheet worksheet = document.Workbook.Worksheets.ByName("Sheet1");

           // Check dates
           for (int i = 0; i &amp;amp;amp;amp;lt; 4; i++)
           {
               // Set current cell
               Cell currentCell = worksheet.Cell(i, 0);

               DateTime date = currentCell.ValueAsDateTime;

               // Write Date
               Console.WriteLine("{0}", date.ToShortDateString());
           }

           // Close document
           document.Close();

           // Write message
           Console.Write("Press any key to continue...");

           // Wait user input
           Console.ReadKey();

       }
   }
}
9

                class Program
			    {
				   static void Main(string[] args)
				   {
					   // Create new Spreadsheet
					   Spreadsheet document = new Spreadsheet();
					   document.LoadFromFile("Sample.xls");
			 
					   // Get worksheet by name
					   Worksheet worksheet = document.Workbook.Worksheets.ByName("Sheet1");
			 
					   // Check dates
					   for (int i = 0; i < 4; i++)
					   {
						   // Set current cell
						   Cell currentCell = worksheet.Cell(i, 0);
			 
						   DateTime date = currentCell.ValueAsDateTime;
			 
						   // Write Date
						   Console.WriteLine("{0}", date.ToShortDateString());
					   }
			 
					   // Close document
					   document.Close();
			 
					   // Write message
					   Console.Write("Press any key to continue...");
			 
					   // Wait user input
					   Console.ReadKey();
			 
				   }
			   }
0

Trong bài viết này, chúng tôi đã cung cấp giải pháp về cách đọc tệp Excel và ghi tệp đó vào tệp Excel bằng cách sử dụng ByteScout Spreadsheet SDK trong C#. Để làm được điều đó, chúng tôi đã tạo hai ứng dụng bảng điều khiển mẫu và viết mã từng bước. Cách tốt nhất để đánh giá SDK và tìm hiểu mã là gỡ lỗi và thực hiện các thay đổi nhỏ để xem điều gì sẽ xảy ra 🙂

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

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

Làm cách nào để chỉnh sửa tệp Excel trong C?

Các bước chỉnh sửa tệp Excel trong C++ .
Thêm Aspose. tế bào. Cpp với công cụ Trình quản lý gói NuGet
Thêm một tham chiếu đến Aspose. không gian tên ô
Tạo thể hiện của đối tượng Workbook để tải tệp Excel để chỉnh sửa
Truy cập các ô A1 và C1 bên trong trang tính và đặt dữ liệu và chuỗi công thức tương ứng
Tính công thức cho Workbook

Làm cách nào để viết văn bản trong trang tính Excel bằng C#?

Định dạng văn bản trong Excel bằng C# .
Bước 1. Tạo một tài liệu excel và nhận bảng tính đầu tiên của nó
Bước 2. Tạo phông chữ và gửi định dạng cho từng phông chữ
Bước 3. Đặt phông chữ cho phạm vi ô được chỉ định
Bước 4. Lưu tài liệu vào tập tin
Mã đầy đủ

Làm cách nào để ghi dữ liệu vào tệp XLSX trong C#?

Ghi dữ liệu vào tệp Excel XLSX bằng C# .
Mở tệp Excel trong đối tượng FileStream
Tạo một thể hiện của Workbook và khởi tạo nó với đối tượng FileStream
Truy cập các trang tính và ô bằng cách sử dụng các lớp Worksheet và Cell tương ứng
Lưu sổ làm việc dưới dạng Excel. tập tin xlsx