视图

public PartialViewResult UploadIndex()
{
return PartialView();
}
<div>
<fieldset style="height:100px;">
<legend>说明</legend>
<h6 style="color: red; font-size: 15px;">请从IV查询中进行下载数据,按其中的表格格式进行数据上传,否则会导致上传失败或数据出错!</h6>
</fieldset>
<fieldset style="height: 50px;">
<legend>工单号</legend>
<div style="margin-top: 10px;">
<form id="formAkIvUpload" name="formToUpload" method="post" action="AkIv/UploadProcess" enctype="multipart/form-data" target="msgAkIvUpload">
<input id="fileAkIvUpload" type="file" size="45" name="fileAkIvUpload" />
<input type="submit" style="cursor: pointer;" value="上传" />
</form>
<iframe id="msgAkIvUpload" name="msgAkIvUpload" style="display: none;"></iframe>
</div>
</fieldset>
</div>

处理

public void UploadProcess()
{
//用于反馈执行信息
string strReturn =
@"
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type='text/javascript'>
alert('@msg');
</script>
</head>
<body>
</body>
</html>
";
//获取文件
var file = Request.Files["fileAkIvUpload"];
string msg = ""; //保存
if (file != null && file.ContentLength > )
{
//文件目录
string path = Server.MapPath("~") + "Content\\File\\";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
//文件类型验证
string[] allowExtension = { "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" };
if (!allowExtension.Contains(file.ContentType))
{
msg = "上传xlsx类型文件";
}
else
{
string filePath = path + "\\" + file.FileName;
if (System.IO.File.Exists(filePath))
System.IO.File.Delete(filePath);
file.SaveAs(filePath);//文件 try
{
NPOI.XSSF.UserModel.XSSFWorkbook hssfworkbook;
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
hssfworkbook = new NPOI.XSSF.UserModel.XSSFWorkbook(fs);
} NPOI.SS.UserModel.ISheet sheet = hssfworkbook.GetSheetAt();
System.Collections.IEnumerator rows = sheet.GetRowEnumerator(); while (rows.MoveNext())
{
NPOI.SS.UserModel.IRow row = (NPOI.SS.UserModel.IRow)rows.Current; if (row.GetCell().ToString() == "条码" || row.GetCell() == null || string.IsNullOrEmpty(row.GetCell().ToString()))
continue; try
{ float power = ;
try
{
power = row.GetCell() == null ? : float.Parse(row.GetCell().ToString());
}
catch { } AkIv akIv = new AkIv()
{
BarCode = row.GetCell() == null ? "" : row.GetCell().ToString(),
DateTime = row.GetCell() == null ? DateTime.Now : Convert.ToDateTime(row.GetCell().ToString()),
Eff = row.GetCell() == null ? : float.Parse(row.GetCell().ToString()),
Isc = row.GetCell() == null ? : float.Parse(row.GetCell().ToString()),
Voc = row.GetCell() == null ? : float.Parse(row.GetCell().ToString()),
Rs = row.GetCell() == null ? : float.Parse(row.GetCell().ToString()),
Rsh = row.GetCell() == null ? : float.Parse(row.GetCell().ToString()),
Pmax = row.GetCell() == null ? : float.Parse(row.GetCell().ToString()),
Vpm = row.GetCell() == null ? : float.Parse(row.GetCell().ToString()),
Ipm = row.GetCell() == null ? : float.Parse(row.GetCell().ToString()),
FF = row.GetCell() == null ? : float.Parse(row.GetCell().ToString()),
Sun = row.GetCell() == null ? : float.Parse(row.GetCell().ToString()),
Temp = row.GetCell() == null ? : float.Parse(row.GetCell().ToString()),
Class = row.GetCell() == null ? "" : row.GetCell().ToString(),
Employee = row.GetCell() == null ? "" : row.GetCell().ToString(),
LineTitle = row.GetCell() == null ? "" : row.GetCell().ToString(),
StationTitle = row.GetCell() == null ? "" : row.GetCell().ToString(),
OrderNumber = row.GetCell() == null ? "" : row.GetCell().ToString(),
BatterySupplier = row.GetCell() == null ? "" : row.GetCell().ToString(),
Power = power,
InterconnectId = row.GetCell() == null ? "" : row.GetCell().ToString(),
InterconnectSpec = row.GetCell() == null ? "" : row.GetCell().ToString()
}; _akIvRepository.Insert(akIv);
}
catch (Exception e)
{ }
} SaveUserLog("AkIv上传", filePath, true);
msg = "上传功成成功!";
}
catch (Exception e)
{
msg = e.ToString();
}
}
}
else
{
msg = "请选择文件!";
} System.Web.HttpContext.Current.Response.ContentType = "text/html";
System.Web.HttpContext.Current.Response.Write(strReturn.Replace("@msg", msg));
}

npoi上传xlsx文件,并读取数据的更多相关文章

  1. 【asp.net】asp.net实现上传Excel文件并读取数据

    #前台代码:使用服务端控件实现上传 <form id="form1" runat="server"> <div> <asp:Fil ...

  2. ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案 try.dot.net 的正确使用姿势 .Net NPOI 根据excel模板导出excel、直接生成excel .Net NPOI 上传excel文件、提交后台获取excel里的数据

    ASP.NET Core 2.2 : 十六.扒一扒新的Endpoint路由方案   ASP.NET Core 从2.2版本开始,采用了一个新的名为Endpoint的路由方案,与原来的方案在使用上差别不 ...

  3. Django框架(上传Excel文件并读取)

    博主今天整理下Django框架中上传Excel文件并读取 博主是要在管理平台中新增用例的维护功能,想着通过上传Excel文件来展示用例,下面是项目的路径图: 首先先建数据库模型 model.py 可以 ...

  4. 使用WebUploader上传HTML文件并读取文件

    需求: 前端需要上传HTML文件并识别里面有多少个特殊标签并录入到数据库. 思路: 使用WebUploader上传文件,然后使用FileReader接口和DOMParser识别HTML中的特殊标签 资 ...

  5. 通过POI实现上传EXCEL的批量读取数据写入数据库

    最近公司新增功能要求导入excel,并读取其中数据批量写入数据库.于是就开始了这个事情,之前的文章,记录了上传文件,本篇记录如何通过POI读取excel数据并封装为对象上传. 上代码: 1.首先这是一 ...

  6. Springboot 上传CSV文件并将数据存入数据库

    .xml文件依赖配置 <!--csv依赖 --> <dependency> <groupId>org.apache.commons</groupId> ...

  7. 上传excel文件,读取内容,增加事务写入数据库

    package com.inspur.icpmg.itss.asset.dao.impl; import com.inspur.icpmg.util.DBHelper; import org.apac ...

  8. .Net NPOI 上传excel文件、提交后台获取excel里的数据

    1.导入NPOI.dll 2.添加类NPOIExcel.cs using System; using System.Collections.Generic; using System.Text; us ...

  9. 最近开发的项目,遇到用户上传excel文件并导入数据到系统这个需求,而有excel中有的单元格是日期格式,本文介绍怎么从excel中读取日期格式的数据。

    可以先判断单元格的类型,有的日期是字符串存储的,有的是按日期存储的(单元格按数字解析),代码如下: Cell cell = row.getCell(); Date date = null; if (c ...

随机推荐

  1. LayaAir引擎——(七)

    LayaAir引擎——人物控制TiledMap地图移动和墙壁检测 所需要的软件: LayaAir IDE 1.0.2版本 TiledMap 所需要的东西: 地图:53 * 32,(48*48) 人物: ...

  2. Centos7搭建java+mysql环境

    前几天买了个国外的vps,打算用来练练手,准备安装mysql+jdk+tomcat+git,然后就从网上找些资料开始安装. 1.准备工具 首先,需要连接到centos,这里我用的连接工具是xshell ...

  3. CSharper~

    昨天晚上开始,重启读clr via c#大计.Jeffery大神的大作基本上没人不推荐的,所以我也抱了很大的期待,希望读完以后对.net的认识能有质的飞跃.争取五月份内读完.昨天看完了第一章,感觉.n ...

  4. 【css】a标签的用法

    <a>标签属性display的不同设置达到目的 display:block和display:inline; display:block 可以使得<a>标签设置宽高.边线.mar ...

  5. 数组循环:循环多个li 每个li 固定N条数据

    PHP代码: $arr = array(1,2,3,4,5,6,7); $x = 1; $y = 0; foreach($arr as $k => $v){ $data[$y][] = $v; ...

  6. TableView分割线从顶端开始

    如果什么都不设置的话 分割线是从cell.textlabel处开始的 如果加上 [_myTableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0) ...

  7. code complete part1

    最近在看code complete,学习了一些东西,作为点滴,记录下来. 关于类: 类的接口抽象应该一致 类的接口要可编程,不要对类的使用者做过多的假设.不要出现类似于:A的输入量一定要大于多少小于多 ...

  8. creature_template

    entry 生物唯一编号 modelid_A 联盟模型ID,参考creature_model_info modelid_A2 同上 modelid_H 部落模型ID,参考creature_model_ ...

  9. .net frameworkAPI文档下载地址

    http://www.msdn.hk/html/2014/5.html VS2013 ILdasm 反编译工具安装在下面地址里了 C:\Program Files (x86)\Microsoft SD ...

  10. CSRF攻击

    1.什么是CSRF攻击CSRF(Cross-site request forgery),跨站请求伪造.CSRF攻击的原理如下:1)用户登录正常的网站A后,在本地生成Cookie2)在不登出A的情况下, ...