大家好,今天跟大家一起學習如何使用LinqToExcel 寫一個批次上傳的功能,因為篇幅有點多,分成兩天來寫XD
Step 1. 下載LinqToExcel
Step 2. 輸入
PM> Install-Package LinqToExcel -Version 1.11.0
Step 3. 建立資料庫
Step 4. 建立Model
public class ExcelModel
{
public string name { get; set; }
public string tel { get; set; }
}
Step 5. 建立連線字串
5-1 Web.config
<connectionStrings>
<add name="connect" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\black\source\repos\ithome2\ithome2\App_Data\Database1.mdf;Integrated Security=True" />
</connectionStrings>
5-2 Controller
string strConnString = ConfigurationManager.ConnectionStrings["connect"].ConnectionString;
Step 6. Controller 寫一個儲存的資料的Function
6-1. 先Using 會使用的class
using System.Collections.Generic;
using System.Configuration;
6-2.再來寫Function
#region
public int Save_Excel(string name, string tel)
{
using (SqlConnection conn = new SqlConnection(strConnString))
{
conn.Open();
SqlCommand scom = new SqlCommand("", conn);
scom.CommandText = @"
insert into [telphone]
(
name,
tel
)
values
(
@name,
@tel
)
";
scom.Parameters.AddWithValue("@name", name);
scom.Parameters.AddWithValue("@tel", tel);
int i = scom.ExecuteNonQuery();
scom.Dispose();
scom.Clone();
return i;
}
}
#endregion
參考網址:https://www.nuget.org/packages/LinqToExcel