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 send = new List();
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 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";
}

 




数据导出

   

强制发送

 


选择文件:   


@ViewData["result"]



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

你可能感兴趣的:(mvc NPOI导入读取Excel文件)