第一步
- <form enctype="multipart/form-data" id="file-form">
- <a id="btnMCToOut" onclick="importExcel()" class="easyui-linkbutton" data-options="iconCls:'icon_CloseAll'">导入Excel</a>
- <input type="file" name="filed" id="filed">
- </form>
第二步
- // 导入Excel
- function importExcel() {
- var file = $("#filed").val();
- if (file == null || file.length == 0) {
- DJMask.msg("请先选择上传文件!");
- return false;
- }
- $('#file-form').ajaxSubmit({
- type: 'POST', // HTTP请求方式
- url: '../Ajax/Handler1.ashx', // 请求的URL地址
- dataType: 'text', // 服务器返回数据转换成的类型
- success: function (data) {
- DJMask.msg(data);
- },
- error: function (data) {
- DJMask.msg("oh,似乎出现点问题了!");
- }
- });
- $('#dgMenCards').datagrid('reload', {});//刷新表格
- }
第三步
- public void ProcessRequest(HttpContext context)
- {
- context.Response.ContentType = "text/plain";
- HttpPostedFile filePost = context.Request.Files["filed"]; // 获取上传的文件
- string filePath = SaveExcelFile(filePost); // 保存文件并获取文件路径
- string msg= ExcelToDataTable(filePath, true);
- context.Response.Write(msg);
- }
第四步
- public void ProcessRequest(HttpContext context)
- {
- context.Response.ContentType = "text/plain";
- HttpPostedFile filePost = context.Request.Files["filed"]; // 获取上传的文件
- string filePath = SaveExcelFile(filePost); // 保存文件并获取文件路径
- string msg= ExcelToDataTable(filePath, true);
- context.Response.Write(msg);
- }
第五步
- /// <param name="filePath"></param>
- /// <param name="isColumnName"></param>
- public string ExcelToDataTable(string filePath, bool isColumnName)
- {
- int count = 0;
- try
- {
- DataTable dataTable = new DataTable();
- FileStream fs = null;
- IWorkbook workbook = null;
- ISheet sheet = null;
- using (fs = new FileStream(filePath, FileMode.Open))
- {
- if (filePath.IndexOf(".xlsx") > 0)
- {
- workbook = new XSSFWorkbook(fs);
- if (workbook != null)
- {
- sheet = workbook.GetSheetAt(0);
- if (sheet != null)
- {
- Models.MPMS_DBDataContext db = new Models.MPMS_DBDataContext();
- int rowCount = sheet.LastRowNum;
- for (int i = 1; i <= rowCount; i++)
- {
- Models.MemCards model = new Models.MemCards();
- IRow row = sheet.GetRow(i);
- model.CL_ID = int.Parse(row.GetCell(0).ToString());
- model.S_ID = int.Parse(row.GetCell(1).ToString());
- model.MC_CardID = (row.GetCell(2).ToString());//会员卡号
- model.MC_Password = (row.GetCell(3).ToString());//卡片密码
- model.MC_Name = (row.GetCell(4).ToString());//卡片密码
- model.MC_Sex = int.Parse(row.GetCell(5).ToString());//会员性别
- model.MC_Mobile = (row.GetCell(6).ToString());//手机号码
- model.MC_Photo = "upload/1.jpg";//靓照
- model.MC_Birthday_Month = 1;//会员生日--月
- model.MC_Birthday_Day = 1;//会员生日--日
- model.MC_BirthdayType = 1;//会员生日类型
- model.MC_IsPast = 1;//是否设置卡片过期时间
- model.MC_PastTime = DateTime.Now.AddYears(10);//卡片过期时间
- model.MC_Point = int.Parse(row.GetCell(7).ToString());//当前积分
- model.MC_Money = int.Parse(row.GetCell(8).ToString());//卡片付费
- model.MC_TotalMoney = 0;//累计消费
- model.MC_TotalCount = 0;//累计消费次数
- model.MC_State = 1;//卡片状态
- model.MC_IsPointAuto = 1;//积分是否可以自动换成等级
- model.MC_RefererID = null;//推荐人ID
- model.MC_RefererCard = null;//推荐人卡号
- model.MC_RefererName = null;//推荐人姓名
- model.MC_CreateTime = DateTime.Now;//积分是否可以自动换成等级
- count++;
- db.MemCards.InsertOnSubmit(model);
- db.SubmitChanges();
- }
- }
- }
- }
- }
- }
- catch{ return "导入失败,字段错误!"; }
- return "成功导入" +count+ "条数据";
- }