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. java获取当前类名和方法名

    Description Below I present you two different ways to get the current Class: Using Thread Using getC ...

  2. [BZOJ1834][ZJOI2010]network 网络扩容 最大流+费用流

    1834: [ZJOI2010]network 网络扩容 Time Limit: 3 Sec  Memory Limit: 64 MB Submit: 3330  Solved: 1739 [Subm ...

  3. Guava源码学习(三)ImmutableCollection

    基于版本:Guava 22.0 Wiki:Immutable collections 0. ImmutableCollection简介 类似于JDK的Collections.unmodifiableX ...

  4. Codeforces Round #270 A. Design Tutorial: Learn from Math【数论/埃氏筛法】

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  5. HDU1016 Prime Ring Problem (回溯 + 剪枝)

    本文链接:http://www.cnblogs.com/Ash-ly/p/5398684.html 题意: 给你一个数字N(N <= 20),要求你把这N个数组成一个环,环内的数字不能重复,左右 ...

  6. linux coreseek-4.1安装

    1.假设已经有coreseek-4.1-beta.tar.gz源文件 [root@qp232 ~]# cd /usr/local [root@qp232 local]# tar -zxvf /yd/l ...

  7. jsp登陆

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  8. implements

    implements 是实现某个接口的意思. 如果某个类 后面使用 implements,并指定了相应的接口,那在该类下面就需要实现相应接口的方法. 比如:接口interface java.lang. ...

  9. CAP理论下对比ACID模型与BASE模型

    CAP介绍 Consistency(一致性), 数据一致更新,所有数据变动都是同步的.比如网购,库存减少的同时资金增多.Availability(可用性), 好的响应性能.比如支付操作10ms内响应用 ...

  10. CSS限制

    http://www.cnblogs.com/YanPSun/archive/2012/03/16/2400141.html