Linq To Excel Provider
using System.Linq; using System.ComponentModel;
[ExcelSheet(Name=“Sheet1”)] public class Person: INotifyPropertyChanged {
private double _id;
private string _firstname;
private string _lastname;
private DateTime _birthdate;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) {
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
[ExcelColumn(Name="ID", Storage="_id")]
public double ID
{
get { return _id;}
set {
_id = value;
SendPropertyChanged("ID");
}
}
[ExcelColumn(Name="FirstName", Storage="_firstname")]
public string FirstName
{
get { return _firstname;}
set {
_firstname = value;
SendPropertyChanged("FirstName");
}
}
[ExcelColumn(Name="LastName", Storage="_lastname")]
public string LastName
{
get { return _lastname;}
set {
_lastname = value;
SendPropertyChanged("LastName");
}
}
[ExcelColumn(Name="BirthDate", Storage="_birthdate")]
public DateTime BirthDate
{
get { return _birthdate;}
set {
_birthdate = value;
SendPropertyChanged("BirthDate");
}
}
}
namespace ConsoleApplication1 { class Program { static void Main(string[] args) {
ExcelProvider provider = ExcelProvider.Create(@"Data.xls");
var linq = from item in provider.GetSheet<Person>()
select item;
Console.WriteLine("現有資料...");
foreach (Person item in linq)
{
Console.WriteLine(item.ToString ());
}
Console.WriteLine();
Console.WriteLine("插入資料...");
Person p = new Person();
p.Id = 10.0;
p.FirstName = "Alex";
p.LastName = "Zander";
p.BirthDate = new DateTime(1980, 4, 4);
provider.GetSheet<Person>().InsertOnSubmit(p);
provider.SubmitChanges();
Console.WriteLine(p.ToString());
Console.WriteLine();
Console.WriteLine("插入後資料...");
foreach (Person item in linq)
{
Console.WriteLine(item.ToString());
}
}
}
[ExcelSheet(Name = "Sheet1")]
public class Person : System.ComponentModel.INotifyPropertyChanged
{
private double id;
private string fName;
private string lName;
private DateTime bDate;
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanged(string propertyName)
{
System.ComponentModel.PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
public Person()
{
id = 0;
}
[ExcelColumn(Name = "ID", Storage = "id")]
public double Id
{
get { return id; }
set { id = value; }
}
[ExcelColumn(Name = "FirstName", Storage = "fName")]
public string FirstName
{
get { return this.fName; }
set
{
fName = value;
SendPropertyChanged("FirstName");
}
}
[ExcelColumn(Name = "LastName", Storage = "lName")]
public string LastName
{
get { return this.lName; }
set
{
lName = value;
SendPropertyChanged("LastName");
}
}
[ExcelColumn(Name = "BirthDate", Storage = "bDate")]
public DateTime BirthDate
{
get { return this.bDate; }
set
{
bDate = value;
SendPropertyChanged("BirthDate");
}
}
public String ToString()
{
return string.Join(",", new string[] { id.ToString(), FirstName, LastName, BirthDate.ToShortDateString() });
}
}
}