private
void
btnDaoRu_ServerClick(
object
sender, System.EventArgs e)
... {
IndPosInsEO model = new IndPosInsEO();
IndPosInsBO indBO = new IndPosInsBO();
DataSet ds = new DataSet();
string sArgs ;
if(this.fileDaoRu.Value != "")
...{
//调用下面的方法
ds = ReadExcelFile();
}
else
...{
sArgs = @"<script language=javascript>window.alert( '请选择文件!' );</script>";
Page.Response.Write( sArgs );
return ;
}
try
...{
for(int i = 0 ;i < ds.Tables[0].DefaultView.Count ;i ++)
...{
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["职位说明书ID"]))
model.ID = 0 ;
else
model.ID = Convert.ToInt32(ds.Tables[0].Rows[i]["职位说明书ID"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["部门名称"]))
model.DepName = "";
else
model.DepName = Convert.ToString(ds.Tables[0].Rows[i]["部门名称"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["职位名称"]))
model.PosName = "" ;
else
model.PosName = Convert.ToString(ds.Tables[0].Rows[i]["职位名称"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["职位系列"]))
model.PosSeries = "" ;
else
model.PosSeries = Convert.ToString(ds.Tables[0].Rows[i]["职位系列"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["直接上级职位名称"]))
model.HigherPos = "";
else
model.HigherPos = Convert.ToString(ds.Tables[0].Rows[i]["直接上级职位名称"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["直接下级职位名称"]))
model.LowerPos = "";
else
model.LowerPos = Convert.ToString(ds.Tables[0].Rows[i]["直接下级职位名称"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["直接下级人数"]))
model.LowerPosNum = "";
else
model.LowerPosNum = Convert.ToString(ds.Tables[0].Rows[i]["直接下级人数"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["编写时间"]))
model.MakeTime = "";
else
model.MakeTime = Convert.ToString(ds.Tables[0].Rows[i]["编写时间"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["工作地点"]))
model.Location = "";
else
model.Location = Convert.ToString(ds.Tables[0].Rows[i]["工作地点"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["职责目的"]))
model.RespSummary = "";
else
model.RespSummary = Convert.ToString(ds.Tables[0].Rows[i]["职责目的"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["对内联系部门"]))
model.Inner = "";
else
model.Inner = Convert.ToString(ds.Tables[0].Rows[i]["对内联系部门"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["对外联系部门"]))
model.Outer = "";
else
model.Outer = Convert.ToString(ds.Tables[0].Rows[i]["对外联系部门"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["描述"]))
model.Decision = "";
else
model.Decision = Convert.ToString(ds.Tables[0].Rows[i]["描述"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["教育程度"]))
model.Education = "";
else
model.Education = Convert.ToString(ds.Tables[0].Rows[i]["教育程度"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["相关工作经验要求"]))
model.JobExperience = "";
else
model.JobExperience = Convert.ToString(ds.Tables[0].Rows[i]["相关工作经验要求"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["基本技能"]))
model.Technology = "";
else
model.Technology = Convert.ToString(ds.Tables[0].Rows[i]["基本技能"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["必备专业知识与基础技能"]))
model.Knowledge = "";
else
model.Knowledge = Convert.ToString(ds.Tables[0].Rows[i]["必备专业知识与基础技能"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["管理素质和能力"]))
model.Management = "";
else
model.Management = Convert.ToString(ds.Tables[0].Rows[i]["管理素质和能力"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["职位相关素质能力"]))
model.PosAbility = "";
else
model.PosAbility = Convert.ToString(ds.Tables[0].Rows[i]["职位相关素质能力"]);
indBO.Add(model);
}
File.Delete(filePath);
sArgs = @"<script language=javascript>window.alert( '成功导入数据!' );</script>";
Page.Response.Write( sArgs );
return;
}
catch
...{
sArgs = @"<script language=javascript>window.alert( '文件正在使用无法导入!' );</script>";
Page.Response.Write( sArgs );
return;
}
}
private DataSet ReadExcelFile()
... {
string sArgs ;
filePath = "" ;
string fileExtend = "" ;//文件扩展名
int fileSize = 0 ;//文件大小
filePath = this.fileDaoRu.PostedFile.FileName.ToLower().Trim();
//取得上传前的文件(存在于客户端)的文件或文件夹的名称,组成数组.例如:C:aaaa.txt,那么Names.lenth就为3
string[] names = filePath.Split('/');
//取得文件名
string name = names[names.Length - 1];
string serverPath = this.Server.MapPath("../../");//获得服务器端的根目录
//判断是否有该目录
if(!Directory.Exists(serverPath+" ile"))
...{
Directory.CreateDirectory(serverPath+"file");
serverPath = serverPath+"file";
}
filePath = serverPath +"/"+name;
//如果存在,删除文件
if(File.Exists(filePath))
...{
File.Delete(filePath);
}
// 上传文件
this.fileDaoRu.PostedFile.SaveAs(filePath);
//得到文件的大小
fileSize = this.fileDaoRu.PostedFile.ContentLength;
//得到扩展名
fileExtend = filePath.Substring(filePath.IndexOf("."));
if(fileSize == 0 )
...{
sArgs = @"<script language=javascript>window.alert( '找不到该文件!' );</script>";
Page.Response.Write( sArgs );
return null;
}
if(fileExtend != ".xls")
...{
sArgs = @"<script language=javascript>window.alert( '请确认您所导入的文件是否EXCEL文件!!' );</script>";
Page.Response.Write( sArgs );
return null;
}
string sFileName = "" ;//文件存在的路径
string [] cFileName = filePath.Split('/');//取得上传后的文件(存在于服务器端)的文件或文件夹的名称,组成数组.例如:C:aaaa.txt,那么Names.lenth就为3
DataSet ds = new DataSet();
for(int i = 0 ;i < cFileName.Length ;i ++)
...{
if(sFileName == "")
...{
sFileName = cFileName[i];
}
else
...{
sFileName = sFileName + "/" + "/" + cFileName[i];
}
}
try
...{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source="+sFileName);
if(con.State != ConnectionState.Open)
...{
con.Open();
}
else
...{
sArgs = @"<script language=javascript>window.alert( 'EXCEL文件正在使用!' );</script>";
Page.Response.Write( sArgs );
return null;
}
OleDbCommand cmd = new OleDbCommand(@"select * from [Sheet1$]",con);
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cmd ;
da.Fill(ds);
con.Close();
}
catch(Exception ex)
...{
throw new Exception(ex.Message);
}
return ds ;
}
... {
IndPosInsEO model = new IndPosInsEO();
IndPosInsBO indBO = new IndPosInsBO();
DataSet ds = new DataSet();
string sArgs ;
if(this.fileDaoRu.Value != "")
...{
//调用下面的方法
ds = ReadExcelFile();
}
else
...{
sArgs = @"<script language=javascript>window.alert( '请选择文件!' );</script>";
Page.Response.Write( sArgs );
return ;
}
try
...{
for(int i = 0 ;i < ds.Tables[0].DefaultView.Count ;i ++)
...{
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["职位说明书ID"]))
model.ID = 0 ;
else
model.ID = Convert.ToInt32(ds.Tables[0].Rows[i]["职位说明书ID"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["部门名称"]))
model.DepName = "";
else
model.DepName = Convert.ToString(ds.Tables[0].Rows[i]["部门名称"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["职位名称"]))
model.PosName = "" ;
else
model.PosName = Convert.ToString(ds.Tables[0].Rows[i]["职位名称"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["职位系列"]))
model.PosSeries = "" ;
else
model.PosSeries = Convert.ToString(ds.Tables[0].Rows[i]["职位系列"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["直接上级职位名称"]))
model.HigherPos = "";
else
model.HigherPos = Convert.ToString(ds.Tables[0].Rows[i]["直接上级职位名称"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["直接下级职位名称"]))
model.LowerPos = "";
else
model.LowerPos = Convert.ToString(ds.Tables[0].Rows[i]["直接下级职位名称"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["直接下级人数"]))
model.LowerPosNum = "";
else
model.LowerPosNum = Convert.ToString(ds.Tables[0].Rows[i]["直接下级人数"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["编写时间"]))
model.MakeTime = "";
else
model.MakeTime = Convert.ToString(ds.Tables[0].Rows[i]["编写时间"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["工作地点"]))
model.Location = "";
else
model.Location = Convert.ToString(ds.Tables[0].Rows[i]["工作地点"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["职责目的"]))
model.RespSummary = "";
else
model.RespSummary = Convert.ToString(ds.Tables[0].Rows[i]["职责目的"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["对内联系部门"]))
model.Inner = "";
else
model.Inner = Convert.ToString(ds.Tables[0].Rows[i]["对内联系部门"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["对外联系部门"]))
model.Outer = "";
else
model.Outer = Convert.ToString(ds.Tables[0].Rows[i]["对外联系部门"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["描述"]))
model.Decision = "";
else
model.Decision = Convert.ToString(ds.Tables[0].Rows[i]["描述"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["教育程度"]))
model.Education = "";
else
model.Education = Convert.ToString(ds.Tables[0].Rows[i]["教育程度"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["相关工作经验要求"]))
model.JobExperience = "";
else
model.JobExperience = Convert.ToString(ds.Tables[0].Rows[i]["相关工作经验要求"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["基本技能"]))
model.Technology = "";
else
model.Technology = Convert.ToString(ds.Tables[0].Rows[i]["基本技能"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["必备专业知识与基础技能"]))
model.Knowledge = "";
else
model.Knowledge = Convert.ToString(ds.Tables[0].Rows[i]["必备专业知识与基础技能"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["管理素质和能力"]))
model.Management = "";
else
model.Management = Convert.ToString(ds.Tables[0].Rows[i]["管理素质和能力"]);
if(Convert.IsDBNull(ds.Tables[0].Rows[i]["职位相关素质能力"]))
model.PosAbility = "";
else
model.PosAbility = Convert.ToString(ds.Tables[0].Rows[i]["职位相关素质能力"]);
indBO.Add(model);
}
File.Delete(filePath);
sArgs = @"<script language=javascript>window.alert( '成功导入数据!' );</script>";
Page.Response.Write( sArgs );
return;
}
catch
...{
sArgs = @"<script language=javascript>window.alert( '文件正在使用无法导入!' );</script>";
Page.Response.Write( sArgs );
return;
}
}
private DataSet ReadExcelFile()
... {
string sArgs ;
filePath = "" ;
string fileExtend = "" ;//文件扩展名
int fileSize = 0 ;//文件大小
filePath = this.fileDaoRu.PostedFile.FileName.ToLower().Trim();
//取得上传前的文件(存在于客户端)的文件或文件夹的名称,组成数组.例如:C:aaaa.txt,那么Names.lenth就为3
string[] names = filePath.Split('/');
//取得文件名
string name = names[names.Length - 1];
string serverPath = this.Server.MapPath("../../");//获得服务器端的根目录
//判断是否有该目录
if(!Directory.Exists(serverPath+" ile"))
...{
Directory.CreateDirectory(serverPath+"file");
serverPath = serverPath+"file";
}
filePath = serverPath +"/"+name;
//如果存在,删除文件
if(File.Exists(filePath))
...{
File.Delete(filePath);
}
// 上传文件
this.fileDaoRu.PostedFile.SaveAs(filePath);
//得到文件的大小
fileSize = this.fileDaoRu.PostedFile.ContentLength;
//得到扩展名
fileExtend = filePath.Substring(filePath.IndexOf("."));
if(fileSize == 0 )
...{
sArgs = @"<script language=javascript>window.alert( '找不到该文件!' );</script>";
Page.Response.Write( sArgs );
return null;
}
if(fileExtend != ".xls")
...{
sArgs = @"<script language=javascript>window.alert( '请确认您所导入的文件是否EXCEL文件!!' );</script>";
Page.Response.Write( sArgs );
return null;
}
string sFileName = "" ;//文件存在的路径
string [] cFileName = filePath.Split('/');//取得上传后的文件(存在于服务器端)的文件或文件夹的名称,组成数组.例如:C:aaaa.txt,那么Names.lenth就为3
DataSet ds = new DataSet();
for(int i = 0 ;i < cFileName.Length ;i ++)
...{
if(sFileName == "")
...{
sFileName = cFileName[i];
}
else
...{
sFileName = sFileName + "/" + "/" + cFileName[i];
}
}
try
...{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source="+sFileName);
if(con.State != ConnectionState.Open)
...{
con.Open();
}
else
...{
sArgs = @"<script language=javascript>window.alert( 'EXCEL文件正在使用!' );</script>";
Page.Response.Write( sArgs );
return null;
}
OleDbCommand cmd = new OleDbCommand(@"select * from [Sheet1$]",con);
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cmd ;
da.Fill(ds);
con.Close();
}
catch(Exception ex)
...{
throw new Exception(ex.Message);
}
return ds ;
}