npoi上传xlsx文件,并读取数据
视图
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文件,并读取数据的更多相关文章
- 【asp.net】asp.net实现上传Excel文件并读取数据
#前台代码:使用服务端控件实现上传 <form id="form1" runat="server"> <div> <asp:Fil ...
- 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的路由方案,与原来的方案在使用上差别不 ...
- Django框架(上传Excel文件并读取)
博主今天整理下Django框架中上传Excel文件并读取 博主是要在管理平台中新增用例的维护功能,想着通过上传Excel文件来展示用例,下面是项目的路径图: 首先先建数据库模型 model.py 可以 ...
- 使用WebUploader上传HTML文件并读取文件
需求: 前端需要上传HTML文件并识别里面有多少个特殊标签并录入到数据库. 思路: 使用WebUploader上传文件,然后使用FileReader接口和DOMParser识别HTML中的特殊标签 资 ...
- 通过POI实现上传EXCEL的批量读取数据写入数据库
最近公司新增功能要求导入excel,并读取其中数据批量写入数据库.于是就开始了这个事情,之前的文章,记录了上传文件,本篇记录如何通过POI读取excel数据并封装为对象上传. 上代码: 1.首先这是一 ...
- Springboot 上传CSV文件并将数据存入数据库
.xml文件依赖配置 <!--csv依赖 --> <dependency> <groupId>org.apache.commons</groupId> ...
- 上传excel文件,读取内容,增加事务写入数据库
package com.inspur.icpmg.itss.asset.dao.impl; import com.inspur.icpmg.util.DBHelper; import org.apac ...
- .Net NPOI 上传excel文件、提交后台获取excel里的数据
1.导入NPOI.dll 2.添加类NPOIExcel.cs using System; using System.Collections.Generic; using System.Text; us ...
- 最近开发的项目,遇到用户上传excel文件并导入数据到系统这个需求,而有excel中有的单元格是日期格式,本文介绍怎么从excel中读取日期格式的数据。
可以先判断单元格的类型,有的日期是字符串存储的,有的是按日期存储的(单元格按数字解析),代码如下: Cell cell = row.getCell(); Date date = null; if (c ...
随机推荐
- CSS样式-文字超出宽部分用省略号代替
.name {text-overflow: ellipsis;white-space: nowrap;overflow: hidden;display:block;width:120px; }
- Dubbo学习 概念定义
Dubbo是什么? Dubbo[]是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案. 其核心部分包含: 远程通讯: 提供对多种基于长连接的NIO框架抽象封 ...
- Nginx+PHP优化实例
1.PHP-FPM高负载的解决办法 http://blog.haohtml.com/archives/11162 2.Nginx优化配置 http://blog.haohtml.com/archive ...
- 升级OpenSSh到 7.3p1
1.开启 telnet 服务 Linux yum install -y telnet-server telnet /etc/xinet.d/telnet 中的yes 修改为no service xin ...
- ABAP屏幕设计
转自 http://www.cnblogs.com/aBaoRong/archive/2012/06/05/2536591.html abap 屏幕控制 ******************** 屏幕 ...
- ASP.NET Core (Database First)
CREATE DATABASE [EFCore_dbfirst] GO USE [EFCore_dbfirst] GO CREATE TABLE [Blog] ( [BlogId] int NOT N ...
- 用unity4.3发布WINDOWS STORE APP应用的方法
http://www.cnblogs.com/suxsho/ 原创,转载请声明 ============================================================ ...
- Git远程仓库(github
一.创建远程仓库(github) 首先到https://github.com注册一个账号,创建自己的Git,点击repositories,再点new 名字自定义,比如叫fansik,选择public点 ...
- Linux 下如何安装软件
一.解析Linux应用软件安装包 通常Linux应用软件的安装包有三种: 1) tar包,如software-1.2.3-1.tar.gz.它是使用UNIX系统的打包工具tar打包的. 2) rpm包 ...
- 激!QTREE系列
我现在才开始刷 QTREE 是不是太弱了?算了不管他…… QTREE: 树链剖分裸题(据说 lct 会超时……该说是真不愧有 spoj 的气息吗?) #include <cstdio> # ...