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 ...
随机推荐
- C#求任意范围内的质数
class Program { public static List<int> list; static void Main(string[] args) { Console.WriteL ...
- Unity3D–RectTransfrom 记录笔记
一.基本要点 部分来源:http://www.2fz1.com/post/unity-ugui-recttransform/ RectTransform继承于Trasnfrom , 在Trasnfor ...
- Linux系统重启python程序
#! /usr/bin/env python #coding=utf-8 import sys import ConfigParser import urllib import urllib2 fro ...
- hhvm之轻进程
本文为原创,转载请注明:http://www.cnblogs.com/gistao/ 背景 我们在aws上部署了hhvm,高峰段发现cpu idle降的比较低,只有10-20%,而使用php-fpm的 ...
- IntelliJ IDEA 工具常用快捷键
IntelliJ IDEA是java语言开发的集成环境,IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助手.代码自动提示.重构.J2EE支持.各类版本工具(git.svn.g ...
- 最小生成树——kruskal算法
kruskal和prim都是解决最小生成树问题,都是选取最小边,但kruskal是通过对所有边按从小到大的顺序排过一次序之后,配合并查集实现的.我们取出一条边,判断如果它的始点和终点属于同一棵树,那么 ...
- C#连接数据库SQL,并转换成list形式
web config 配置 <connectionStrings> <add name="SQLConnString" connectionString=& ...
- Python 类与作用域——一些测试
/* 一 */ >>> class T (): a = 0 b = a >>> T.b 0 >>> del T /* 二 */ >>& ...
- 创建支持ssh服务的docker容器和镜像
http://www.kongxx.info/blog/?p=57 1. 这里使用的centos作为容器,所以首先下载centos的imagessudo docker pull centos 2. 下 ...
- 空的安卓工程添加activity
1.编写类继承activity,并重写onCreate方法 package org.tonny; import android.app.Activity; import android.os.Bund ...