翻譯|使用教程|編輯:吉煒煒|2024-12-13 10:24:13.963|閱讀 143 次
概述:本文將探討如何使用 Spire.XLS for .NET 在 C# 程序中導(dǎo)入 Excel 數(shù)據(jù)到數(shù)據(jù)庫以及導(dǎo)出數(shù)據(jù)庫到 Excel 文件,實(shí)現(xiàn)數(shù)據(jù)在 Excel 和數(shù)據(jù)庫之間無縫流轉(zhuǎn)。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
在現(xiàn)代企業(yè)環(huán)境中,確保 Excel 文件與數(shù)據(jù)庫之間順暢的數(shù)據(jù)交換對于優(yōu)化工作流程和提升數(shù)據(jù)分析的效率至關(guān)重要。通過高效的數(shù)據(jù)導(dǎo)入導(dǎo)出操作,企業(yè)能夠充分利用數(shù)據(jù)庫的強(qiáng)大處理能力和 Excel 的靈活性,實(shí)現(xiàn)更精準(zhǔn)的業(yè)務(wù)決策支持。
本文將探討如何使用 Spire.XLS for .NET 在 C# 程序中導(dǎo)入 Excel 數(shù)據(jù)到數(shù)據(jù)庫以及導(dǎo)出數(shù)據(jù)庫到 Excel 文件,實(shí)現(xiàn)數(shù)據(jù)在 Excel 和數(shù)據(jù)庫之間無縫流轉(zhuǎn)。
安裝 Spire.XLS for .NET
首先,您需要將 Spire.XLS for .NET 包含的 DLL 文件作為引用添加到您的 .NET 項(xiàng)目中。
PM> Install-Package Spire.XLS
用 C# 將 Excel 數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫
借助 Spire.XLS for .NET,我們可以使用 Workbook.LoadFromFile() 方法載入 Excel 文件,并通過 CellRange.Value 屬性訪問單元格數(shù)據(jù)。然后,可以利用相關(guān)的數(shù)據(jù)庫操作模塊(如適用于 SQLite 的 System.Data.SQLite 模塊)將數(shù)據(jù)寫入數(shù)據(jù)庫,從而實(shí)現(xiàn)將 Excel 文件中的數(shù)據(jù)無縫導(dǎo)入到數(shù)據(jù)庫的功能。
以下步驟和代碼以 SQLite 數(shù)據(jù)庫為例,展示如何使用 C# 將 Excel 數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫:
using System.Data.SQLite; using Spire.Xls; namespace ExcelToSQLite { class Program { static void Main(string[] args) { // Excel文件的路徑 string excelFilePath = "示例.xlsx"; // SQLite數(shù)據(jù)庫的文件路徑 string sqliteFilePath = "output/Excel導(dǎo)入數(shù)據(jù)庫.db"; // 打開Excel文件 Workbook workbook = new Workbook(); workbook.LoadFromFile(excelFilePath); // 如果數(shù)據(jù)庫文件不存在,則創(chuàng)建一個(gè)新的數(shù)據(jù)庫文件 if (!File.Exists(sqliteFilePath)) { SQLiteConnection.CreateFile(sqliteFilePath); Console.WriteLine("新的SQLite數(shù)據(jù)庫文件已創(chuàng)建。"); } // 創(chuàng)建SQLite連接 using (SQLiteConnection connection = new SQLiteConnection($"Data Source={sqliteFilePath};Version=3;")) { connection.Open(); // 遍歷每個(gè)工作表 foreach (Worksheet sheet in workbook.Worksheets) { string tableName = sheet.Name; // 獲取第一行作為列名 var columns = sheet.Rows[0].CellList; string createTableQuery = $"CREATE TABLE IF NOT EXISTS [{tableName}] ("; foreach (var column in columns) { createTableQuery += $"[{column.Value}] TEXT,"; } createTableQuery = createTableQuery.TrimEnd(',') + ");"; // 創(chuàng)建表 using (SQLiteCommand createTableCommand = new SQLiteCommand(createTableQuery, connection)) { createTableCommand.ExecuteNonQuery(); } // 插入數(shù)據(jù) for (int i = 1; i < sheet.Rows.Length; i++) // 跳過第一行 { var row = sheet.Rows[i]; string insertQuery = $"INSERT INTO [{tableName}] VALUES ("; foreach (var cell in row.CellList) { insertQuery += $"'{cell.Value?.Replace("'", "''")}',"; // 防止SQL注入 } insertQuery = insertQuery.TrimEnd(',') + ");"; using (SQLiteCommand insertCommand = new SQLiteCommand(insertQuery, connection)) { insertCommand.ExecuteNonQuery(); } } } connection.Close(); workbook.Dispose(); } Console.WriteLine("Excel數(shù)據(jù)已成功寫入新的SQLite數(shù)據(jù)庫!"); } } }
用 C# 將數(shù)據(jù)從數(shù)據(jù)庫導(dǎo)出到 Excel 文件
同樣,我們可以利用數(shù)據(jù)庫操作模塊從數(shù)據(jù)庫中讀取數(shù)據(jù),然后創(chuàng)建 Workbook 對象并使用 CellRange.Value 屬性將讀取的數(shù)據(jù)寫入 Excel 工作簿中,從而實(shí)現(xiàn)從數(shù)據(jù)庫導(dǎo)出數(shù)據(jù)到 Excel 文件的功能。
以下步驟和代碼以 SQLite 數(shù)據(jù)庫為例,展示了如何將數(shù)據(jù)庫中的數(shù)據(jù)導(dǎo)出到 Excel 文件:
using System.Data; using System.Data.SQLite; using Spire.Xls; namespace SQLiteToExcel { class Program { static void Main(string[] args) { // SQLite數(shù)據(jù)庫的文件路徑 string sqliteFilePath = "示例.db"; // Excel文件的保存路徑 string excelFilePath = "output/數(shù)據(jù)庫導(dǎo)出到Excel.xlsx"; // 創(chuàng)建一個(gè)新的Workbook實(shí)例 Workbook workbook = new Workbook(); // 清空默認(rèn)的工作表 workbook.Worksheets.Clear(); // 創(chuàng)建SQLite連接 using (SQLiteConnection connection = new SQLiteConnection($"Data Source={sqliteFilePath};Version=3;")) { connection.Open(); // 獲取所有表的名稱 DataTable tables = connection.GetSchema("Tables"); // 遍歷每個(gè)表 foreach (DataRow tableRow in tables.Rows) { string tableName = tableRow["TABLE_NAME"].ToString(); // 創(chuàng)建新的工作表 Worksheet sheet = workbook.Worksheets.Add(tableName); // 獲取表數(shù)據(jù) string selectQuery = $"SELECT * FROM [{tableName}]"; using (SQLiteCommand command = new SQLiteCommand(selectQuery, connection)) { using (SQLiteDataReader reader = command.ExecuteReader()) { // 獲取列名并寫入到第一行 for (int col = 0; col < reader.FieldCount; col++) { sheet.Range[1, col + 1].Value = reader.GetName(col); } // 設(shè)置表頭字體樣式 sheet.Rows[0].Style.Font.IsBold = true; sheet.Rows[0].Style.Font.Size = 12; // 插入數(shù)據(jù)行 int rowIndex = 2; while (reader.Read()) { for (int col = 0; col < reader.FieldCount; col++) { sheet.Range[rowIndex, col + 1].Value = reader.GetValue(col).ToString(); // 自動(dòng)調(diào)整列寬 sheet.AutoFitColumn(col + 1); } // 設(shè)置數(shù)據(jù)行的字體樣式 sheet.Rows[rowIndex - 1].Style.Font.Size = 11; rowIndex++; } } } } connection.Close(); } // 保存Excel文件 workbook.SaveToFile(excelFilePath); workbook.Dispose(); Console.WriteLine("數(shù)據(jù)已成功導(dǎo)出到Excel文件!"); } } }
歡迎下載|體驗(yàn)更多E-iceblue產(chǎn)品
獲取更多信息請咨詢慧都在線客服 ;技術(shù)交流Q群(767755948)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)