最近项目中需要使用Excel取数据。
引用 using Microsoft.Office.Interop.Excel;。需要在项目中引用11.0库类。
public class BillRepository
{
public delegate void ExcelRowsChangedDelegate(object sender, TaoBaoBillEventArsg e);//声明一个委托
public event ExcelRowsChangedDelegate ExcelRowsChanged;//声明一个事件。
/// <summary>
/// 获取支付宝上的订单
/// </summary>
public void getTaobaoBill(string filePath)
{
Bills bills=new Bills();
UInt32 totalRows;
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook wk = excel.Workbooks.Open(filePath);
Worksheet ws = wk.Worksheets[1];
string DateCell = ws.Rows[3].Cells[1].Value;
Regex reg = new Regex(@"\[(.+?)\]");
var regColl = reg.Matches(DateCell);
for (int i = 0; i < regColl.Count; i++)
{
string time = regColl[i].ToString().Replace("年", "-").Replace("月", "-").Replace("日", "");
switch (i)
{
case 0:
bills.firstDate = DateTime.Parse(time.Substring(1,time.Length-2));
break;
default:
bills.lastDate = DateTime.Parse(time.Substring(1, time.Length - 2));
break;
}
}
Int32 firstRowINdex = 6;
Int32 lastRowINdex = ws.UsedRange.Rows.Count - 4;
totalRows = (UInt32)(lastRowINdex - firstRowINdex);
if (ExcelRowsChanged != null)
{
ExcelRowsChanged(this, new TaoBaoBillEventArsg() { counterRows = totalRows, CurrentRowIndex = 0 });
}
PayType result;
for (int i = firstRowINdex; i < lastRowINdex; i++)
{
Bill tempBill = new Bill();
if (Enum.TryParse<PayType>((string)ws.Rows[i].Cells[11].Value, out result))
{
Decimal payment = (Decimal)ws.Rows[i].Cells[8].Value + (Decimal)ws.Rows[i].Cells[7].Value;
string orderno = (string)ws.Rows[i].Cells[3].Value;
tempBill.order = orderno.Substring(5, orderno.Length - 5);
tempBill.payment = payment;
bills.Trades.Add(tempBill);
//当值更改时触发事件
if (ExcelRowsChanged != null)
{
ExcelRowsChanged(this, new TaoBaoBillEventArsg() { counterRows = totalRows, CurrentRowIndex = (double)i });
}
}
}
excel.Quit();
}
}
这个类是准备为事件输入参数准备的。
public class TaoBaoBillEventArsg:EventArgs
{
public UInt32 counterRows { get; set; }
public double CurrentRowIndex { get; set; }
}
//以下是在另一个cs文件中。
namespace TaoBaoBillCheck
{
delegate void updateProgressBar(System.Windows.DependencyProperty dp,object value);//这个是在类的外面建立的。
public void getTaobaoData(object sender, EventArgs e)
{
//先分配一个progressBar的图形界面,需要建立grid,。
string filePath = @"C:\Users\dypeng\Desktop\淘宝打印生成文件\test.xls";
Border border = new Border();
border.BorderThickness = new Thickness(2d);
border.BorderBrush=new SolidColorBrush(Color.FromRgb(200,200,200));
ProgressBar progbar = new ProgressBar();
Canvas canvas = new Canvas();
border.Child = canvas;
border.Width = 300;
border.Height = 100;
canvas.Background = this.Background;
Label label = new Label();
label.Content = "解析进度";
canvas.Children.Add(label);
canvas.Children.Add(progbar);
progbar.IsIndeterminate = false;
progbar.Orientation = Orientation.Horizontal;
progbar.Width = 250;
progbar.Height = 15;
StaPan1.Children.Add(border);
border.Margin = new Thickness(100, 100, 0, 0);
progbar.Margin = new Thickness(20, 40, 0, 0);
BillRepository billRepository = new BillRepository();
updateProgressBar updateprogressBar = new updateProgressBar(progbar.SetValue);//实例化updateProgressBar 。
billRepository.ExcelRowsChanged += (o, i) =>
{
progbar.Minimum = 0;
//progbar.Value = i.CurrentRowIndex;
progbar.Maximum = (Double)i.counterRows;
//i注意ProgressBar.ValueProperty,它并非使用的实例。
Dispatcher.Invoke(updateprogressBar, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, i.CurrentRowIndex });
};
billRepository.getTaobaoBill(filePath);
StaPan1.Children.Remove(border);
}
}