首页 技术 正文
技术 2022年11月16日
0 收藏 437 点赞 4,202 浏览 2621 个字

博主用的是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>
}
}

最后上一张效果图

【第二篇】.NET用NPOI读取Excel表格并在页面预览

———————————————————————————————————

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

———————————————————————————————————

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849