LINQ - 實作 LinqToExcel

摘要:LINQ - 實作 LinqToExcel

最近在想做一個轉檔用的程式,主要是將 Excel 讀取後,寫入到資料庫中,當然第一個想法就是用NPOI這強大的套件。但是筆者猶豫了一下,有需要用到那麼強大的套件嘛!? 之後 Google 了一下,沒想到有驚人的收穫,原來大家都推薦一個輕量級的套件 LinqToExcel 了,這套件確實很符合筆者的需求,為什麼呢!? 主要原因只有一個,筆者只對 Excel 做讀的動作,其餘的都不需要,以下就以範例來做呈現...

XLS:






Code:


using LinqToExcel;
using System.IO;
using System.Reflection;

namespace ConsoleApplication1
{
class myEmp
{
public string C_uID { get; set; }
public string C_uName { get; set; }
public string C_uTel { get; set; }
public int C_uSalary { get; set; }
}

class Program
{
static void Main(string[] args)
{
var ExcelFile = new ExcelQueryFactory(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "員工基本資料.xlsx"));
ExcelFile.DatabaseEngine = LinqToExcel.Domain.DatabaseEngine.Ace;

//寫法一
Console.WriteLine("寫法一==================================================");
var sqlExcelData_1 = ExcelFile.Worksheet("員工基本資料一");
foreach (var itemRow in sqlExcelData_1)
{
Console.WriteLine("員工編號:{0}、員工姓名:{1}、連絡電話:{2}、薪資:{3}",
itemRow["C_uID"],
itemRow["C_uName"],
itemRow["C_uTel"],
Convert.ToInt32(itemRow["C_uSalary"])
);
}
Console.WriteLine("========================================================");

//寫法二
Console.WriteLine("寫法二==================================================");
var sqlExcelData_2 = ExcelFile.Worksheet<myEmp>("員工基本資料一").Where(p => p.C_uSalary >= 35000);
foreach (var itemRow in sqlExcelData_2)
{
Console.WriteLine("員工編號:{0}、員工姓名:{1}、連絡電話:{2}、薪資:{3}",
itemRow.C_uID,
itemRow.C_uName,
itemRow.C_uTel,
Convert.ToInt32(itemRow.C_uSalary)
);
}
Console.WriteLine("========================================================");

//寫法三
Console.WriteLine("寫法三==================================================");
ExcelFile.AddMapping<myEmp>(p => p.C_uID, "員工編號");
ExcelFile.AddMapping<myEmp>(p => p.C_uName, "員工姓名");
ExcelFile.AddMapping<myEmp>(p => p.C_uTel, "連絡電話");
ExcelFile.AddMapping<myEmp>(p => p.C_uSalary, "薪資");

var sqlExcelData_3 = ExcelFile.Worksheet<myEmp>("員工基本資料二").Where(p => p.C_uID == "A009");
foreach (var itemRow in sqlExcelData_3)
{
Console.WriteLine("員工編號:{0}、員工姓名:{1}、連絡電話:{2}、薪資:{3}",
itemRow.C_uID,
itemRow.C_uName,
itemRow.C_uTel,
itemRow.C_uSalary
);
}
Console.WriteLine("========================================================");

//寫法四
Console.WriteLine("寫法四==================================================");
ExcelFile.AddMapping<myEmp>(p => p.C_uID, "員工編號");
ExcelFile.AddMapping<myEmp>(p => p.C_uName, "員工姓名");
ExcelFile.AddMapping<myEmp>(p => p.C_uTel, "連絡電話");
ExcelFile.AddMapping<myEmp>(p => p.C_uSalary, "薪資");
var sqlExcelData_4 = ExcelFile.WorksheetRange<myEmp>("B2", "E12", "員工基本資料三").Where(p => p.C_uSalary < 35000);
foreach (myEmp itemRow in sqlExcelData_4)
{
Console.WriteLine("員工編號:{0}、員工姓名:{1}、連絡電話:{2}、薪資:{3}",
itemRow.C_uID,
itemRow.C_uName,
itemRow.C_uTel,
itemRow.C_uSalary
);
}
Console.WriteLine("========================================================");

//寫法五:取得欄位
Console.WriteLine("寫法五==================================================");
var sqlExcelData_5 = ExcelFile.GetColumnNames("員工基本資料二");
foreach (var itemRow in sqlExcelData_5)
{
Console.WriteLine(itemRow.ToString());
}
Console.WriteLine("========================================================");

//寫法六:取得 Excel 中的頁籤
Console.WriteLine("寫法六==================================================");
var sqlExcelData_6 = ExcelFile.GetWorksheetNames();
foreach (var itemRow in sqlExcelData_6)
{
Console.WriteLine(itemRow.ToString());
}
Console.WriteLine("========================================================");

//寫法七:取得 Excel 中的頁籤
Console.WriteLine("寫法七==================================================");
Console.WriteLine(ExcelFile.FileName);
Console.WriteLine("========================================================");

Console.ReadKey();
}
}
}

結果:


參考:
Linq to Excel