2 Star 2 Fork 1

zhyuey/SchoolTool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Form1.cs 35.00 KB
一键复制 编辑 原始数据 按行查看 历史
zhyuey 提交于 2014-03-22 13:26 . First commit
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
using Microsoft.CSharp;
namespace SchoolTool
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public List<int>[] info;
List<int>[] boydivclass;
List<int>[] girldivclass;
int[] boybelongclass;
int[] girlbelongclass;
Student[] stulist;
int boynum;
int girlnum;
int classnum;
int[] boyclassarray;
int[] girlclassarray;
List<Student>[] stuinclass;
List<Student> stulist1;
public bool isNumber(string str)
{
return System.Text.RegularExpressions.Regex.IsMatch(str, @"^[0-9]*$");
}
private void genDivInfoBtn_Click(object sender, EventArgs e)
{
boynum = Int32.Parse(boyNumBox.Text.ToString());
girlnum = Int32.Parse(girlNumBox.Text.ToString());
if (classNumBox.Text.Equals(""))
{
MessageBox.Show("请输入班级数");
return;
}
if (!isNumber(classNumBox.Text))
{
MessageBox.Show("请输入正确的数字(1-100)");
return;
}
classnum = Int32.Parse(classNumBox.Text.ToString());
if (classnum <= 0 || classnum > 100)
{
MessageBox.Show("请输入合理的班级数值");
classNumBox.Text = "";
return;
}
infoWinTextBox.Text = "";
boyclassarray = new int[classnum];
girlclassarray = new int[classnum];
Random ra = new Random(unchecked((int)DateTime.Now.Ticks));
int boyperclass = boynum / classnum;
int girlperclass = girlnum / classnum;
for (int i = 0; i < classnum; i++)
{
boyclassarray[i] = boyperclass;
}
for (int i = 0; i < boynum - boyperclass * classnum; i++)
{
int tmp = ra.Next(classnum);
while (boyclassarray[tmp] > boyperclass)
{
tmp = ra.Next(classnum);
}
boyclassarray[tmp]++;
}
for (int i = 0; i < classnum; i++)
{
girlclassarray[i] = girlperclass;
}
for (int i = 0; i < girlnum - girlperclass * classnum; i++)
{
int tmp = ra.Next(classnum);
while (girlclassarray[tmp] > girlperclass)
{
tmp = ra.Next(classnum);
}
girlclassarray[tmp]++;
}
int maxcount = 0;
int mincount = 0;
for (int i = 0; i < classnum; i++)
{
int tmp = girlclassarray[i] + boyclassarray[i];
if (tmp == girlperclass + boyperclass + 2)
{
maxcount++;
}
if (tmp == girlperclass + boyperclass)
{
mincount++;
}
}
if (maxcount > 0 && mincount > 0)
{
int mmin = Math.Min(maxcount, mincount);
int addcount = 0;
int subcount = 0;
for (int i = 0; i < classnum; i++)
{
int tmp = girlclassarray[i] + boyclassarray[i];
if (tmp == girlperclass + boyperclass + 2 && subcount < mmin)
{
boyclassarray[i]--;
subcount++;
}
if (tmp == girlperclass + boyperclass && addcount < mmin)
{
boyclassarray[i]++;
addcount++;
}
if (subcount == mmin && addcount == mmin)
break;
}
}
stulist = new Student[boynum + girlnum];
for (int i = 0; i < boynum + girlnum; i++)
{
stulist[i] = new Student();
}
for (int i = 0; i < boynum; i++)
{
stulist[i].num = i;
stulist[i].orinum = boyorinum[i];
stulist[i].oriname = boyoriname[i];
stulist[i].sex = true;
stulist[i].bannum = 0;
}
for (int i = 0; i < girlnum; i++)
{
stulist[i + boynum].num = i + 1;
stulist[i + boynum].orinum = girlorinum[i];
stulist[i + boynum].oriname = girloriname[i];
stulist[i + boynum].sex = false;
stulist[i + boynum].bannum = 0;
}
bool sucflag = false;
for(int ll = 0; ll < 20; ll++)
{
bool ff = maingen();
if (ff)
{
sucflag = true;
break;
}
}
if (sucflag)
{
infoWinTextBox.AppendText("#########################################\r\n");
infoWinTextBox.AppendText("产生分配信息成功,请按相应按钮保存至文件\r\n");
infoWinTextBox.AppendText("#########################################\r\n");
}
else
{
infoWinTextBox.AppendText("#########################################\r\n");
infoWinTextBox.AppendText("产生分配信息失败,请按相应按钮重新分配\r\n");
infoWinTextBox.AppendText("#########################################\r\n");
infoWinListBox.Items.Clear();
classComboBox.Items.Clear();
return;
}
classComboBox.Items.Clear();
for (int i = 0; i < classnum; i++)
{
stuinclass[i].Sort();
classComboBox.Items.Add(i + 1);
}
//for (int i = 0; i < classnum; i++)
//{
// boydivclass[i].Sort();
// girldivclass[i].Sort();
//}
stulist1=new List<Student>();
for (int i = 0; i < classnum; i++)
{
for (int j = 0; j < stuinclass[i].Count; j++)
{
stuinclass[i][j].bannum = i + 1;
stulist1.Add(stuinclass[i][j]);
}
}
stulist1.Sort();
for (int i = 0; i < boynum + girlnum - 1; i++)
{
if (stulist1[i + 1].oriname.Equals(stulist1[i].oriname))
{
List<int> repeat = new List<int>();
repeat.Add(stulist1[i].bannum);
repeat.Add(stulist1[i + 1].bannum);
int j = i + 2;
while (j < boynum + girlnum && stulist1[j].oriname.Equals(stulist1[i].oriname))
{
repeat.Add(stulist1[j].bannum);
j++;
};
int orilen = repeat.Count;
for (int ii = orilen - 1; ii >= 0; --ii)
{
if (repeat.IndexOf(repeat[ii]) != ii)
{
repeat.RemoveAt(ii);
}
}
if (orilen > classnum)
{
string lline = string.Format("{0} 有{1}位重名,大于班级数,由抽屉原理,不可能出现班内无同名现象\r\n但您仍可使用本次分班信息作为参考\r\n", stulist1[i].oriname, orilen);
infoWinTextBox.AppendText(lline);
}
else if (orilen == repeat.Count)
{
string lline = string.Format("{0} 有{1}位重名,均已分至不同班级\r\n", stulist1[i].oriname, orilen);
infoWinTextBox.AppendText(lline);
}
else if (orilen > repeat.Count)
{
string lline = string.Format("{0} 有{1}位重名,没有分至不同班级,建议重新分配!\r\n", stulist1[i].oriname, orilen);
infoWinTextBox.AppendText(lline);
//suc = false;
}
i = j - 1;
}
}
for (int i = 0; i < classnum; i++)
{
string a = String.Format("班级 {0}\t 总人数 {1} 男生 {2} 女生 {3}\n", i + 1, boyclassarray[i] + girlclassarray[i], boyclassarray[i], girlclassarray[i]);
infoWinTextBox.AppendText(a);
}
//for (int i = 0; i < girlnum + boynum; i++)
//{
// string a = string.Format("{0}\r\n", stulist[i].oriname);
// infoWinTextBox.AppendText(a);
//}
label4.Visible = true;
label5.Visible = true;
classComboBox.Visible = true;
classComboBox.SelectedIndex = 0;
fileOutBtn.Visible = true;
}
private bool maingen()
{
Random ra = new Random(unchecked((int)DateTime.Now.Ticks));
bool sucflag = true;
List<Student> final = new List<Student>();
int curboynum = boynum;
int curgirlnum = girlnum;
stuinclass = new List<Student>[classnum];
for (int i = 0; i < stulist.Length; i++)
{
final.Add(stulist[i]);
}
for (int i = 0; i < classnum - 1; i++)
{
stuinclass[i] = new List<Student>();
int tmp;
for (int j = 0; j < boyclassarray[i]; j++)
{
while (true)
{
tmp = ra.Next(curboynum);
bool hasdup = false;
for (int k = 0; k < stuinclass[i].Count; k++)
{
if (stuinclass[i][k].oriname.Equals(final[tmp].oriname))
{
hasdup = true;
break;
}
}
if (!hasdup)
break;
};
stuinclass[i].Add(final[tmp]);
final.RemoveAt(tmp);
curboynum--;
}
}
stuinclass[classnum - 1] = new List<Student>();
for (int j = 0; j < curboynum; j++)
{
Student tt = final[0];
//final.RemoveAt(0);
for (int k = 0; k < stuinclass[classnum - 1].Count; k++)
{
if (stuinclass[classnum - 1][k].oriname.ToString().Equals(tt.oriname.ToString()))
{
sucflag = false;
return sucflag;
}
}
if (sucflag)
{
stuinclass[classnum - 1].Add(tt);
final.RemoveAt(0);
}
else
{
return false;
}
}
for (int i = 0; i < classnum - 1; i++)
{
//stuinclass[i] = new List<Student>();
int tmp;
for (int j = 0; j < girlclassarray[i]; j++)
{
while (true)
{
tmp = ra.Next(curgirlnum);
bool hasdup = false;
for (int k = 0; k < stuinclass[i].Count; k++)
{
if (stuinclass[i][k].oriname.Equals(final[tmp].oriname))
{
hasdup = true;
break;
}
}
if (!hasdup)
break;
};
stuinclass[i].Add(final[tmp]);
final.RemoveAt(tmp);
curgirlnum--;
}
}
//stuinclass[classnum - 1] = new List<Student>();
for (int j = 0; j < curgirlnum; j++)
{
Student tt = final[0];
//final.RemoveAt(0);
for (int k = 0; k < stuinclass[classnum - 1].Count; k++)
{
if (stuinclass[classnum - 1][k].oriname.ToString().Equals(tt.oriname.ToString()))
{
sucflag = false;
return sucflag;
}
}
if (sucflag)
{
stuinclass[classnum - 1].Add(tt);
final.RemoveAt(0);
}
else
{
return false;
}
}
return true;
}
private void classComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
int num = Int32.Parse(classComboBox.SelectedItem.ToString())-1;
infoWinListBox.Items.Clear();
for (int j = 0; j < stuinclass[num].Count; j++)
{
string a = String.Format("{0}\t{1}\t{2}", j + 1, stuinclass[num][j].oriname, stuinclass[num][j].orinum);
infoWinListBox.Items.Add(a);
}
}
private void Form1_Load(object sender, EventArgs e)
{
label4.Visible = false;
label5.Visible = false;
boyNumBox.Enabled = false;
girlNumBox.Enabled = false;
classComboBox.Visible = false;
genDivInfoBtn.Visible = false;
fileOutBtn.Visible = false;
//fileNameInputTextBox.Enabled = false;
//folderNameOutputTextBox.Enabled = false;
}
private void fileOutBtn_Click(object sender, EventArgs e)
{
fileWriteOutCsv();
fileWriteOutTxt();
}
private void fileWriteOutTxt()
{
string rootdir = folderNameOutputTextBox.Text;
if (!Directory.Exists(rootdir))
{
MessageBox.Show("输出目录不存在");
return;
}
System.IO.FileInfo f;
//infoWinTextBox.Text = "";
String instrutionline;
FileStream fs;
try
{
fs = new FileStream(rootdir + "\\zonghe.txt", FileMode.Create);
}
catch (Exception err)
{
String se = String.Format("创建文件错误,可能的原因是文件名重名且原文件为只读\r\n请换个输出目录");
MessageBox.Show(se);
return;
}
String tt = String.Format("男生总数 {0} 女生总数 {1} 学生总数 {2} 班级数 {3}\r\n", boynum, girlnum, boynum + girlnum, classnum);
byte[] data = new UTF8Encoding().GetBytes(tt);
fs.Write(data, 0, data.Length);
for (int i = 0; i < classnum; i++)
{
String str = String.Format("班级 {0}: 男生 {1} 女生 {2} 总人数 {3} \r\n", i + 1, boyclassarray[i], girlclassarray[i], boyclassarray[i] + girlclassarray[i]);
byte[] data1 = new UTF8Encoding().GetBytes(str);
fs.Write(data1, 0, data1.Length);
}
fs.Flush();
fs.Close();
f = new FileInfo(rootdir + "\\zonghe.txt");
f.Attributes = FileAttributes.ReadOnly;
instrutionline = String.Format("班级分配综合表已保存至:\r\n{0}\r\n", rootdir + "\\zonghe.txt");
infoWinTextBox.AppendText(instrutionline);
FileStream zb;
try
{
zb = new FileStream(rootdir + "\\xiangxi.txt", FileMode.Create);
}
catch (Exception err)
{
String se = String.Format("创建文件错误,可能的原因是文件名重名且原文件为只读\r\n请换个输出目录");
MessageBox.Show(se);
return;
}
String fss = String.Format("班级,报名序号,姓名,性别\r\n");
byte[] data4 = new UTF8Encoding().GetBytes(fss);
zb.Write(data4, 0, data4.Length);
for (int i = 0; i < stulist1.Count; i++)
{
String ffss = String.Format("{0},{1},{2},{3}\r\n", stulist1[i].bannum, stulist1[i].orinum, stulist1[i].oriname, (stulist1[i].sex == true) ? "男" : "女");
byte[] data5 = new UTF8Encoding().GetBytes(ffss);
zb.Write(data5, 0, data5.Length);
}
zb.Flush();
zb.Close();
f = new FileInfo(rootdir + "\\xiangxi.txt");
f.Attributes = FileAttributes.ReadOnly;
instrutionline = String.Format("班级分配详细表已保存至:\r\n{0}\r\n", rootdir + "\\xiangxi.txt");
infoWinTextBox.AppendText(instrutionline);
for (int i = 0; i < classnum; i++)
{
string fn = string.Format("{0}\\class{1}.txt", rootdir, i + 1);
StreamWriter sw;
try
{
sw = new StreamWriter(fn, false, Encoding.GetEncoding("utf-8"));
}
catch (Exception err)
{
String se = String.Format("创建文件错误,可能的原因是文件名重名且原文件为只读\r\n请换个输出目录");
MessageBox.Show(se);
return;
}
String ss = String.Format("班级,报名序号,姓名,性别\r\n");
//byte[] data2 = new UTF8Encoding().GetBytes(ss);
//ff.Write(data2, 0, data2.Length);
sw.Write(ss);
for (int j = 0; j < stuinclass[i].Count; j++)
{
String sss = String.Format("{0},{1},{2},{3}\r\n", stuinclass[i][j].bannum, stuinclass[i][j].orinum, stuinclass[i][j].oriname, stuinclass[i][j].sex == true ? "男" : "女");
//byte[] data3 = new UTF8Encoding().GetBytes(sss);
//ff.Write(data3, 0, data3.Length);
sw.Write(sss);
}
sw.Flush();
sw.Close();
f = new FileInfo(fn);
f.Attributes = FileAttributes.ReadOnly;
instrutionline = String.Format("班级{0}学生名单已保存至:\r\n{1}\r\n", i + 1, fn);
infoWinTextBox.AppendText(instrutionline);
}
}
private void fileWriteOutCsv()
{
string rootdir = folderNameOutputTextBox.Text;
if (!Directory.Exists(rootdir))
{
MessageBox.Show("输出目录不存在");
return;
}
System.IO.FileInfo f;
infoWinTextBox.Text = "";
String instrutionline;
FileStream fs;
try
{
fs = new FileStream(rootdir + "\\zonghe.csv", FileMode.Create);
}
catch (Exception err)
{
String se = String.Format("创建文件错误,可能的原因是文件名重名且原文件为只读\r\n请换个输出目录");
MessageBox.Show(se);
return;
}
String tt = String.Format("男生总数 {0} 女生总数 {1} 学生总数 {2} 班级数 {3}\r\n", boynum, girlnum, boynum + girlnum, classnum);
byte[] data = new UTF8Encoding().GetBytes(tt);
fs.Write(data, 0, data.Length);
for (int i = 0; i < classnum; i++)
{
String str = String.Format("班级 {0}: 男生 {1} 女生 {2} 总人数 {3} \r\n", i + 1, boyclassarray[i], girlclassarray[i], boyclassarray[i] + girlclassarray[i]);
byte[] data1 = new UTF8Encoding().GetBytes(str);
fs.Write(data1, 0, data1.Length);
}
fs.Flush();
fs.Close();
f = new FileInfo(rootdir + "\\zonghe.csv");
f.Attributes = FileAttributes.ReadOnly;
instrutionline = String.Format("班级分配综合表已保存至:\r\n{0}\r\n", rootdir + "\\zonghe.csv");
infoWinTextBox.AppendText(instrutionline);
FileStream zb;
try
{
zb = new FileStream(rootdir + "\\xiangxi.csv", FileMode.Create);
}
catch (Exception err)
{
String se = String.Format("创建文件错误,可能的原因是文件名重名且原文件为只读\r\n请换个输出目录");
MessageBox.Show(se);
return;
}
String fss = String.Format("班级,报名序号,姓名,性别\r\n");
byte[] data4 = new UTF8Encoding().GetBytes(fss);
zb.Write(data4, 0, data4.Length);
for (int i = 0; i < stulist1.Count; i++)
{
String ffss = String.Format("{0},{1},{2},{3}\r\n", stulist1[i].bannum, stulist1[i].orinum, stulist1[i].oriname, (stulist1[i].sex == true) ? "男" : "女");
byte[] data5 = new UTF8Encoding().GetBytes(ffss);
zb.Write(data5, 0, data5.Length);
}
zb.Flush();
zb.Close();
f = new FileInfo(rootdir + "\\xiangxi.csv");
f.Attributes = FileAttributes.ReadOnly;
instrutionline = String.Format("班级分配详细表已保存至:\r\n{0}\r\n", rootdir + "\\xiangxi.csv");
infoWinTextBox.AppendText(instrutionline);
for (int i = 0; i < classnum; i++)
{
string fn = string.Format("{0}\\class{1}.csv", rootdir, i + 1);
StreamWriter sw;
try
{
sw = new StreamWriter(fn, false, Encoding.GetEncoding("utf-8"));
}
catch (Exception err)
{
String se = String.Format("创建文件错误,可能的原因是文件名重名且原文件为只读\r\n请换个输出目录");
MessageBox.Show(se);
return;
}
String ss = String.Format("班级,报名序号,姓名,性别\r\n");
//byte[] data2 = new UTF8Encoding().GetBytes(ss);
//ff.Write(data2, 0, data2.Length);
sw.Write(ss);
for (int j = 0; j < stuinclass[i].Count; j++)
{
String sss = String.Format("{0},{1},{2},{3}\r\n", stuinclass[i][j].bannum, stuinclass[i][j].orinum, stuinclass[i][j].oriname, stuinclass[i][j].sex == true ? "男" : "女");
//byte[] data3 = new UTF8Encoding().GetBytes(sss);
//ff.Write(data3, 0, data3.Length);
sw.Write(sss);
}
sw.Flush();
sw.Close();
f = new FileInfo(fn);
f.Attributes = FileAttributes.ReadOnly;
instrutionline = String.Format("班级{0}学生名单已保存至:\r\n{1}\r\n", i + 1, fn);
infoWinTextBox.AppendText(instrutionline);
}
}
List<string> boyorinum;
List<string> girlorinum;
List<string> boyoriname;
List<string> girloriname;
private void readFileBtn_Click(object sender, EventArgs e)
{
string filename = fileNameInputTextBox.Text;
infoWinTextBox.Text = "";
if (!File.Exists(filename))
{
MessageBox.Show("输入文件不存在");
return;
}
infoWinTextBox.AppendText("正在打开文件...\r\n");
DataSet myset = LoadDataFromExcel(filename);
boyorinum = new List<string>();
boyoriname = new List<string>();
girlorinum = new List<string>();
girloriname = new List<string>();
for (int i = 0; i < myset.Tables[0].Rows.Count; i++ )
{
var row = myset.Tables[0].Rows[i];
var s0 = row[0].ToString();
var s1 = row[1].ToString();
var s2 = row[2].ToString();
s1 = s1.Replace(" ", "");
if (s2.Trim().Equals("男"))
{
boyorinum.Add(s0);
boyoriname.Add(s1);
}
if (s2.Trim().Equals("女"))
{
girlorinum.Add(s0);
girloriname.Add(s1);
}
String aa = String.Format("{0}\t{1}\t{2}", s0, s1, s2);
infoWinListBox.Items.Add(aa);
}
boynum = boyorinum.Count;
girlnum = girlorinum.Count;
boyNumBox.Text = boynum.ToString();
girlNumBox.Text = girlnum.ToString();
genDivInfoBtn.Visible = true;
infoWinTextBox.AppendText("文件已读入\r\n");
string infoline = string.Format("男生数\t{0}\r\n女生数\t{1}\r\n总人数\t{2}\r\n", boynum, girlnum, boynum + girlnum);
infoWinTextBox.AppendText(infoline);
}
private void chooseInputFileBtn_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Excel文件(*.xls)|*.xls";
ofd.ShowDialog();
fileNameInputTextBox.Text = ofd.FileName.ToString();
}
private void chooseOutputFolderBtn_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.Description = "请选择一个输出文件夹";
fbd.ShowDialog();
folderNameOutputTextBox.Text = fbd.SelectedPath;
}
public static DataSet LoadDataFromExcel(string filePath)
{
try
{
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1'";
OleDbConnection OleConn = new OleDbConnection(strConn);
OleConn.Open();
String sql = "SELECT * FROM [Sheet1$]";//可是更改Sheet名称,比如sheet2,等等
OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
DataSet OleDsExcle = new DataSet();
OleDaExcel.Fill(OleDsExcle, "Sheet1");
OleConn.Close();
return OleDsExcle;
}
catch (Exception err)
{
MessageBox.Show("数据绑定Excel失败!失败原因:" + err.Message, "提示信息",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return null;
}
}
private void label4_Click(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
//private void button1_Click(object sender, EventArgs e)
//{
//}
}
public class CodeFormat
{
/// <summary>
/// 根据文件名称判断文件的编码格式
/// </summary>
/// <param name="FILE_NAME"></param>
/// <returns></returns>
public static System.Text.Encoding GetType(string FILE_NAME)
{
FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
System.Text.Encoding r = GetType(fs);
fs.Close();
return r;
}
/// <summary>
/// 根据文件流判断文件编码格式
/// </summary>
/// <param name="fs"></param>
/// <returns></returns>
public static System.Text.Encoding GetType(FileStream fs)
{
BinaryReader r = new BinaryReader(fs, System.Text.Encoding.Default);
byte[] ss = r.ReadBytes((int)fs.Length);
r.Close();
if (ss == null || ss.Length <= 0)
return null;
//Encoding Representation
//UTF-8 EF BB BF
//UTF-16 Big Endian FE FF
//UTF-16 Little Endian FF FE
//UTF-32 Big Endian 00 00 FE FF
//UTF-32 Little Endian FF FE 00 00
//编码类型 Coding=编码类型.ASCII;
if (ss.Length >= 3 &&
ss[0] == 0xEF && ss[1] == 0xBB && ss[2] == 0xBF)
{//UTF-8 EF BB BF
return System.Text.Encoding.UTF8;
}
else if (ss.Length >= 2 &&
ss[0] == 0xFE && ss[1] == 0xFF)
{//UTF-16 Big Endian FE FF
return System.Text.Encoding.BigEndianUnicode;
}
else if (ss.Length >= 2 &&
ss[0] == 0xFF && ss[1] == 0xFE)
{//UTF-16 Little Endian FF FE
if (ss.Length >= 4 && ss[2] == 0x00 && ss[3] == 0x00)
{//UTF-32 Little Endian FF FE 00 00
return System.Text.Encoding.UTF32;
}
else
{//UTF-16 Little Endian FF FE
return System.Text.Encoding.Unicode;
}
}
//else if (ss.Length >= 4 &&
// ss[0] == 0x00 && ss[1] == 0x00 && ss[2] == 0xFE && ss[3] == 0xFF)
//{//UTF-32 Big Endian 00 00 FE FF
// return System.Text.Encoding.UTF32; //没有找到?
//}
else
{//无BOM和其他编码
return GetNoBomType(ss, ss.Length);
}
}
/// <summary>
/// 针对无BOM的内容做判断,不是分准确--主要是有可能没分析所有的字节
/// </summary>
/// <param name="buf"></param>
/// <param name="len"></param>
/// <returns></returns>
public static System.Text.Encoding GetNoBomType(byte[] buf, int len)
{
//UNICODE UTF-8
//00000000 - 0000007F 0xxxxxxx
//00000080 - 000007FF 110xxxxx 10xxxxxx
//00000800 - 0000FFFF 1110xxxx 10xxxxxx 10xxxxxx
//00010000 - 001FFFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
//00200000 - 03FFFFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
//04000000 - 7FFFFFFF 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
for (int i = 0; i < len; i++)
{
////////////////////////////////////////////////
//另一种判断方法,只判断2个或3个字节的utf8,只要找到一个符合的就返回了
//0xf0 1111 0000
//0xe0 1110 0000
//0xc0 1100 0000
//0x80 1000 0000
if (buf[i] < 0x80)
{//0xxxxxxx
//ascii字符 或 一个字节的utf8
}
else if (buf.Length >= i + 2 && //有后续的字节
(buf[i] & 0xE0) == 0xC0 //110xxxxx
&& (buf[i + 1] & 0xC0) == 0x80) //10xxxxxx
{//110xxxxx 10xxxxxx
return System.Text.Encoding.UTF8; //2个字节的utf8
}
else if (buf.Length >= i + 3 && //有后续的字节
(buf[i] & 0xF0) == 0xE0 //1110xxxx
&& (buf[i + 1] & 0xC0) == 0x80 //10xxxxxx
&& (buf[i + 2] & 0xC0) == 0x80) //10xxxxxx
{//1110xxxx 10xxxxxx 10xxxxxx
return System.Text.Encoding.UTF8; //3个字节的utf8
}
else if (buf.Length >= i + 4 && //有后续的字节
(buf[i] & 0xF8) == 0xF0 //11110xxx
&& (buf[i + 1] & 0xC0) == 0x80 //10xxxxxx
&& (buf[i + 2] & 0xC0) == 0x80 //10xxxxxx
&& (buf[i + 3] & 0xC0) == 0x80) //10xxxxxx
{//11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
return System.Text.Encoding.UTF8; //4个字节的utf8
}
else if (buf.Length >= i + 5 && //有后续的字节
(buf[i] & 0xFC) == 0xF8 //111110xx
&& (buf[i + 1] & 0xC0) == 0x80 //10xxxxxx
&& (buf[i + 2] & 0xC0) == 0x80 //10xxxxxx
&& (buf[i + 3] & 0xC0) == 0x80 //10xxxxxx
&& (buf[i + 4] & 0xC0) == 0x80) //10xxxxxx
{//111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
return System.Text.Encoding.UTF8; //5个字节的utf8
}
else if (buf.Length >= i + 6 && //有后续的字节
(buf[i] & 0xFE) == 0xFC //1111110x
&& (buf[i + 1] & 0xC0) == 0x80 //10xxxxxx
&& (buf[i + 2] & 0xC0) == 0x80 //10xxxxxx
&& (buf[i + 3] & 0xC0) == 0x80 //10xxxxxx
&& (buf[i + 4] & 0xC0) == 0x80 //10xxxxxx
&& (buf[i + 5] & 0xC0) == 0x80) //10xxxxxx
{//1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
return System.Text.Encoding.UTF8; //6个字节的utf8
}
else
return System.Text.Encoding.Default;
////////////////////////////////////////////////
}
return System.Text.Encoding.Default;
}
}
public class Student : IComparable
{
public int num;
public string orinum;
public string oriname;
public bool sex; //boy: true, girl: false
public int bannum;
public int CompareTo(object obj)
{
Student p = obj as Student;
return this.oriname.CompareTo(p.oriname);
}
};
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/zhyuey/SchoolTool.git
git@gitee.com:zhyuey/SchoolTool.git
zhyuey
SchoolTool
SchoolTool
master

搜索帮助