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>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM