npoi 导入
<%@ 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: ;
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: ;
padding: 5px;
width: 30px;
height: 30px;
line-height: 28px;
font-size: 12px;
border: 1px solid #dbdbdb;
color: #;
}
</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"]=="" ?"售前分配/调配会员":"售后分配/调配会员")%></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[]; // 获取上传的文件
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 = ;
int errorCount = ;
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") > )
{
workbook = new XSSFWorkbook(fs);
if (workbook != null)
{
sheet = workbook.GetSheetAt();
if (sheet != null)
{
int rowCount = sheet.LastRowNum;
for (int i = ; 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().ToString();
model.sfzh =row.GetCell().ToString();
model.webnickname = "";
model.mob = row.GetCell().ToString();
model.mobVisible = ;
model.weixin = "";
model.QQ = "";
model.txdz = "";
model.sex = row.GetCell().ToString() == "男" ? : ;
model.csrq = Convert.ToDateTime(row.GetCell().ToString()).ToString("yyyy-MM-dd HH:mm:ss");;
model.nl = Convert.ToInt32(row.GetCell().ToString());
model.sg = Convert.ToInt32(row.GetCell().ToString());
model.tz = Convert.ToInt32(row.GetCell().ToString());
model.hyzk = "";
model.xl = "";
model.regtime = ;
model.nlsx = ;
model.nlxx = ;
model.sgsx = ;
model.sgxx = ;
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 = ;
model.sendDate = ;
model.llcs = ;
model.dj = ;
model.provinceid = ;
model.province = "";
model.cityid = ;
model.city = "";
model.isregmob = ;
model.isregidcard = ;
model.isemphn = ;
model.ishidemsg = ;
model.isdown = ; model.isreghn = ;
model.svclass = ;
model.svclassname = "";
model.isviewlimit = ;
model.svcount = ;
model.viewcount = ; model.storeid = (Session[XGHUNLIAN.Common.MXKeys.SESSION_ADMIN_INFO] as XGHUNLIAN.Model.manager).storeid; model.love20 = love20; if (bll.CheckIDCardIsExiste(, model.sfzh))
{
errorCount++;
errorRow += i + "身份证号已经存在" + ",";
continue;
} if (bll.CheckTelIsExiste(, 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 > )
{
return "成功导入" + count + "条数据,失败" + errorCount + " 失败的行为:" + errorRow;
}
else
{
return "成功导入" + count + "条数据,失败" + errorCount + "";
}
}
}
}
npoi 导入的更多相关文章
- c# .Net :Excel NPOI导入导出操作教程之读取Excel文件信息及输出
c# .Net :Excel NPOI导入导出操作教程之读取Excel文件信息及输出 using NPOI.HSSF.UserModel;using NPOI.SS.UserModel;using S ...
- NPOI导入导出EXCEL通用类,供参考,可直接使用在WinForm项目中
以下是NPOI导入导出EXCEL通用类,是在别人的代码上进行优化的,兼容xls与xlsx文件格式,供参考,可直接使用在WinForm项目中,由于XSSFWorkbook类型的Write方法限制,Wri ...
- c#.net 使用NPOI导入导出标准Excel (asp.net winform csharp)
尝试过很多Excel导入导出方法,都不太理想,无意中逛到oschina时,发现了NPOI,无需Office COM组件且不依赖Office,顿时惊为天人,怀着无比激动的心情写下此文. 曾使用过的方法 ...
- 使用NPOI导入导出标准的Excel
关于NPOI NPOI是POI项目的.NET版本,是由@Tony Qu(http://tonyqus.cnblogs.com/)等大侠基于POI开发的,可以从http://npoi.codeplex. ...
- ASP.NET- 使用NPOI导入导出标准Excel
尝试过很多Excel导入导出方法,都不太理想,无意中逛到oschina时,发现了NPOI,无需Office COM组件且不依赖Office,顿时惊为天人,怀着无比激动的心情写下此文. 曾使用过的方法 ...
- NPOI导入导出Excel
.net mvc利用NPOI导入导出excel 注意:如何导出的提交方式ajax导出是失效的! 解决方案是:js处理l两个表单的提交 代码: 第一步. 在页面里面加入2个隐藏的iframe, 如下 ...
- 使用NPOI导入导出标准Excel
尝试过很多Excel导入导出方法,都不太理想,无意中逛到oschina时,发现了NPOI,无需Office COM组件且不依赖Office,顿时惊为天人,怀着无比激动的心情写下此文. 曾使用过的方法 ...
- ASP.NET MVC NPOI导入Excel DataTable批量导入到数据库
使用NPOI导入Excel 首先在MVC项目中导入NPOI 查询NPOI安装,排序依据,选择:最高下载量,选择第一个. 在控制器中创建ExcelController 在Index视图中写入代码: @u ...
- 使用NPOI导入Excel注意日期格式和数字格式
//使用NPOI导入Excel public static DataTable importExcelToDataSetUsingNPOI(string FilePath, string fileNa ...
- .Net core NPOI导入导出Excel
最近在想.net core NPOI 导入导出Excel,一开始感觉挺简单的,后来真的遇到很多坑.所以还是写一篇博客让其他人少走一些弯路,也方便忘记了再重温一遍.好了,多的不说,直接开始吧. 在.Ne ...
随机推荐
- flask standrad class 使用
from flask import Flask,views,url_for app = Flask(__name__) class IndexView(views.View): def dispatc ...
- PostgreSQL中with和without time zone两者有什么区别
with和without time zone两者有什么区别 1.区别 1)名字上看一个是带时区的,另一个是不带时区的,查出来的时间是一样的,只是一个带时区标志,一个不带而已,时区的基准是格林威治时间U ...
- spring boot zuul集成kubernetes等第三方登录
介绍一下,在单点登录平台集成kubernetes登录,集成其它系统的登录原理是一样的,如grafana, nacos, jenkins等. POM引用: <dependency> < ...
- oa_mvc_easyui_删除(6)
1.删除列,添加a标签,绑定参数 <a href="javascript:void(0)" class="delete" ids="@newli ...
- 小程序存emoji表情 不改变数据库
1.小程序:提交前先编码 encodeURIComponent(data) 2.服务端解码(PHP) urldecode(data) 3.如果有空格字符串的,保存之前先对空格进行处理,不然空格在页面会 ...
- postMessage解决iframe跨域问题
转:https://juejin.im/post/5b8359f351882542ba1dcc31 https://juejin.im/post/590c3983ac502e006531df11 ht ...
- IDEA修改Maven全局配置
在使用过程中发现,IDEA每次新建一个Project ,这个maven配置都会初始化默认的. 这里需要设置下全局配置: File -> Other Settings -> Settings ...
- Linux下如何查看CPU型号、个数、核数、逻辑CPU数、位数、发行版本、内核信息、内存、服务器生产厂家
[原文链接]:http://blog.csdn.net/mdx20072419/article/details/7767809 http://blog.chinaunix.net/uid-224252 ...
- 判断页面是在移动端还是PC端打开的
$(function () { var curWwwPath = window.document.location.href; var pathName = window.document.locat ...
- python学习-Python简介以及运行环境
Python语言是全世界几百种编程语言中的一个,诞生时间不算长,但是现在已经成为很热门的语言,近几年在TIOBE排行榜一直呈现上升趋势,截止19年2月,python已经超过C++成为排名第三的语言. ...