视图

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. Linux内核分析——分析system_call中断处理过程

    万子惠 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 我选择的是get ...

  2. .htaccess详解及.htaccess参数说明【转】

    目录(?)[-] htaccess 详解 htaccess rewrite 规则详细说明 RewriteEngine OnOff RewriteBase URL-path RewriteCond Te ...

  3. Entity Framework search sequnce

    sql express, then (LocalDb)\v11.0 (LocalDb)\mssqllocaldb sqllocaldb i  could list all the local db i ...

  4. Android之动态申请权限(API23以上需求)

    API 23之前的版本都是自动获取权限,而从 Android 6.0 开始添加了权限申请的需求,更加安全. 这里以单个存储权限为例: · 在 Manifest 中添加访问权限:(只需设置可写,因为可写 ...

  5. Default Title

    測試的標誌 代表意義 1. 關於某個檔名的『檔案類型』判斷,如 test -e filename 表示存在否 -e 該『檔名』是否存在?(常用) -f 該『檔名』是否存在且為檔案(file)?(常用) ...

  6. delegate和protocol

    协议和代理对于一个新手来说确实不讨好理解,也有很多的iOS开发的老手对此是懂非懂的.网上的很多博文只是讲了怎么使用,并没有说的很明白.下面我谈一下我的理解. 1.你要先搞明白,协议和代理为什么会出现, ...

  7. April Fools Day Contest 2014

    April Fools Day Contest 2014 A.C.H三道题目 ============================================================= ...

  8. 拓展Yii Framework(易框架)

    1.拓展yii 此文针对Yii1.1.15而写,请注意甄别你的Yii Framework 版本. 拓展yii是开发期间常见的代码处理方式.例如,你写一个新的controller(业务控制器),你通过继 ...

  9. Codeforces Round #167 (Div. 2)

    C. Dima and Staircase 线段树维护区间最大值. D. Dima and Two Sequences 由于模数不一定为质数,所以通过拆分质因数来做阶乘取模. E. Dima and ...

  10. cocos2dx 3.2 Touch Listen和menu回调实现截屏

    在Cocos2d-X 3.x里面,已经集成了截屏功能,单独放在utils命名空间里,实现在base/ccUtils.h文件里面.看下函数申明 /** Capture the entire screen ...