事件委托加linq的操作
//委托的定义
public delegate string wings(string input);
/// <summary>
/// 委托方法二匿名
/// </summary>
class Program
{
static void Main(string[] args)
{
//匿名方法方式
wings w = delegate(string str)
{
return str.ToUpper();
};
string name = "my name is fly and I like eat apples";
//用数组分开
string[] delete = name.Split(' ');
foreach (var item in delete)
{
//此时的 w 实际上就是匿名 方法
Console.WriteLine(w(item));
}
Console.WriteLine("***************************");
//直接打印字符串w 实际上就是 匿名 方法
Console.WriteLine(w(name));
Console.Read();
}
}
wpf的Excel分类导出数据(Aspose)
对学习wpf的朋友们有一定的帮助 /// <summary>
/// 工参表分割类, 按区域进行对数据行进行分割
/// </summary>
public class AreaSplit
{
public event EventHandler<StringEventArgs> Opening;
public event EventHandler<StringEventArgs> ErrorHappened;
public event EventHandler<Int32EventArgs> BeginSplit;
public event EventHandler<Int32EventArgs> Splitting;
public event EventHandler<StringEventArgs> Saving;
public event EventHandler<StringEventArgs> Splitted;
private List<string> m_ColNames;
/// <summary>
/// 执行工参表分割
/// </summary>
/// <param name="sourceFile">原始工参文件</param>
/// <param name="outPath">分割后输出所在总目录</param>
public void StartSplit(string sourceFile, string outPath)
{
try
{
Dictionary<string, DataTable> dict = this.SplitToDictionary(sourceFile);
if (dict == null) return;
this.SaveToExcels(outPath, dict);
}
catch (Exception ex)
{
if (ErrorHappened != null)
ErrorHappened(this, new StringEventArgs(ex.Message));
}
}
/// <summary>
/// 将工参文件分割为Dictionary<string, DataTable>
/// </summary>
private Dictionary<string, DataTable> SplitToDictionary(string sourceFile)
{
if (!File.Exists(sourceFile))
{
if (ErrorHappened != null)
ErrorHappened(this, new StringEventArgs("指定路径的文件不存在"));
return null;
}
if (this.Opening != null)
this.Opening(this, new StringEventArgs("正在读取原始工参表 ..."));
Workbook wb = new Workbook(sourceFile);
if (wb.Worksheets.Count == 0)
{
if (ErrorHappened != null) ErrorHappened(this, new StringEventArgs("缺少有效的工作表"));
return null;
}
Worksheet ws = wb.Worksheets[0];
string areaKey = "区域";
int areaPos = -1;
// 获取区域所在列号
this.m_ColNames = new List<string>();
int rowCount = ws.Cells.MaxDataRow + 1;
int colCount = ws.Cells.MaxDataColumn + 1;
for (int i = 0; i < colCount; i++)
{
string str = ws.Cells[0, i].StringValue;
if (str == null) continue;
if (str.Equals(areaKey))
{
areaPos = i;
}
this.m_ColNames.Add(str);
}
if (areaPos == -1)
{
if (ErrorHappened != null)
ErrorHappened(this, new StringEventArgs("找不到正确的区域列"));
return null;
}
if (BeginSplit != null) BeginSplit(this, new Int32EventArgs(rowCount));
Dictionary<string, DataTable> dict = new Dictionary<string, DataTable>();
for (int i = 1; i < rowCount; i++)
{
string area = ws.Cells[i, areaPos].StringValue;
if (string.IsNullOrEmpty(area))
area = "未知区域";
if (!dict.ContainsKey(area))
{
DataTable dt = this.GetTable(colCount);
dict.Add(area, dt);
}
DataRow row = dict[area].NewRow();
for (int j = 0; j < colCount; j++)
{
row[j] = ws.Cells[i, j].StringValue;
}
dict[area].Rows.Add(row);
if (Splitting != null) Splitting(this, new Int32EventArgs(i));
}
return dict;
}
/// <summary>
/// 将分割后的数据,按区域分别保存为Excel文档
/// </summary>
/// <param name="outPath"></param>
/// <param name="dict"></param>
private void SaveToExcels(string outPath, Dictionary<string, DataTable> dict)
{
foreach (string key in dict.Keys)
{
if (Saving != null) this.Saving(this, new StringEventArgs("正在保存 " + key + "工参表"));
DataTable dt = dict[key];
if (outPath.Equals(""))
outPath = AppDomain.CurrentDomain.BaseDirectory + "分割后工参";
string dir = outPath + "\\" + key;
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
string fn = dir + "\\" + key + "_" + DateTime.Now.ToString("yyyyMMdd") + ".xlsx";
if (File.Exists(fn)) File.Delete(fn);
Workbook wb = new Workbook();
wb.Worksheets.Clear();
Worksheet ws = wb.Worksheets.Add(key + "工参表");
for (int i=0; i<this.m_ColNames.Count; i++)
{
ws.Cells[0, i].PutValue(this.m_ColNames[i]);
ws.Cells.Columns[i].Width += 2;
}
int rowCount = dt.Rows.Count;
int colCount = dt.Columns.Count;
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < colCount; j++)
ws.Cells[i+1, j].PutValue(dt.Rows[i][j].ToString());
}
wb.Save(fn, SaveFormat.Xlsx);
}
if (Splitted != null) Splitted(this, new StringEventArgs("工参表分割结束"));
}
private DataTable GetTable(int colCount)
{
DataTable dt = new DataTable();
for (int i = 0; i < colCount; i++)
{
DataColumn col = new DataColumn();
col.DataType = typeof(string);
dt.Columns.Add(col);
}
return dt;
}