1.文件操作
  1.1递归目录,查找所有文件

  

 #region 递归目录路径
private List<string> GetDirs(string path)
{
string[] dirs = Directory.GetDirectories(path);
List<string> result = new List<string>();
foreach(string dir in dirs)
{
result.Add(dir);
}
if (dirs.Length > )
{
foreach (string dir in dirs)
{
result.AddRange(GetDirs(dir));
}
}
return result;
}
#endregion //调用
List<string> strdirs = GetDirs(path);
List<string> strfiles = new List<string>();
foreach (string dir in strdirs)
{
foreach (string file in Directory.GetFiles(dir,"*汇总*.xls"))
{
strfiles.Add(file);
}
}

  1.2 获取文件目录与文件名:

  #region 获取文件路径设置
private List<string> GetFileSet(string par_path)
{
List<string> result = new List<string>();
result.Add(Path.GetDirectoryName(par_path));
result.Add(Path.GetFileNameWithoutExtension(par_path));
return result;
}
#endregion

2.导入导出Excel与数据统计:

 using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.Collections.Generic; #region 处理数据
private void BI(string mespath, string scorepath)
{
try
{
List<string> fnames = GetFileSet(scorepath); DataTable msg_table = new DataTable();
DataTable score_table = new DataTable();
//数据处理
//读取考生信息表
using (FileStream fs = File.OpenRead(mespath))
{
HSSFWorkbook msg_xls = new HSSFWorkbook(fs);
for (int i = ; i < msg_xls.NumberOfSheets; i++)
{
ISheet sheet = msg_xls.GetSheetAt(i);
for (int j = ; j <= sheet.LastRowNum; j++)
{
IRow row = sheet.GetRow(j);
if (j > )
{
if (row != null)
{
DataRow t_row = msg_table.NewRow();
for (int k = ; k <= row.LastCellNum; k++)
{
ICell cell = row.GetCell(k);
if (cell != null)
{
t_row[k] = cell.ToString().Trim();
}
}
msg_table.Rows.Add(t_row);
}
}
else
{
if (row != null)
{
//表定义
for (int k = ; k <= row.LastCellNum; k++)
{
ICell cell = row.GetCell(k);
if (cell != null)
{
msg_table.Columns.Add(new DataColumn(cell.StringCellValue, typeof(string)));
}
}
}
}
}
}
}
//读取考生成绩表
using (FileStream fs = File.OpenRead(scorepath))
{
HSSFWorkbook msg_xls = new HSSFWorkbook(fs);
for (int i = ; i < msg_xls.NumberOfSheets; i++)
{
ISheet sheet = msg_xls.GetSheetAt(i);
for (int j = ; j <= sheet.LastRowNum; j++)
{
IRow row = sheet.GetRow(j);
if (j > )
{
if (row != null)
{
DataRow t_row = score_table.NewRow();
for (int k = ; k <= row.LastCellNum; k++)
{
ICell cell = row.GetCell(k);
if (cell != null)
{
t_row[k] = cell.ToString().Trim();
}
}
score_table.Rows.Add(t_row);
}
}
else
{
if (row != null)
{
//表定义
for (int k = ; k <= row.LastCellNum; k++)
{
ICell cell = row.GetCell(k);
if (cell != null)
{
score_table.Columns.Add(new DataColumn(cell.StringCellValue.Trim(), typeof(string)));
}
}
}
}
}
}
}
DataTable res_table = new DataTable();
res_table.Columns.Add(new DataColumn("考号", typeof(string)));
res_table.Columns.Add(new DataColumn("姓名", typeof(string)));
res_table.Columns.Add(new DataColumn("身份证号码", typeof(string)));
res_table.Columns.Add(new DataColumn("执业类别", typeof(string)));
res_table.Columns.Add(new DataColumn("执业证书编号", typeof(string)));
res_table.Columns.Add(new DataColumn("资格证编号", typeof(string)));
res_table.Columns.Add(new DataColumn("总分", typeof(float))); //进行数据统计
float failcount = ; //不及格人数
List<student_modele> failtable = new List<student_modele>(); //不及格名单
student_modele failstudent = null;
//进行数据处理
DataRow res_row = null;
for (int n = ; n < score_table.Rows.Count; n++)
{
res_row = res_table.NewRow();
res_row["考号"] = score_table.Columns.Contains("考号") ? score_table.Rows[n]["考号"] : "";
res_row["姓名"] = score_table.Columns.Contains("姓名") ? score_table.Rows[n]["姓名"] : "";
res_row["身份证号码"] = score_table.Columns.Contains("身份证号码") ? score_table.Rows[n]["身份证号码"] : "";
res_row["执业类别"] = score_table.Columns.Contains("执业类别") ? score_table.Rows[n]["执业类别"] : "";
res_row["执业证书编号"] = score_table.Columns.Contains("执业证书编号") ? score_table.Rows[n]["执业证书编号"] : "";
res_row["资格证编号"] = score_table.Columns.Contains("资格证编号") ? score_table.Rows[n]["资格证编号"] : "";
res_row["总分"] = score_table.Columns.Contains("总分") ? score_table.Rows[n]["总分"] : ; if ((float)res_row["总分"] < )
{
failcount++;
failstudent = new student_modele();
failstudent.Num = res_row["考号"].ToString();
failstudent.Name = res_row["姓名"].ToString();
failstudent.PeopleNum = res_row["身份证号码"].ToString();
failstudent.Type = res_row["执业类别"].ToString();
failstudent.TypeNum = res_row["执业证书编号"].ToString();
failstudent.SeniorityNum = res_row["资格证编号"].ToString();
failstudent.Score=(float) res_row["总分"];
failtable.Add(failstudent);
}
res_table.Rows.Add(res_row);
}
this.Total_kryptonTextBox.Text += fnames[] + "统计:\r\n";
float per = - failcount / res_table.Rows.Count;
this.Total_kryptonTextBox.Text += "合格率:"+per.ToString("p") + "\r\n";
if (failcount > )
{
this.Total_kryptonTextBox.Text += "不合格名单:\r\n";
for (int n = ; n < failtable.Count; n++)
{
this.Total_kryptonTextBox.Text += failtable[n].Num + "\r\n";
}
}
this.Total_kryptonTextBox.Text += "--------------------------------------------------------------------------\r\n";
//导出Excel
using (FileStream fs = new FileStream(fnames[] + "\\" + fnames[] + "_汇总.xls", FileMode.Create))
{
HSSFWorkbook result_xls = new HSSFWorkbook();
ISheet sheet = result_xls.CreateSheet(fnames[]);
//创建行
IRow row = sheet.CreateRow();
for (int n = ; n < res_table.Columns.Count; n++)
{
//单元格设置
ICell cell = row.CreateCell(n);
cell.SetCellValue(res_table.Columns[n].ColumnName);
}
//设置表头样式
var titlestyle = result_xls.CreateCellStyle();
titlestyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
//设置excel列宽
sheet.SetColumnWidth(, * );
sheet.GetRow().GetCell().CellStyle = titlestyle; sheet.SetColumnWidth(, * );
sheet.GetRow().GetCell().CellStyle = titlestyle; sheet.SetColumnWidth(, * );
sheet.GetRow().GetCell().CellStyle = titlestyle; sheet.SetColumnWidth(, * );
sheet.GetRow().GetCell().CellStyle = titlestyle; sheet.SetColumnWidth(, * );
sheet.GetRow().GetCell().CellStyle = titlestyle; sheet.SetColumnWidth(, * );
sheet.GetRow().GetCell().CellStyle = titlestyle; sheet.SetColumnWidth(, * );
sheet.GetRow().GetCell().CellStyle = titlestyle; var style1 = result_xls.CreateCellStyle();
style1.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Right; DataRow t_row = null;
for (int n = ; n < res_table.Rows.Count; n++)
{
t_row = res_table.Rows[n];
row = sheet.CreateRow(n + );
for (int p = ; p < t_row.ItemArray.Length; p++)
{
ICell cell = row.CreateCell(p);
cell.SetCellValue(t_row.ItemArray[p].ToString());
switch (p)
{
case :
cell.CellStyle = style1;
break;
case :
cell.CellStyle = titlestyle;
break;
case :
cell.CellStyle = style1;
break;
case :
cell.CellStyle = titlestyle;
break;
case :
cell.CellStyle = style1;
break;
case :
cell.CellStyle = style1;
break;
case :
cell.CellStyle = titlestyle;
break;
}
}
}
result_xls.Write(fs);
}
}
catch
{
errorlogs.Append("发生错误:成绩文件为:"+scorepath+"\r\n");
}
}
#endregion

3.数据模型:

  #region 统计数据模型
class student_modele
{
private string num = string.Empty; //考号
private string name = string.Empty; //姓名
private string peoplenum = string.Empty; //身份证号
private string type = string.Empty; //职业类别
private string typenum = string.Empty; //职业证书编号
private string senioritynum = string.Empty; //资格证编号
private float score = ; //总分 public string Num
{
get { return num; }
set { this.num=value;}
} public string Name
{
get { return name; }
set { this.name = value; }
} public string PeopleNum
{
get { return peoplenum; }
set {this.peoplenum=value;}
} public string Type
{
get { return type; }
set { this.type = value; }
} public string TypeNum
{
get { return typenum; }
set { this.typenum = value; }
} public string SeniorityNum
{
get { return senioritynum; }
set { this.senioritynum = value; }
} public float Score
{
get { return score; }
set { this.score = value; }
}
}
#endregion

.Net开发复习与总结的更多相关文章

  1. IOS开发复习笔记(1)-OC基础知识

    在上班之余学习IOS已经有三个多月了,因为基础有些薄弱从OC的基本语法开始学习的,相继看了青柚子和红柚子的书,现在在看编程实战,趁这个机会好好的总结一下: 1.命名约定 对象类型和名称一致,以免混淆 ...

  2. IOS开发复习笔记(4)-TableView

    总结几个TableView常用的代码 1.初始化方面 static string CellIndetifier=@"cellIndetifier"; -(NSInteger)num ...

  3. IOS开发复习笔记(3)-ARC

    1.ARC 当你自己调用了release或retain语句的时候,ARC有效时编译文件会遇到错误,你可以通过-fno-objc-arc和-fobjc-arc两个编译器标志在混搭中支持ARC和非ARC的 ...

  4. 安卓开发笔记——Fragment+ViewPager组件(高仿微信界面)

    什么是ViewPager? 关于ViewPager的介绍和使用,在之前我写过一篇相关的文章<安卓开发复习笔记——ViewPager组件(仿微信引导界面)>,不清楚的朋友可以看看,这里就不再 ...

  5. 安卓开发笔记——Fragment+FragmentTabHost组件(实现新浪微博底部菜单)

    记得之前写过2篇关于底部菜单的实现,由于使用的是过时的TabHost类,虽然一样可以实现我们想要的效果,但作为学习,还是需要来了解下这个新引入类FragmentTabHost 之前2篇文章的链接: 安 ...

  6. 安卓开发笔记——TabHost组件(二)(实现底部菜单导航)

    上面文章<安卓开发复习笔记——TabHost组件(一)(实现底部菜单导航)>中提到了利用自定义View(ImageView+TextView)来设置一个底部菜单的样式 这边再补充一种更为灵 ...

  7. 安卓开发笔记——TabHost组件(一)(实现底部菜单导航)

    什么是TabHost? TabHost组件的主要功能是可以进行应用程序分类管理,例如:在用户使用windows操作系统的时候,经常见到如图所示的图形界面.     TabHost选项卡,说到这个组件, ...

  8. 转-Fragment+ViewPager组件(高仿微信界面)

    http://www.cnblogs.com/lichenwei/p/3982302.html 什么是ViewPager? 关于ViewPager的介绍和使用,在之前我写过一篇相关的文章<安卓开 ...

  9. 转-Fragment+FragmentTabHost组件(实现新浪微博底部菜单)

    http://www.cnblogs.com/lichenwei/p/3985121.html 记得之前写过2篇关于底部菜单的实现,由于使用的是过时的TabHost类,虽然一样可以实现我们想要的效果, ...

随机推荐

  1. bottle框架学习(2):变量定义等

    try: from simplejson import dumps as json_dumps, loads as json_lds except ImportError: # pragma: no ...

  2. Ionic Angular自动捕获错误 配置Angular2.x +

    配置app.module.ts import { Pro } from '@ionic/pro'; // These are the imports required for the code bel ...

  3. POJ 3620 Avoid The Lakes【DFS找联通块】

    Avoid The Lakes Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6826   Accepted: 3637 D ...

  4. sqldeveloper 英文设置

    在软件ide\bin目录下找到sqldeveloper.conf或ide.conf,加入 C:\Program Files (x86)\sqldeveloper\ide\bin AddVMOption ...

  5. NMAP输出结果中CPE的含义

    NMAP输出结果中CPE的含义   CPE全称是Common Platform Enumeration,意思是通用平台枚举项.它是NMAP对识别出来的软件.操作系统和硬件的一种命名方式.它的格式如下: ...

  6. Binary Tree Maximum Path Sum - LeetCode

    Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...

  7. [CTSC2018]假面(概率DP)

    考场上以为CTSC的概率期望题都不可做,连暴力都没写直接爆零. 结果出来发现全场70以上,大部分AC,少于70的好像极少,感觉血亏. 设a[i][j]表示到当前为止第i个人的血量为j的概率(注意特判血 ...

  8. La 4976 Defense lines

    蓝书紫书上都有的一道题...这里就懒得说题解了. 但是我竟然WA了6次!为什么呢??? 一开始没看见连续子序列..... 后来插入的时候忘判断了是不是比前驱大.... 所以我们只需要维护一个权值递增( ...

  9. UVA 11990 ”Dynamic“ Inversion(线段树+树状数组)

    [题目链接] UVA11990 [题目大意] 给出一个数列,每次删去一个数,求一个数删去之前整个数列的逆序对数. [题解] 一开始可以用树状数组统计出现的逆序对数量 对于每个删去的数,我们可以用线段树 ...

  10. Struts2笔记--文件上传

    Servlet 3.0规范的HttpServletRequest已经提供了方法来处理文件上传但这种上传需要在Servlet中完成.而Struts2则提供了更简单的封装. Struts2默认使用的是Ja ...