@using (Html.BeginForm("Student", "Excel", FormMethod.Post, new { enctype = "multipart/form-data" }))
{ <div style="margin-top: 20px;">
<fieldset id="myfieldset1">
<legend>学生信息导入</legend>
<p>
选择文件:<input id="FileUpload1" type="file" name="files" style="width: 250px; height: 24px;
background: White" class="easyui-validatebox" /></p>
<p>
<input id="btnImport1" type="submit" value="导入" style="width: 60px; height: 28px;" /></p>
<p style="color: Red; text-align: center;">@ViewBag.errorstu</p>
</fieldset>
</div>
}
     public class ExcelController : Controller
{
ContractInfoBLL contract;
TeacherInfoBLL TIBLL;
ParentBLL PBLL;
public ExcelController()
{
if (contract == null)
{
contract = new ContractInfoBLL();
}
if (TIBLL == null)
{
TIBLL = new TeacherInfoBLL();
}
if (PBLL == null)
{
PBLL = new ParentBLL();
}
}
public ActionResult Index(bool Code = true, string Message="")
{
if (!Code)
{
ViewBag.Message = Message;
}
return View();
}
string StuExcel = ApiHelper.GetWebConfig("StuExcel", @"E:/需求/20160520 家宝贝/新建文件夹/学生用户.xlsx");
/// <summary>
/// Excel学生用户导入
/// </summary>
/// <returns></returns>
[HttpPost]
public ActionResult Student(HttpPostedFileBase filebase)
{
#region
HttpPostedFileBase file = Request.Files["files"];
string FileName;
string savePath;
if (file == null || file.ContentLength <= )
{
ViewBag.error = "文件不能为空";
return View();
}
else
{
string filename = Path.GetFileName(file.FileName);
int filesize = file.ContentLength;//获取上传文件的大小单位为字节byte
string fileEx = System.IO.Path.GetExtension(filename);//获取上传文件的扩展名
string NoFileName = System.IO.Path.GetFileNameWithoutExtension(filename);//获取无扩展名的文件名
int Maxsize = * ;//定义上传文件的最大空间大小为4M
string FileType = ".xls,.xlsx";//定义上传文件的类型字符串 FileName = NoFileName + DateTime.Now.ToString("yyyyMMddhhmmss") + fileEx;
if (!FileType.Contains(fileEx))
{
ViewBag.error = "文件类型不对,只能导入xls和xlsx格式的文件";
return View();
}
if (filesize >= Maxsize)
{
ViewBag.error = "上传文件超过4M,不能上传";
return View();
}
string path = AppDomain.CurrentDomain.BaseDirectory + "App_Data/";
savePath = Path.Combine(path, FileName);
file.SaveAs(savePath);
}
#endregion
List<Student_Excel> stuList = new ExcelBLL().GetStudentByExcel(savePath);
string Meg = "";
int ItemSchool = ;
int classID = ;
int i = ;
foreach (var item in stuList)
{ bool ishavecode = contract.IsHaveContractCode(item.ContractCode);
if (!ishavecode)
{
i++;
Meg = "学籍号重复";
LoggerFactory.Instance.Logger_Info("学生用户_序号" + item.Number + "_学籍号:" + item.ContractCode + "_学生姓名:" + item.Name + "_原因:" + Meg);
}
else
{
ItemSchool = contract.GetGardenByName(item.ItemSchoolName);
classID = contract.GetClassInfoID(ItemSchool, item.ClassName);
if (classID != )
{
DateTime da = DateTime.Now;
ContractInfoList model = new ContractInfoList();
model.ContractCode = item.ContractCode;
model.ContractStatus = (int)ContractStatus.Normal;
model.AdmissionsTeacherID = contract.GetTeacherIDbyName(item.TeacherName);
model.StudentName = item.Name;
model.StudentGender = item.Sex == "男" ? : ;
model.ParentName = item.ParentName;
model.ParenRelation = item.ParenRelation;
model.ParentTel = item.Phone;
model.EntranceTime = item.EntranceTime.HasValue ? item.EntranceTime.Value : DateTime.Now;
model.PaymentAmount = item.PaymentAmount != "" ? Convert.ToInt32(item.PaymentAmount) : ;
model.ItemSchool = ItemSchool;
model.AddTime = da;
model.UpdateTime = da;
model.Remarks = "";
model.ClassInfoID = classID; bool boo = contract.IsCreateContract(model);
if (!boo)
{
i++;
Meg = "创建失败";
LoggerFactory.Instance.Logger_Info("学生用户_序号" + item.Number + "_学籍号:" + item.ContractCode + "_学生姓名:" + item.Name + "_原因:" + Meg);
}
}
else
{
i++;
Meg = "班级不存在";
LoggerFactory.Instance.Logger_Info("学生用户_序号" + item.Number + "_学籍号:" + item.ContractCode + "_学生姓名:" + item.Name + "_原因:" + Meg);
}
}
} return Json(new { Code = Meg == "" ? true : false, Message = Meg != "" ? "未导入数据:" + i + "条" : "" });
//ViewBag.errorstu = "导入成功";
//return RedirectToAction("Index");
} }
     public class ExcelBLL
{
public List<Student_Excel> GetStudentByExcel(string path)
{
List<Student_Excel> stuList = new List<Student_Excel>();
DataSet ds = Helper.GetDataSet("select * from [Sheet1$]", path);
DataTable dt = ds.Tables[];//忽略第一行表名,从第二行开始
foreach (DataRow row in dt.Rows)
{
if (row["序号"].ToString()!="")
{
DateTime ad = new DateTime();
if (row["入学日期"].ToString() != "")
{
ad = Convert.ToDateTime(row["入学日期"]);
}
stuList.Add(new Student_Excel()
{
Number = row["序号"].ToString(),
ContractCode = row["学籍号"].ToString(),
Name = row["学生姓名"].ToString(),
Sex = row["性别"].ToString(),
ClassName = row["所在班级"].ToString(),
Phone = row["紧急联系电话"].ToString(),
ParenRelation = row["与孩子关系"].ToString(),
ParentName = row["家长姓名"].ToString(),
EntranceTime = ad,//Convert.ToDateTime(row["入学日期"]),
TeacherName = row["招生老师"].ToString(),
PaymentAmount = row["收费金额"].ToString(),
ItemSchoolName = row["所在园区"].ToString(),
});
}
} return stuList;
}
}
      public class Helper:BaseBLL
{
#region Excel 导入
private static string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0; HDR=yes; IMEX=2'"; public static DataSet GetDataSet(string sql, string path)
{
OleDbConnection conn = new OleDbConnection(string.Format(connstring, path));
OleDbCommand cmd = new OleDbCommand(sql, conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();//创建内存数据集
try
{
conn.Open();
da.Fill(ds);
return ds;
}
catch (Exception e)
{
//写日志 throw e;
}
finally
{
conn.Close();
}
}
#endregion
}

Excel 数据导入(OleDb)的更多相关文章

  1. C#使用oledb方式将excel数据导入到datagridview后数据被截断为 255 个字符

    问题描述:在使用oledb方式将excel数据导入到datagridview中,在datagridview单元格中的数据没有显示全,似乎只截取了数据源中的一段 解决方案:1.关于该问题,微软官方答案: ...

  2. c#将Excel数据导入到数据库的实现代码(OleDb)

    sing System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web ...

  3. 效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中【附源代码下载】) 转

    效率最高的Excel数据导入---(c#调用SSIS Package将数据库数据导入到Excel文件中[附源代码下载])    本文目录: (一)背景 (二)数据库数据导入到Excel的方法比较   ...

  4. excel 数据导入数据表

    环境: Windows server 2012  rm sql server  2012 excel 数据导入数据表 INSERT INTO [dbo].[AdminUser] SELECT [Adm ...

  5. excel数据导入到sqlserver中---------工作笔记

    调用页面: using System; using System.Collections.Generic; using System.Linq; using System.Web; using Sys ...

  6. c#将Excel数据导入到数据库的实现代码(转载)

    假如Excel中的数据如下:     数据库建表如下:     其中Id为自增字段:      代码如下: using System; using System.Collections.Generic ...

  7. C#将Excel数据导入数据库(MySQL或Sql Server)

    最近一直很忙,很久没写博客了.今天给大家讲解一下如何用C#将Excel数据导入Excel,同时在文章最后附上如何用sqlserver和mysql工具导入数据. 导入过程大致分为两步: 1.将excel ...

  8. C# Excel数据导入到数据库

    http://www.jb51.net/article/44743.htm 假如Excel中的数据如下: 数据库建表如下: 其中Id为自增字段: 代码: 复制代码 代码如下: using System ...

  9. 批量Excel数据导入Oracle数据库

    由于一直基于Oracle数据库上做开发,因此常常会需要把大量的Excel数据导入到Oracle数据库中,其实如果从事SqlServer数据库的开发,那么思路也是一样的,本文主要介绍如何导入Excel数 ...

  10. c#将Excel数据导入到数据库的实现代码

    这篇文章主要介绍了c#将Excel数据导入到数据库的实现代码,有需要的朋友可以参考一下 假如Excel中的数据如下: 数据库建表如下: 其中Id为自增字段: 代码: 代码如下: using Syste ...

随机推荐

  1. php多进程编程详解

    php多进程编程 前言 php单进程存在的问题: 多核处理器未充分利用,而单处理器通常需要等待其他操作完成之后才能再继续工作. 任何现代操作系统都可在幕后执行多任务,这意味着在很短时间内,计算机可以调 ...

  2. Java实现非递归删除目录

    最近在学C#的文件系统, 发现C#的文件系统貌似比java的东西少一点, 居然连删除目录都直接做好封装了, 想到学java的时候还要自己写递归删除, 好像没写过非递归的,就在网上查了下, 关于非递归删 ...

  3. 蓄水池抽样(原理&实现)

    前言: 蓄水池抽样:从N个元素中随机的等概率的抽取k个元素,其中N无法确定. 适用场景: 模式识别等概率抽样,抽样查看渐增的log日志(无法先保存整个数据流然后再从中选取,而是期望有一种将数据流遍历一 ...

  4. MCMC(三)MCMC采样和M-H采样

    MCMC(一)蒙特卡罗方法 MCMC(二)马尔科夫链 MCMC(三)MCMC采样和M-H采样 MCMC(四)Gibbs采样(待填坑) 在MCMC(二)马尔科夫链中我们讲到给定一个概率平稳分布$\pi$ ...

  5. Oracle存储过程的调用和执行

    1.什么是存储过程: 用于在数据库中完成特定的操作或者任务.是一个PLSQL程序块,可以永久的保存在数据库中以供其他程序调用. 2.无参存储过程的使用: Normal 0 7.8 磅 0 2 fals ...

  6. java做帐户登录失败锁定

    对于连续失败登录应用系统5次的帐号,需锁定该帐号至少30分钟不允许登录. 这里也用简单的map集合进行判定,功能能实现,但并不是很完美,不用更改数据库的表字段 1.首先建立一个用户登陆失败的实体类 p ...

  7. 简单的叙述下SQL中行列转换的小知识!

    行列转换对于工作还是学习中总是不可避免的会遇到(虽然本人还尚未工作,萌萌哒的学生一枚),解决的方法也有很多,我这里就总结一下我所想解决的问题以及怎么去解决的方法, 可能网上已经有很多类似的方法了,有的 ...

  8. 20155214 2016-2017-2 《Java程序设计》第5周学习总结

    20155214 2016-2017-2 <Java程序设计>第5周学习总结 教材学习内容总结 1.错误会被包装为可抛出的对象,继承自java.lang.Throwable类. 2.可以利 ...

  9. poptest老李谈分布式与集群

    poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:908821478,咨询电话010-845052 ...

  10. 老李推荐:第6章2节《MonkeyRunner源码剖析》Monkey原理分析-事件源-事件源概览-获取命令字串

    老李推荐:第6章2节<MonkeyRunner源码剖析>Monkey原理分析-事件源-事件源概览-获取命令字串   从上一节的描述可以知道,MonkeyRunner发送给Monkey的命令 ...