翻譯|使用教程|編輯:胡濤|2022-05-27 13:34:25.763|閱讀 231 次
概述:在本文中,您將學習如何從 CSV 填充 DataTable,或者換句話說,如何在 C# 和 VB.NET中使用Spire.XLS for .NET將 CSV 轉換為 DataTable 。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
DataTable 表示內存中的關系數據表。它可以從 Microsoft SQL Server 之類的數據源或 CSV 或 Excel 之類的文件中填充。在本文中,您將學習如何從 CSV 填充 DataTable,或者換句話說,如何在 C# 和 VB.NET中使用Spire.XLS for .NET將 CSV 轉換為 DataTable 。
首先,您需要添加 Spire.XLS for .NET 包中包含的 DLL 文件作為 .NET 項目中的引用。DLL 文件可以從此鏈接下載或通過NuGet安裝。
PM> Install-Package Spire.XLS
以下是將 CSV 轉換為 DataTable 的主要步驟:
【C#】
using Spire.Xls; using System; using System.Data; using System.Windows.Forms; namespace ConvertCsvToExcel { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //Create a Workbook instance Workbook workbook = new Workbook(); //Load a CSV file workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\Input.csv", ","); //Get the first worksheet Worksheet worksheet = workbook.Worksheets[0]; //Export data from the worksheet to a DataTable DataTable dt = worksheet.ExportDataTable(); //This overload enables you to specify the range to be exported along with whether to export column names and calculated values of formulas //DataTable dt = worksheet.ExportDataTable(worksheet.Range["A1:C10"], true, true); //Show the DataTable in a DataGridView control (optional) dataGridView1.DataSource = dt; } } }
【VB.NET】
Imports Spire.Xls Imports System Imports System.Data Imports System.Windows.Forms Namespace ConvertCsvToExcel Public Partial Class Form1 Inherits Form Public Sub New() InitializeComponent() End Sub Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) 'Create a Workbook instance Dim workbook As Workbook = New Workbook() 'Load a CSV file workbook.LoadFromFile("Input.csv", ",") 'Get the first worksheet Dim worksheet As Worksheet = workbook.Worksheets(0) 'Export data from the worksheet to a DataTable Dim dt As DataTable = worksheet.ExportDataTable() 'This overload enables you to specify the range to be exported along with whether to export column names and calculated values of formulas 'DataTable dt = worksheet.ExportDataTable(worksheet.Range["A1:C10"], true, true); 'Show the DataTable in a DataGridView control (optional) dataGridView1.DataSource = dt End Sub End Class End Namespace
歡迎下載|體驗更多E-iceblue產品
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn