博主用的是npoi2.0.1,支持.xls也支持.xlsx

直接上代码吧。

<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Shape</th>
<th>Pcs</th>
<th>Sizw</th>
<th>Size Range</th>
<th>Color</th>
<th>Clarity</th>
<th>Rap-Price</th>
<th>Cut</th>
<th>POL</th>
<th>SYM</th>
<th>H&A</th>
<th>FL</th>
<th>Diam/LxW</th>
<th>Dep%</th>
<th>Tab%</th>
<th>Rap%</th>
<th>LAB1</th>
<th>CersNo</th>
<th>LAB2</th>
<th>CersNo</th>
<th>Price/ct¥</th>
</tr>
</thead>
<tbody id="td">
</tbody>
</table>

之后用了jquery.form.js

function uploadFile() {
var options = {
type: "post",
enctype: "multipart/form-data",
url: "UploadExcel",
success: function (data) {
if (data != null) {
$("#td").append(data);
$("#uploadData").css("display", "block");
} else if (data == 0) {
layer.msg('文档加载失败!', { icon: 5 }, function () {
$("#uploadData").css("display", "none");
});
} else if (data == -1) {
layer.msg('数据库连接失败!', { icon: 5 }, function () {
$("#uploadData").css("display", "none");
});
} else {
layer.msg('未知错误!', { icon: 5 }, function () {
$("#uploadData").css("display", "none");
});
}
},
processData: true,
global: true
};
$("#form").ajaxSubmit(options);
}

跟着是动作方法

public ActionResult UploadExcel()
{
string rtn = "";
if (Request.Files.Count > )
{
HttpPostedFileBase file = Request.Files["File"];
string newFile = Server.MapPath("~/Areas/Admin/UploadFile/StockImprot/") + DateTime.Now.ToString("yyyyMMddHHmmss") + file.FileName;
while (System.IO.File.Exists(newFile))
{
newFile = Server.MapPath("~/Areas/Admin/UploadFile/StockImprot/") + DateTime.Now.ToString("yyyyMMddHHmmss") + file.FileName;
}
file.SaveAs(newFile);
DataTable ds = ExcelHelper.GetDataTable(newFile);
if (ds != null)
{
if (ds.Rows != null)
{
ViewData["datatable"] = ds;
return PartialView("_PartialDataTable"); //注意这里返回是的局部视图哦
}
}
else
{
rtn = ""; //文档加载失败
}
}
else
{
rtn = "-1";
}
return Content(rtn);
}

局部视图的代码

@if (ViewData["datatable"] != null)
{
System.Data.DataTable dt = ViewData["datatable"] as System.Data.DataTable;
foreach (System.Data.DataRow dr in dt.Rows)
{
<tr>
<td>@dr[].ToString()</td>
<td>@dr[].ToString()</td>
<td>@dr[].ToString()</td>
<td>@dr[].ToString()</td>
<td>@dr[].ToString()</td>
<td>@dr[].ToString()</td>
<td>@dr[].ToString()</td>
<td>@dr[].ToString()</td>
<td>@dr[].ToString()</td>
<td>@dr[].ToString()</td>
<td>@dr[].ToString()</td>
<td>@dr[].ToString()</td>
<td>@dr[].ToString()</td>
<td>@dr[].ToString()</td>
<td>@dr[].ToString()</td>
<td>@dr[].ToString()</td>
<td>@dr[].ToString()</td>
<td>@dr[].ToString()</td>
<td>@dr[].ToString()</td>
<td>@dr[].ToString()</td>
<td>@dr[].ToString()</td>
</tr>
}
}

最后上一张效果图

---------------------------------------------------------------------------------------------------------

转载请记得说明作者和出处哦-.-
作者:KingDuDu
原文出处:https://www.cnblogs.com/kingdudu/articles/4745151.html

---------------------------------------------------------------------------------------------------------

【第二篇】.NET用NPOI读取Excel表格并在页面预览的更多相关文章

  1. 使用NPOI读取Excel表格内容并进行修改

    前言 网上使用NPOI读取Excel文件的例子现在也不少,本文就是参考网上大神们的例子进行修改以适应自己需求的. 参考博文 http://www.cnblogs.com/restran/p/38894 ...

  2. NPOI读取Excel表格类

    public class NPOIHelper    {        private HSSFWorkbook workbook;        public static IWorkbook Lo ...

  3. .NET Core 使用NPOI读取Excel返回泛型List集合

    我是一名 ASP.NET 程序员,专注于 B/S 项目开发.累计文章阅读量超过一千万,我的博客主页地址:https://www.itsvse.com/blog_xzz.html 网上有很多关于npoi ...

  4. C# 读取Excel表格内容,以及NPOI的使用

    在实际的开发中,我们可能需要读写word或者Excel的内容,在我开发的项目中,需要读取Excel的内容,并将相对应的内容存储到数据库中,这里简单跟大家分享一下,希望能够帮助一些人. 我相信在读写wo ...

  5. NPOI读取Excel遇到的坑

    NPOI是POI的.NET版本.POI是用Java写成的库,能帮助用户在没有安装Office环境下读取Office2003-2007文件.NPOI在.NET环境下使用,能读写Excel/Word文件. ...

  6. 使用NPOI读取Excel数据并写入SQLite

    首先,我们来建一个数据库,我们就叫Hello.db(不一定是db后缀,你可以sqlite,sqlite3,db3)都可以作为识别,然后往里面建一个空的表格,如下图所示 然后建一个Excel表格,往表格 ...

  7. NPOI读取Excel帮助类,支持xls与xlsx,实现公式解析,空行的处理

    NPOI读取Excel(2003或者2010)返回DataTable.支持公式解析,空行处理. /// <summary>读取excel /// 默认第一行为表头 /// </sum ...

  8. 读取Excel表格中数据原型

    写下这篇博客来记录自己的工作,这部分功能是读取Excel表格中的数据,并通过c#中的datagridview控件将读取的数据显示出来.为了方便用户,我设计了一个read按钮,用户点击这个按钮,会弹出打 ...

  9. 使用NPOI读取Excel数据到DataTable

    如今XML文件的存储格式大行其道,可是也不是适用于全部情况,非常多单位的数据交换还是使用Excel的形式.这就使得我们须要读取Excel内的数据.载入到程序中进行处理.可是如何有效率的读取,如何使程序 ...

随机推荐

  1. Grid行编辑插件

    //操作列不显示应该就是autoLoad的问题.         Ext.onReady(function () {             Ext.BLANK_IMAGE_URL = "E ...

  2. linux下命令行查看Memcached运行状态(shell)

    stats查看memcached状态的基本命令,通过这个命令可以看到如下信息:STAT pid 22459                             进程IDSTAT uptime 10 ...

  3. Find the smallest number whose digits multiply to a given number n

    Given a number ‘n’, find the smallest number ‘p’ such that if we multiply all digits of ‘p’, we get ...

  4. P==NP??

    注:基础知识见下方 下面是关于P==NP ???  一些讨论,挺好玩的. 1. 首先强调一下数学上还没有证明这个问题!但是我们看看其他角度来看这个问题. 其次,心理上来说,要是可以证明P==NP那么早 ...

  5. [STL]双层级配置器

    考虑到过多“小型区块”可能造成的内存碎片问题,SGI设计了双层级配置器: 第一级配置器直接调用malloc()和free(): 第二级配置器分两种情况:当配置区块大于128字节时,调用第一级配置器:当 ...

  6. CAS登录后回传除了ticket参数以外的其他自定义参数

    在一次项目的技术选型中,选择了easyui+cas+shiro+spring的组合,cas实现了单点登录,这使得在一个应用中嵌入另一个应用的页面来展示数据所涉及到的授权方面变得简单. 由于shiro在 ...

  7. 单选项框RadioGroup的综合应用

    大家好,我们今天这一节要介绍的是RadioGroup 的组事件.RadioGroup 可将各自不同的RadioButton ,设限于同一个Radio 按钮组,同一个RadioGroup 组里的按钮,只 ...

  8. UVA 10943 How do you add? DP

    Larry is very bad at math — he usually uses a calculator, whichworked well throughout college. Unfor ...

  9. hdu2023 求平均成绩 ~~很闲~~~

    #include<iostream> #include<stdio.h> #include<math.h> #include<string.h> #in ...

  10. Yarn上的几个问题整理

    原文链接   http://xiguada.org/yarn_some_question/ ‎   1. NodeManager是如何Kill掉Container的呢? 答,在DefaultConta ...