<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CusImport.aspx.cs" Inherits="XGHUNLIAN.admin.CusService.CusImport" %>
<%@ Import Namespace="XGHUNLIAN.Common" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>售前分配/调配会员</title>
<link href="../../css/style.css" rel="stylesheet" />
<script type="text/javascript" src="../../scripts/jquery/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="../../scripts/jquery/Validform_v5.3.2_min.js"></script>
<script type="text/javascript" src="../../js/layout.js"></script>
<script src="../../js/MyDatePicker/WdatePicker.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
//初始化表单验证
$("#form1").initValidform();
});
</script>
<style>
.userheadimg
{
width: 35px;
height: 35px;
overflow: hidden;
top: 10px;
left: 10px;
z-index: 2;
border-radius: 50px;
border: #fff solid 1px;
}
.vip
{
background: #ff5065;
font-size: .8em;
color: #fff;
padding: 2px 10px;
border-radius: 20px;
}
.toolbar .r-list .nl
{
display: table-cell;
vertical-align: inherit;
margin: 0;
padding: 0 5px;
width: 30px;
height: 30px;
line-height: 28px;
font-size: 12px;
border: 1px solid #dbdbdb;
color: #444;
}
</style>
</head>
<body id="weixin" style="background: #fff;">
<form id="form1" runat="server">
<asp:HiddenField ID="hd_id" runat="server" />
<asp:HiddenField ID="hd_type" runat="server" />
<div class="hd">
<h3>
<%=(Request.QueryString["Type"]=="0" ?"售前分配/调配会员":"售后分配/调配会员")%></h3>
</div>
<div class="bd" style="padding-top: 10px;">
<div class="content">
<div class="tab-content">
<dl style=" margin-bottom: 5px;">
<dt>请选择文件</dt>
<dd>
<asp:FileUpload ID="fu_select" runat="server"/></dd><dd><a href="../../templet/会员导入数据模板.xlsx" style="color:Red;">点击下载模板</a></dd>
</dl>
</div>
</div>
</div>
<div class="ft">
<div class="content">
<span class="button_a">
<asp:Button ID="btnSubmit" runat="server" Text="提交保存" OnClick="btnSubmit_Click" /></span>
</div>
</div>
</form>
<script src="../../js/layui/layui.all.js" type="text/javascript"></script>
<link href="../../js/layui/css/layui.css" rel="stylesheet" type="text/css" />
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XGHUNLIAN.Common;
using System.IO;
using System.Data;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
namespace XGHUNLIAN.admin.CusService
{
public partial class CusImport : Web.UI.ManagePage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Model.manager model = GetAdminInfo(); //取得当前管理员信息
if (model == null)
{ Response.Write("<script>top.parent.location.href='../login.aspx'</script>"); }
else
{
}
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
HttpPostedFile filePost = Request.Files[0]; // 获取上传的文件
string filePath = SaveExcelFile(filePost); // 保存文件并获取文件路径
string msg = ExcelToDataTable(filePath, true);
Response.Write(msg);
}
/// <summary>
/// 保存Excel文件
/// <para>Excel的导入导出都会在服务器生成一个文件</para>
/// <para>路径:UpFiles/ExcelFiles</para>
/// </summary>
/// <param name="file">传入的文件对象</param>
/// <returns>如果保存成功则返回文件的位置;如果保存失败则返回空</returns>
public static string SaveExcelFile(HttpPostedFile file)
{
try
{
var fileName = file.FileName.Insert(file.FileName.LastIndexOf('.'), "-" + DateTime.Now.ToString("yyyyMMddHHmmssfff"));
var filePath = Path.Combine(HttpContext.Current.Server.MapPath("~/Upload"), fileName);
string directoryName = Path.GetDirectoryName(filePath);
if (!Directory.Exists(directoryName))
{
Directory.CreateDirectory(directoryName);
}
file.SaveAs(filePath);
return filePath;
}
catch
{
return string.Empty;
}
}
/// <summary>
/// 上传读取文件
/// </summary>
/// <param name="filePath"></param>
/// <param name="isColumnName"></param>
public string ExcelToDataTable(string filePath, bool isColumnName)
{
int count = 0;
int errorCount = 0;
string errorRow = "";
try
{
BLL.wei_Member bll = new BLL.wei_Member();
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)
{
int rowCount = sheet.LastRowNum;
for (int i = 1; i <= rowCount; i++)
{
try
{
Model.wei_Member model = new Model.wei_Member();
IRow row = sheet.GetRow(i);
string love20 = "";
model.openid = "";
model.nickname = "";
model.zsxm = row.GetCell(0).ToString();
model.sfzh =row.GetCell(4).ToString();
model.webnickname = "";
model.mob = row.GetCell(3).ToString();
model.mobVisible = 0;
model.weixin = "";
model.QQ = "";
model.txdz = "";
model.sex = row.GetCell(1).ToString() == "男" ? 0 : 1;
model.csrq = Convert.ToDateTime(row.GetCell(5).ToString()).ToString("yyyy-MM-dd HH:mm:ss");;
model.nl = Convert.ToInt32(row.GetCell(2).ToString());
model.sg = Convert.ToInt32(row.GetCell(6).ToString());
model.tz = Convert.ToInt32(row.GetCell(7).ToString());
model.hyzk = "";
model.xl = "";
model.regtime = 0;
model.nlsx = 0;
model.nlxx = 0;
model.sgsx = 0;
model.sgxx = 0;
model.hsyq = "";
model.xlyq = "";
model.imgurl = "";
model.sx = "";
model.xx = "";
model.mudi = "";
model.ywzn = "";
model.szdq = "";
model.mz = "";
model.zy = "";
model.yx = "";
model.jzqk = "";
model.gcqk = "";
model.xqah = "";
model.jb = 0;
model.sendDate = 0;
model.llcs = 0;
model.dj = 0;
model.provinceid = 0;
model.province = "";
model.cityid = 0;
model.city = "";
model.isregmob = 0;
model.isregidcard = 0;
model.isemphn = 0;
model.ishidemsg = 0;
model.isdown = 0;
model.isreghn = 0;
model.svclass = 0;
model.svclassname = "";
model.isviewlimit = 0;
model.svcount = 0;
model.viewcount = 0;
model.storeid = (Session[XGHUNLIAN.Common.MXKeys.SESSION_ADMIN_INFO] as XGHUNLIAN.Model.manager).storeid;
model.love20 = love20;
if (bll.CheckIDCardIsExiste(0, model.sfzh))
{
errorCount++;
errorRow += i + "身份证号已经存在" + ",";
continue;
}
if (bll.CheckTelIsExiste(0, model.mob))
{
errorCount++;
errorRow += i + "电话号码已经存在" + ",";
continue;
}
if (bll.Add(model))
{
count++;
}
else {
errorCount++;
errorRow += i + ",";
}
}
catch (Exception ex)
{
errorCount++;
errorRow += i+ex.Message + ",";
}
}
}
}
}
}
}
catch { return "导入失败,字段错误!"; }
if (errorCount > 0)
{
return "成功导入" + count + "条数据,失败" + errorCount + " 失败的行为:" + errorRow;
}
else
{
return "成功导入" + count + "条数据,失败" + errorCount + "";
}
}
}
}