mvc NPOI导入读取Excel文件

后台方法:

public ActionResult ExcelUploadSubmit()
{
HttpPostedFileBase excelFile = Request.Files["excelFile"]; //取到上传域
int count = 0;
if (null != excelFile)
{
string fileName = Path.GetFileName(excelFile.FileName); //取到文件的名称
if (fileName.Equals("") || null == fileName)
{ //没有选择文件就上传的话,则跳回到上传页面
return View("ZQ");
}
if (System.IO.Directory.Exists(Server.MapPath("upload")) == false)//如果不存在就创建file文件夹
{
System.IO.Directory.CreateDirectory(Server.MapPath("upload"));
}
string serverpath = Server.MapPath("/"); //获取文件根目录
string filePath = Path.Combine(HttpContext.Server.MapPath(@"\upload\"), Path.GetFileName(fileName));//合并得到完整的文件路径

excelFile.SaveAs(serverpath + @"\upload\" + fileName); //保存上传文件到服务器
DataTable dt = new DataTable();
dt.Columns.Add("Name", Type.GetType("System.String"));
dt.Columns.Add("Tel", Type.GetType("System.String"));
FileStream inputStream = new FileStream(serverpath + @"\upload\" + fileName, FileMode.Open);
HSSFWorkbook workbook = new HSSFWorkbook(inputStream); //解析上传的Excel
HSSFSheet sheet = workbook.GetSheetAt(0) as HSSFSheet;
int rowNum = sheet.PhysicalNumberOfRows;
for (int i = 0; i < rowNum; i++)
{
HSSFRow row = sheet.GetRow(i) as HSSFRow;
int cellNum = row.PhysicalNumberOfCells;

DataRow newRow = dt.NewRow(); //DataTable创建新行
for (int j = 0; j < cellNum; j++)
{
HSSFCell cell = row.GetCell(j) as HSSFCell;
if (cell.CellType == CellType.Numeric)
{
newRow[j] = cell.NumericCellValue; //给新建的行加列
}
else
{
newRow[j] = cell.StringCellValue;
}
}
dt.Rows.Add(newRow); //新建的行加入到DataTable中
}
if (dt.Rows.Count > 0)
{
List<string> send = new List<string>();
StringBuilder sb = new StringBuilder();
foreach (DataRow dr in dt.Rows)
{
string name = dr["Name"].ToString();
string tel = dr["Tel"].ToString();
if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(tel))
continue;
tel = Regex.Replace(tel, " ", "", RegexOptions.IgnoreCase);//去掉空格
tel = Regex.Replace(tel, "-", "", RegexOptions.IgnoreCase);//去掉电话号码中间-

if (!IsHandset(tel))
continue;

if (tel.Length != 11)
continue;

tel = tel.Substring(0, 11);
if (send.Where(p => p.Contains(tel)).FirstOrDefault() != null)
continue;
bool result = false;
string tempId = ConfigurationManager.AppSettings["zq"];
bool isInit = api.init(url, prot);
api.setAccount(sid, token);
api.setAppId(appid);
try
{
if (isInit)
{
string[] date = { name };
Dictionary<string, object> retData = api.SendTemplateSMS(tel, tempId, date);
ret = CCPSmsHelper.getDictionaryData(retData);
string resultcode = ret.Split(';')[0].Split('=')[1];
if (resultcode == "000000")
result = true;
else
{
result = false;
continue;
}
}
else
{
ret = "初始化失败";
}
}
catch (Exception exc)
{
result = false;
}
sb.Append(name + ":" + tel + "\r\n");
if (result)
{
count++;
send.Add(tel);
}
else
{
continue;
}
System.Threading.Thread.Sleep(500);
}
WriteLog("短信日志:" + sb.ToString());
}
//}
ViewData["result"] = "总共发送" + count + "条";

return View("ZQJ");
}

return View();
}

 

 

前台页面

@{
ViewBag.Title = "MoonFestivalSMS";
}

 


<div style="border:1px solid #ccc;margin:60px auto;width:60%;text-align:center;height:80px; line-height:80px;">
<a class="btn btn-small" href="~/Home/Dc">
数据导出
</a>
&nbsp;&nbsp;&nbsp;
<a class="btn btn-small" href="~/Home/FS">
强制发送
</a>
&nbsp;
<form action="~/Home/ExcelUploadSubmit" method="post" enctype="multipart/form-data">
<p>
选择文件: <input id="excelFile" name="excelFile" type="file" /> &nbsp;&nbsp;
<input id="Submit1" type="submit" value="发送" />
</p>
<h2>@ViewData["result"]</h2>
</form>
</div>

转载于:https://www.cnblogs.com/zhan-shuai/p/5881773.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值