參考網址
https://code.google.com/p/linqtoexcel/
影片教學
https://www.youtube.com/watch?v=t3BEUP0OTFM
下面是MVC C# example
public class MetaDataService
{
/// <summary>
/// 查詢該商品是否有更新的資料
/// </summary>
/// <param name="ItemID">查詢商品ID</param>
/// <returns>回傳更新的資料</returns>
public MetaDataGroup GetMetaDatabyID(int ItemID)
{
string strItemID = ItemID.ToString();
var getExcelDataList = ReadMetaExcel();
var MetaData = getExcelDataList.Where(x => x.ItemID == strItemID).FirstOrDefault();
return MetaData;
}
/// <summary>
/// 利用LinqToExcel_x64_1.7.1讀取Excel檔案
/// </summary>
/// <returns>將Excel轉成List<MetaDataGroup>後回傳</returns>
public List<MetaDataGroup> ReadMetaExcel()
{
List<MetaDataGroup> MetaData = new List<MetaDataGroup>();
string path = AppDomain.CurrentDomain.BaseDirectory + "Configurations\\SEO填表.xlsx";
if (File.Exists(path))
{
ExcelQueryFactory ExcelFile = new ExcelQueryFactory(path);
// 去掉中文標題那一行
var MetaDatafromExcel = ExcelFile.Worksheet<MetaDataGroup>("MetaData").Skip(1).ToList();
bool UpdateMetaData = MetaDatafromExcel.Any();
if (UpdateMetaData)
{
int default_itemID = 0;
foreach (var MetaIndex in MetaDatafromExcel)
{
// 判斷賣場編號是否有填入
if (!string.IsNullOrWhiteSpace(MetaIndex.ItemID))
{
// 判斷 ItemID 是否能夠正常轉換 Int
if (int.TryParse(MetaIndex.ItemID, out default_itemID))
{
MetaData.Add(MetaIndex);
}
}
}
}
}
return MetaData;
}
}
沒有留言:
張貼留言