只需Ctrlc+ctrlv超级简单就能实现Npoi的导入,人类历史上.net,c#初级菜鸟学员的福利

     第一步

  1. <form enctype="multipart/form-data" id="file-form">  
  2.         <a id="btnMCToOut" onclick="importExcel()" class="easyui-linkbutton" data-options="iconCls:'icon_CloseAll'">导入Excel</a>  
  3.         <input type="file" name="filed" id="filed">  
  4. </form>  
  5. 第二步

  1. // 导入Excel  
  2. function importExcel() {  
  3.     var file = $("#filed").val();  
  4.     if (file == null || file.length == 0) {  
  5.         DJMask.msg("请先选择上传文件!");  
  6.         return false;  
  7.     }  
  8.     $('#file-form').ajaxSubmit({  
  9.         type: 'POST'// HTTP请求方式  
  10.         url: '../Ajax/Handler1.ashx'// 请求的URL地址  
  11.         dataType: 'text'// 服务器返回数据转换成的类型  
  12.         success: function (data) {  
  13.             DJMask.msg(data);  
  14.         },  
  15.         error: function (data) {  
  16.             DJMask.msg("oh,似乎出现点问题了!");  
  17.         }  
  18.     });  
  19.     $('#dgMenCards').datagrid('reload', {});//刷新表格  
  20. }  

第三步

  1. public void ProcessRequest(HttpContext context)  
  2. {  
  3.     context.Response.ContentType = "text/plain";  
  4.     HttpPostedFile filePost = context.Request.Files["filed"]; // 获取上传的文件  
  5.     string filePath = SaveExcelFile(filePost); // 保存文件并获取文件路径  
  6.     string msg= ExcelToDataTable(filePath, true);  
  7.     context.Response.Write(msg);  

第四步

  1. public void ProcessRequest(HttpContext context)  
  2. {  
  3.     context.Response.ContentType = "text/plain";  
  4.     HttpPostedFile filePost = context.Request.Files["filed"]; // 获取上传的文件  
  5.     string filePath = SaveExcelFile(filePost); // 保存文件并获取文件路径  
  6.     string msg= ExcelToDataTable(filePath, true);  
  7.     context.Response.Write(msg);  

第五步

  1. /// <param name="filePath"></param>  
  2. /// <param name="isColumnName"></param>  
  3. public string ExcelToDataTable(string filePath, bool isColumnName)  
  4. {  
  5.     int count = 0;  
  6.     try  
  7.     {  
  8.   
  9.   
  10.         DataTable dataTable = new DataTable();  
  11.         FileStream fs = null;  
  12.         IWorkbook workbook = null;  
  13.         ISheet sheet = null;  
  14.         using (fs = new FileStream(filePath, FileMode.Open))  
  15.         {  
  16.             if (filePath.IndexOf(".xlsx") > 0)  
  17.             {  
  18.                 workbook = new XSSFWorkbook(fs);  
  19.                 if (workbook != null)  
  20.                 {  
  21.                     sheet = workbook.GetSheetAt(0);  
  22.                     if (sheet != null)  
  23.                     {  
  24.                         Models.MPMS_DBDataContext db = new Models.MPMS_DBDataContext();  
  25.                         int rowCount = sheet.LastRowNum;  
  26.                         for (int i = 1; i <= rowCount; i++)  
  27.                         {  
  28.                             Models.MemCards model = new Models.MemCards();  
  29.                             IRow row = sheet.GetRow(i);  
  30.                             model.CL_ID = int.Parse(row.GetCell(0).ToString());  
  31.                             model.S_ID = int.Parse(row.GetCell(1).ToString());  
  32.                             model.MC_CardID = (row.GetCell(2).ToString());//会员卡号  
  33.   
  34.                             model.MC_Password = (row.GetCell(3).ToString());//卡片密码  
  35.                             model.MC_Name = (row.GetCell(4).ToString());//卡片密码  
  36.                             model.MC_Sex = int.Parse(row.GetCell(5).ToString());//会员性别  
  37.   
  38.                             model.MC_Mobile = (row.GetCell(6).ToString());//手机号码  
  39.                             model.MC_Photo = "upload/1.jpg";//靓照  
  40.   
  41.                             model.MC_Birthday_Month = 1;//会员生日--月  
  42.                             model.MC_Birthday_Day = 1;//会员生日--日  
  43.                             model.MC_BirthdayType = 1;//会员生日类型    
  44.   
  45.                             model.MC_IsPast = 1;//是否设置卡片过期时间      
  46.                             model.MC_PastTime = DateTime.Now.AddYears(10);//卡片过期时间    
  47.                             model.MC_Point = int.Parse(row.GetCell(7).ToString());//当前积分      
  48.                             model.MC_Money = int.Parse(row.GetCell(8).ToString());//卡片付费      
  49.                             model.MC_TotalMoney = 0;//累计消费    
  50.                             model.MC_TotalCount = 0;//累计消费次数      
  51.                             model.MC_State = 1;//卡片状态     
  52.                             model.MC_IsPointAuto = 1;//积分是否可以自动换成等级   
  53.   
  54.                             model.MC_RefererID = null;//推荐人ID     
  55.                             model.MC_RefererCard = null;//推荐人卡号   
  56.                             model.MC_RefererName = null;//推荐人姓名   
  57.   
  58.                             model.MC_CreateTime = DateTime.Now;//积分是否可以自动换成等级     
  59.                             count++;  
  60.                             db.MemCards.InsertOnSubmit(model);  
  61.                             db.SubmitChanges();  
  62.                         }  
  63.                     }  
  64.                 }  
  65.             }  
  66.         }  
  67.     }  
  68.     catchreturn "导入失败,字段错误!"; }  
  69.     return "成功导入" +count+ "条数据";  


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值