<script>
$(function () {
$("#Attachment").change(function () {
var att = $("#Attachment").val();
$("#hid-Attachment").val(att);
});
})
</script>
@using (Html.BeginForm("ImportExcel", "ProductOrder", FormMethod.Post, new { id = "ff", enctype = "multipart/form-data" }))
{
<div class="easyui-panel" title="受理工单" style="width: auto;">
<div style="padding: 10px 0 10px 400px">
<table style="text-align: center;">
<tr>
<td>上传附件:</td>
<td colspan="">
<input type="file" name="Attachment" id="Attachment" />
<input type="hidden" name="hid-Attachment" id="hid-Attachment" />
</td>
</tr>
</table>
</div>
<div style="text-align: center; padding: 5px 220px 10px 250px">
<a href="javascript:void(0)" class="easyui-linkbutton" onclick="window.history.go(-1)">返回</a>
<a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitForm()" id="submit">按指定方式处理</a>
<input type="submit" value="提交" />
</div>
</div>

  public ActionResult ImportExcel(HttpPostedFileBase Attachment)
{
if (!string.IsNullOrEmpty(Request["hid-Attachment"]))
{
string path = this.Server.MapPath(@"../Uploads");
string fileName = Path.GetFileName(Attachment.FileName); uploadInfo.ContentLength = Attachment.ContentLength;
uploadInfo.FileName = fileName;
uploadInfo.UploadedLength = ; uploadInfo.IsReady = true; int bufferSize = ;
byte[] buffer = new byte[bufferSize]; using (FileStream fs = new FileStream(Path.Combine(path, fileName), FileMode.Create))
{
while (uploadInfo.UploadedLength < uploadInfo.ContentLength)
{
int bytes = Attachment.InputStream.Read(buffer, , bufferSize);
fs.Write(buffer, , bytes);
uploadInfo.UploadedLength += bytes;
}
}
string file = Path.Combine(path, fileName);
string result = string.Empty;
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + file + "; " + "Extended Properties=Excel 8.0;";
OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet, "ProductOrder");
System.Data.DataTable tab = myDataSet.Tables["ProductOrder"].DefaultView.ToTable();
// ...用foreach把tab中数据添加到数据库 省略了如果是多表插入,可以调用存储过程.呵呵
foreach (DataRow dr in tab.Rows)
{
if (!string.IsNullOrEmpty(dr["商品号"].ToString()))
{
ProductOrder model = new ProductOrder()
{
CreateTime = dr["日期"].ToString(),
CustomerServices = dr["接待客服"].ToString(),
Customer = dr["顾客姓名"].ToString(),
Address = dr["联系地址"].ToString(),
TEL = dr["联系电话"].ToString(),
ProductName = dr["所购商品"].ToString(),
Quantity = dr["数量"].ToString(),
Price = dr["单价"].ToString(),
Unit = dr["单位"].ToString(),
TotalPrice = dr["总价"].ToString(),
PayoutStatus = dr["支付情况"].ToString(),
OrderId = dr["商品号"].ToString(),
Sim = dr["sim卡号"].ToString(),
SimStatus = dr["sim卡开通情况"].ToString(),
LogisticsId = dr["物流单号"].ToString(),
ReceivingStatus = dr["收货情况"].ToString(),
CollectionStatus = dr["收款情况"].ToString(),
};
db.ProductOrders.Add(model);
db.SaveChanges();
}
else
{
break;
} }
#region MyRegion
//using (System.Data.SqlClient.SqlBulkCopy bcp = new System.Data.SqlClient.SqlBulkCopy("Data Source=.;Initial Catalog=WorkOrder;Integrated Security=True;MultipleActiveResultSets=True"))
//{
// bcp.ColumnMappings.Add("日期", "CreateTime");
// bcp.ColumnMappings.Add("接待客服", "CustomerServices");
// bcp.ColumnMappings.Add("顾客姓名", "Customer");
// bcp.ColumnMappings.Add("联系地址", "Address");
// bcp.ColumnMappings.Add("数量", "Quantity");
// bcp.ColumnMappings.Add("所购商品", "ProductName");
// bcp.ColumnMappings.Add("联系电话", "TEL");
// bcp.ColumnMappings.Add("支付情况", "PayoutStatus");
// bcp.ColumnMappings.Add("商品号", "OrderId");
// bcp.ColumnMappings.Add("sim卡号", "Sim");
// bcp.ColumnMappings.Add("sim卡开通情况", "SimStatus");
// bcp.ColumnMappings.Add("物流单号", "LogisticsId");
// bcp.ColumnMappings.Add("收货情况", "ReceivingStatus");
// bcp.ColumnMappings.Add("收款情况", "CollectionStatus");
// bcp.BatchSize = 100;//每次传输的行数
// bcp.NotifyAfter = 100;//进度提示的行数
// bcp.DestinationTableName = "ProductOrder";//目标表
// bcp.WriteToServer(tab);
//}
#endregion
#region MyRegion
//using (SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=WorkOrder;Integrated Security=True;MultipleActiveResultSets=True"))
//{
// try
// {
// connection.Open();
// //给表名加上前后导符
// using (var bulk = new SqlBulkCopy(connection, SqlBulkCopyOptions.KeepIdentity, null)
// {
// DestinationTableName = "ProductOrder",
// BatchSize = 10000
// })
// {
// //循环所有列,为bulk添加映射
// //dataTable.EachColumn(c => bulk.ColumnMappings.Add(c.ColumnName, c.ColumnName), c => !c.AutoIncrement);
// foreach (DataColumn dc in tab.Columns)
// {
// bulk.ColumnMappings.Add(dc.ColumnName, dc.ColumnName);
// }
// bulk.WriteToServer(tab);
// bulk.Close();
// }
// return View();
// }
// catch (Exception exp)
// {
// return View();
// }
//}
#endregion return RedirectToAction("List");
result = "导入成功!";
JsonResult json = new JsonResult();
json.Data = result;
return json;
} return View();
}

mvc 导入excel表格的更多相关文章

  1. ASP.NET MVC导入excel到数据库

    MVC导入excel和webform其实没多大区别,以下为代码: 视图StationImport.cshtml的代码: @{ ViewBag.Title = "StationImport&q ...

  2. php导入excel表格

    我们做网站的时候经常要用到excel导入和导出的功能,我们通常的做法是用phpexcel工具包来完成,具体方法如下: html代码: <form action="{:U('Mall/u ...

  3. phpexcel导入excel表格

    html代码 <form action="{:U('Mall/updExcel')}" method="POST" enctype="multi ...

  4. 怎样把Word文档导入Excel表格

    Word是现在办公中的基础文件格式了,很多的内容我们都通过Word来进行编辑,那么当我们需要将Word文档里的信息导入到Excel里面的时候,我们应该怎样做呢?下面我们就一起来看一下吧. 操作步骤: ...

  5. 第三次作业--导入excel表格(完整版)

    031302322 031302316 将教师排课表导入系统 使用powerdesigner设计数据库表格 设计概念模型 打开new -> Conceptual Data Model创建概念模型 ...

  6. 【tp5.1】通过PHPExcel实现导入excel表格

    1.上github下载PHPExcel,链接:https://github.com/PHPOffice/PHPExcel 2.下载解压后,将Classes改名为PHPExcel如图 3.将文件夹复制到 ...

  7. 导入Excel表格(二)

    1. 提取session中的数据.并进行分页操作,上传excel表格,保存到临时表格. 初始化临时表格,提交表单,判断状态是否为真,若为真,则启用 导入到数据库 的按钮:为false,让查询的url ...

  8. 前端JS实现一键导入excel表格

    前面的文章中已经讲过关于js表格的导出,此文章主要说到的是excel文件如何导入到网页中,并在网页端显示. 代码部分: <!DOCTYPE html> <html> <h ...

  9. MySQL中导入Excel表格中的数据

    在数据库中建立好响应的数据库.表(参考excel表格中列中的名字和内容): 将excel表格另存为txt文件,选择“文本文件(制表符分割)”: 打开相应的txt文件,只留下要导入的数据(windows ...

随机推荐

  1. js对话框5秒自动消失

    使用了easyui的对话框控件 <html> <head> <title>5秒后关闭对话框</title> <meta http-equiv=&q ...

  2. x64 PL/SQL 连接 Oralce 提示 Could not initialize oci.dll

    在 x64 的 Win10 上重新安装了 Oralce 后,通过 PL/SQL 连接数据库时,提示如下错误信息 环境 windows7 64bit Oracle win64 11gR2 PL/SQL ...

  3. OAF_开发系列04_实现OAF查询4种不同的实现方式的比较和实现(案例)

    2014-06-02 Created By BaoXinjian

  4. [转载]给IT人员支招:如何跟业务部门谈需求分析?

    一提跟业务人员做“需求分析”,许多IT人员立刻就头大了,要么不在同一个“频道”讲话,要么“变来变去,定不下来”.如何跟业务部门谈需求分析呢,我们带着这个问题,与聚冠因尚的咨询顾问杨春波展开了讨论. 1 ...

  5. osgi笔记

    Bundle-Classpath可以实现内嵌jar. 一个Bundle的Activator不需要进行Export 一个Package中的类被两个ClassLoader加载,包中的Private cla ...

  6. Hololens开发笔记之Gesture手势识别(Manipulation手势控制物体旋转)

    Manipulation gesture:保持点击手势,在3D世界中绝对运动 当你想要全息图像1:1响应用户手部移动时,操纵手势能被用于移动.缩放或旋转全息图像.如此的一个用处是使得用户可以在世界中绘 ...

  7. 解压版Tomcat配置

    解压版Tomcat配置(本例Tomcat6):   一 配置Tomcat 1 下载Tomcat Zip压缩包,解压.      如果增加tomcat的用户名和密码,则修改/conf/tomcat-us ...

  8. npm命令ionic安装失败cordova安装失败解决方法

    转载:http://bbs.phonegap100.com/thread-2622-1-1.html 镜像使用方法(三种办法任意一种都能解决问题,建议使用第三种,将配置写死,下次用的时候配置还在): ...

  9. 拥抱高效、拥抱 Bugtags 之来自用户的声音(三)

    小编按:这是一篇 Bugtags 用户来稿,主要是介绍了使用 Bugtags 前后对测试及解决 Bug 所带来的变化,感谢山西农业大学 - 高正炎同学对 Bugtags 的信赖和支持.小编在这里诚邀各 ...

  10. VM12.1.1 下载 序列号

    VF5XA-FNDDJ-085GZ-4NXZ9-N20E6UC5MR-8NE16-H81WY-R7QGV-QG2D8ZG1WH-ATY96-H80QP-X7PEX-Y30V4AA3E0-0VDE1-0 ...