前端代码

html

<link href="~/lib/bootstrap-fileinput/css/fileinput.min.css" media="all" rel="stylesheet" type="text/css" />
<link href="~/lib/bootstrap-fileinput/themes/explorer-fa/theme.min.css" media="all" rel="stylesheet" type="text/css" />
<script src="~/lib/bootstrap-fileinput/js/fileinput.min.js" type="text/javascript"></script>
<script src="~/lib/bootstrap-fileinput/js/locales/zh.js" type="text/javascript"></script>
<script src="~/lib//bootstrap-fileinput/themes/explorer-fa/theme.min.js" type="text/javascript"></script>
<script src="~/lib/bootstrap-fileinput/themes/fa/theme.min.js" type="text/javascript"></script> <div class="modal-body">
<form name="msproject" role="form" method="post" novalidate class="form-validation" enctype="multipart/form-data">
<div class="file-loading">
<input id="kv-explorer" type="file" multiple />
</div>
</form>
</div>

JS

(function ($) {
app.modals.ImportProjectMaterialsModal = function () {
var _modalManager; var _$form = null; this.init = function (modalManager) {
_modalManager = modalManager; var materilaTable = _modalManager.getOptions().importMppOptions.materials; _$form = _modalManager.getModal().find('form[name=CmsImportMsProjectMaterilasForm]');
_$form.validate({ ignore: "" }); var _projectId = _modalManager.getModal().find('input[name=ProjectId]').val();
var _actionType = _modalManager.getModal().find('input[name=ActionType]').val(); _modalManager.getModal().find('#kv-explorer')
.fileinput({
'theme': 'explorer-fa',
'uploadUrl': '', //上传的地址
language: 'zh', //设置语言
overwriteInitial: false,
showPreview: false,
showClose: false,
allowedFileExtensions: ['xls','xlsx'],
initialPreviewAsData: false,
previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
maxFileCount: 1,//允许同时上传的最大文件数
enctype: 'multipart/form-data',
uploadExtraData: function () {
var data = {
projectId: _projectId,
actionType: _actionType
};
return data;
}
})
.on("fileuploaded", function (event, data, previewId, index) {
abp.notify.info(app.localize('SavedSuccessfully'));
materilaTable.reload();
_modalManager.setResult(data.response.result);
_modalManager.close();
});
}; this.save = function () {
if (!_$form.valid()) {
return;
}
};
}
})(jQuery)

后台上传

  string localFileName = null;
string fileName = Guid.NewGuid().ToString("N") + Path.GetExtension(file.FileName);
string location = Path.Combine(""); //上传到的路径
using (FileStream fs = new FileStream(location, FileMode.Create))
{
await file.CopyToAsync(fs);
fs.Close();
localFileName = fs.Name;
}
return await SetupFromMSProjectMaterials(projectId, localFileName);

后台解析使用NPOI

 public class NPOIExcelHelper
{
/// <summary>
/// 将excel导入到datatable
/// </summary>
/// <param name="filePath">excel路径</param>
/// <param name="isColumnName">第一行是否是列名</param>
/// <returns>返回datatable</returns>
public static DataTable ExcelToDataTable(string filePath, bool isColumnName)
{
DataTable dataTable = null;
FileStream fs = null;
DataColumn column = null;
DataRow dataRow = null;
IWorkbook workbook = null;
ISheet sheet = null;
IRow row = null;
ICell cell = null;
int startRow = 0;
try
{
using (fs = File.OpenRead(filePath))
{
// 2007版本
if (filePath.IndexOf(".xlsx") > 0)
workbook = new XSSFWorkbook(fs);
// 2003版本
else if (filePath.IndexOf(".xls") > 0)
workbook = new HSSFWorkbook(fs); if (workbook != null)
{
sheet = workbook.GetSheetAt(0);//读取第一个sheet,当然也可以循环读取每个sheet
dataTable = new DataTable();
if (sheet != null)
{
int rowCount = sheet.LastRowNum;//总行数
if (rowCount > 0)
{
IRow firstRow = sheet.GetRow(0);//第一行
int cellCount = firstRow.LastCellNum;//列数 //构建datatable的列
if (isColumnName)
{
startRow = 1;//如果第一行是列名,则从第二行开始读取
for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
{
cell = firstRow.GetCell(i);
if (cell != null)
{
if (cell.StringCellValue != null)
{
column = new DataColumn(cell.StringCellValue);
dataTable.Columns.Add(column);
}
}
}
}
else
{
for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
{
column = new DataColumn("column" + (i + 1));
dataTable.Columns.Add(column);
}
} //填充行
for (int i = startRow; i <= rowCount; ++i)
{
row = sheet.GetRow(i);
if (row == null) continue; dataRow = dataTable.NewRow();
for (int j = row.FirstCellNum; j < cellCount; ++j)
{
cell = row.GetCell(j);
if (cell == null)
{
dataRow[j] = "";
}
else
{
//CellType(Unknown = -1,Numeric = 0,String = 1,Formula = 2,Blank = 3,Boolean = 4,Error = 5,)
switch (cell.CellType)
{
case CellType.Blank:
dataRow[j] = "";
break;
case CellType.Numeric:
short format = cell.CellStyle.DataFormat;
//对时间格式(2015.12.5、2015/12/5、2015-12-5等)的处理
if (format == 14 || format == 31 || format == 57 || format == 58)
dataRow[j] = cell.DateCellValue;
else
dataRow[j] = cell.NumericCellValue;
break;
case CellType.String:
dataRow[j] = cell.StringCellValue;
break;
}
}
}
dataTable.Rows.Add(dataRow);
}
}
}
}
}
return dataTable;
}
catch (Exception e)
{
if (fs != null)
{
fs.Close();
}
return null;
}
}
}

Excel 导入解析数据 NPOIExcelHelper的更多相关文章

  1. [办公自动化] 再读《让EXCEL飞》(从excel导入access数据时,union联合查询,数据源中没有包含可见的表格)

    一年多以前就买了@Mrexcel的<让excel飞>这本书.整体思路是利用access结合excel,大幅度提高数据分析效率. 最近又拿出来看了看.第十五章,比高级筛选更“高级”,P241 ...

  2. 工作日志,Excel导入树结构数据

    目录 1. 前言 2. 需求分析 2.1 需求难点 2.2 解决难点 2.3 表格设计 3. 功能实现 3.1 一个分枝 3.2 一个分枝多个树叶 3.3 多个分枝多个树叶 4. 代码事例 4.1 目 ...

  3. PHP 清除 Excel 导入的数据空格

    处理excel中的数据时,遇到了页面中显示为空格,审查元素时却显示为换行,使用replace函数也不管用,反正就是不知道是什么东西,看起来像空格 中文空格这里面有好几种:没有简单的解决问题的方式,比如 ...

  4. Talend 从Excel导入Saleforce数据(二) TMAP是精髓

    TMap LookUp 经过测试的结果: ------------------------------------------ LookUp最好从CSV读数据,这样是最快了(20万记录1s).从Sal ...

  5. Talend 从Excel导入Saleforce数据(一) 直接从salesforce lookup 性能的噩梦

    速度的瓶颈是在查询Sales force是否有该电话号码的联系人资料. TMap属性的 lookup Model, 如果用Load Once, 则会把SaleForce的contact全部load下来 ...

  6. excel导入mysql数据

    excel加载mysql数据 1.第一步,选择从mysql导入数据 2.单击会出现弹框: 3.可能有的同学的,这里缺少插件,例如: 4.去下载 这个 插件安装即可.https://dev.mysql. ...

  7. 将Excel导入到数据中

    常用的方式的有两种: 1. 通过 Microsoft.Jet.OLEDB.4.0 或  Microsoft.ACE.OLEDB.12.0 Microsoft.ACE.OLEDB.12.0 需要安装 A ...

  8. java利用jxl实现Excel导入功能

    本次项目实践基于Spring+SpringMvc+MyBatis框架,简单实现了Excel模板导出.和Excel批量导入的功能.实现过程如下:. 1.maven导入所需jar包 <depende ...

  9. JeeSite中Excel导入导出

    在各种管理系统中,数据的导入导出是经常用到的功能,通常导入导出以Excel.CSV格式居多.如果是学习的过程中,最好是自己实现数据导入与导出的功能,然而在项目中,还是调用现成的功能比较好.近期一直使用 ...

  10. Excel导入保存附件和解析数据

    Excel导入保存附件和解析数据 一,前端上传附件的组件 1.先给一个下载模板的按钮 // 下载Excel模板 downLoadExcel: function () { window.open(GLO ...

随机推荐

  1. Android 存储概览

    存储区​ Android 一开始就将存储区分为内部存储和外部存储,对应手机自带的存储和可插拔的 sd 卡(可类比于 PC 的硬盘和 U盘). 内部存储容量有限,Google 建议 App 数据尽量存储 ...

  2. cnetos 9 安装巨坑!!! ssh无法登录

    不管任何软件登录 或任何形式的ssh登录 仅开启了密钥的登录 没有账号密码 在安装引导设置root密码时候 下面有个复选框允许root密码ssh登录 勾选即可省略这些步骤 具体步骤: 找到合适的插入位 ...

  3. 【Docker学习教程系列】7-如何将本地的Docker镜像发布到阿里云

    在上一篇中,我们使用docker commit 命令,创建了一个带有vim的Ubuntu镜像.那么怎么将这个镜像分享出去呢?本文就来讲解如何将本地的docker镜像发布到阿里云上. 本文主要内容: 1 ...

  4. JavaScript – Decimal

    前言 之前就写过一篇 decimal, double, float, 但有点杂乱, 这篇把 JS 的部分独立写成一篇整理版. 参考 JavaScript 浮点数运算的精度问题 关于JavaScript ...

  5. Asp.net core 学习笔记 Secret 和 Data Protect Azure key-vault & Storage Account 第 2 篇

    更新 30-04-2023 最新版本请看这 2 篇 ASP.NET Core – User Secret & Azure Key Vault 之前有写过 2 篇关于 key-vault 和 d ...

  6. sicp每日一题[2.13-2.16]

    Exercise 2.13 Show that under the assumption of small percentage tolerances there is a simple formul ...

  7. vue3 3.3.4

    https://cn.vuejs.org/guide/introduction.html#what-is-vue 简介 import { createApp } from 'vue' createAp ...

  8. Spring —— bean实例化

    bean 实例化 bean本质上就是对象,创建bean使用构造方法完成(反射)      构造方法(常用)        静态工厂*        实例工厂*        FactoryBean(实 ...

  9. Wordle For Linux 2.0 | Windows 2.2.0

    2.2.0 更新时间:2024/8/2 Click to Download | Linux 2.0 | Windows 2.2.0 2.2.0 版本已开源,详见压缩包 2.1.1 存在问题:答案显示未 ...

  10. HuggingChat macOS 版现已发布

    Hugging Face 的开源聊天应用程序 Hugging Chat,现已推出适用于 macOS 的版本. 主要特点 Hugging Chat macOS 版本具有以下亮点: 强大的模型支持: 用户 ...