视图

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. PHPnow在win8下安装失败的解决办法

    提示: 安装服务[ Apache_pn ]失败,可能原因如下:1.服务名已存在,请卸载或使用不同服务名.2.非管理员权限,不能操作Window NT服务. 解决方案: 搜索:命令提示符   , 右键以 ...

  2. C#异常语句

    try: 用于检查发生的异常,并帮助发送任何可能的异常. catch: 以控制权更大的方式处理错误,可以有多个catch子句. finally :无论是否引发了异常,finally的代码块都将被执行. ...

  3. 如何清除SQL Server Management Studio的最近服务器列表

    SQL Server Management Studio (SSMS) 的"连接到服务器"对话框会记录用户所有访问过的服务器名称,这个功能对于经常连接多个数据库的人来说确实挺方便的 ...

  4. ubuntu安装rpm包

    ubuntu下的rpm包的安装方法 一般是不能够直接安装的,我们需要一个工具叫alien,先install它吧.然后按照下面择所需. 1.直接安装: alien -i -c filename.rpm ...

  5. Hive中的排序和分组(对map和reduce的影响,值得一看!)

    order by order by 会对输入做全局排序,因此只有一个reducer(多个reducer无法保证全局有序)只有一个reducer,会导致当输入规 模较大时,需要较长的计算时间. set ...

  6. sql指定插入自增长id的数据

    SET IDENTITY_Insert [Tag.V2].[dbo].[Members_EnAccount] ON; insert into [Tag.V2].[dbo].[Members_EnAcc ...

  7. 《Android深度探索HAL与驱动开发》第二章阅读心得

    按照书中所讲的步骤,第一步先准备好搭建Android开发环境所需要的工具:JDK6或以上版本:Eclipse3.4或以上版本:ADT:CDT:Android SDK:Android NDK;交叉编译环 ...

  8. archlinux vmware一些问题

    虚拟机没法上网 sudo modprobe vmnet sudo vmware-network --start

  9. Java中的Object类介绍

    Object类是所有类的父类,如果一个类没有使用extends关键字明确标识继承另外一个类,那么这个类默认继承Object类. Object类中的所有方法适用于所有子类 Object中比较常见的方法: ...

  10. h5和h4区别图片