Linq To Excel
//自己可自行加要過濾的條件,這邊只是示範
var linq = from item in excel.Worksheet(sheetName)
select item;
...</pre>
//這邊會取使用Sheet1的工作表內容去做查詢動作
var linq = from item in excel.Worksheet()
select item;
...</pre>
var linq = from item in excel.Worksheet<Blogger>(sheetName)
where item.Sex==SexType.Boy
select item;
...</pre>
namespace ConsoleApplication1 { class Blogger { public int ID { get; set; } public String FirstName { get; set; } public String LastName { get; set; } public SexType Sex { get; set; } public int Age { get; set; } public String Blog { get; set; }
public override string ToString()
{
return string.Join(",", new string[] { ID.ToString(), FirstName, LastName, Sex.ToString(), Age.ToString(), Blog });
}
}
}
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { const string EXCEL_FILE = “Data.xls”; const string FIRST_SHEET = “BlogData1”; const string SECOND_SHEET = “BlogData2”;
var excel = new ExcelQueryFactory(EXCEL_FILE);
System.Console.WriteLine("Excel File: {0}", excel.FileName);
System.Console.WriteLine();
System.Console.WriteLine("WorksheetNames...");
var workSheetNames = excel.GetWorksheetNames();
foreach (var item in workSheetNames)
{
System.Console.WriteLine(item.ToString());
}
System.Console.WriteLine();
System.Console.WriteLine("BlogData's Columns...");
var columnNames = excel.GetColumnNames(FIRST_SHEET);
foreach (var item in columnNames)
{
System.Console.WriteLine(item.ToString());
}
System.Console.WriteLine();
System.Console.WriteLine("BlogData1 With ExcelQueryFactory.Worksheet...");
excel.AddMapping<Blogger>(item => item.FirstName, "First Name");
excel.AddMapping<Blogger>(item => item.LastName, "Last Name");
excel.AddTransformation<Blogger>(item => item.Sex, item => (item == "Boy") ? SexType.Boy : SexType.Girl);
foreach (var item in excel.Worksheet<Blogger>(FIRST_SHEET))
{
System.Console.WriteLine(item.ToString());
}
System.Console.WriteLine();
System.Console.WriteLine("BlogData2 With ExcelQueryFactory.Worksheet...");
foreach (var item in excel.Worksheet<Blogger>(SECOND_SHEET))
{
System.Console.WriteLine(item.ToString());
}
System.Console.WriteLine();
System.Console.WriteLine("BlogData2 With ExcelQueryFactory.WorksheetRange...");
foreach (var item in excel.WorksheetRange<Blogger>("B2", "G3", SECOND_SHEET))
{
System.Console.WriteLine(item.ToString());
}
}
}
}