input file 选择Excel
Posted net064
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了input file 选择Excel相关的知识,希望对你有一定的参考价值。
说明:开发环境 vs2012 asp.net mvc4 c# ,使用file 选择Excel文件 传到后台 使用Aspose.Cells获取Excel sheet页的名称
1、html代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EasyuiCombotree.aspx.cs" Inherits="MvcAppTest.EasyuiCombotree" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Easyui Combotree 测试</title>
<link href="Easyui/themes/default/easyui.css" rel="stylesheet" />
<script src="Easyui/jquery-1.7.2.js"></script>
<script src="Easyui/jquery.easyui.min.js"></script>
<script src="Easyui/locale/easyui-lang-zh_CN.js"></script>
<script src="JS/jquery.form.js"></script>
<script type="text/javascript">
$(function () {
$(‘#ee‘).combobox({
valueField: ‘id‘,
textField: ‘text‘,
editable: false,//不可编辑,只能选择
disabled: false,
value: ‘选择sheet‘
});
});
function getGSParam()
{
var objModel = $(‘#fileID‘).val();
if (objModel == ‘‘) {
alert(‘请选择文件‘);
} else {
var valText = objModel.split(‘.‘);
if (valText[1] == ‘xlsx‘ || valText[1] == ‘xls‘) {
var f = $(‘#form1‘);
f.ajaxSubmit({
url: ‘Home/getGSParamNew‘,
data: { OffID: ‘100‘ },
type: ‘post‘,
async: false,
success: function (data) {
console.log(data);
//var data01 = $.parseJSON(data);
$("#ee").combobox("loadData", data);
},
error: function (data) {
alert(‘数据未填写规范,请参照规范指南‘);
}
});
}
else {
alert(‘请选择Excel数据表‘);
}
}
}
</script>
</head>
<body>
<div>
<form id="form1" enctype="multipart/form-data">
<input id="fileID" type="file" accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" name="files" />
</form>
<div>
<div><span>传参获取Excel数据表的sheet名称</span></div>
<div><input id="ee" name ="ee" value=""/></div>
<input type="button" value="获取sheet" onclick="getGSParam()" style="width:120px;" />
</div>
</body>
</html>
2、Home控制器后台代码
using Aspose.Cells;
public JsonResult getGSParamNew(string OFFID, HttpPostedFileBase files)
{
List<ComboModel> myList = new List<ComboModel>();
Workbook book = new Workbook(files.InputStream);
WorksheetCollection myColection = book.Worksheets;
int k1 = myColection.Count;
for (int i = 0; i < k1; i++)
{
ComboModel model = new ComboModel() { id = i, text = myColection[i].Name };
myList.Add(model);
}
return Json(myList, JsonRequestBehavior.DenyGet);
}
public class ComboModel
{
public System.Int32 id { get; set; }
public System.String text { get; set; }
}
3、引用文件下载地址
Aspose.Cells引用地址
链接:https://pan.baidu.com/s/14ILv4vYkE5dVCbdsZApucA
提取码:dlyu
Easyui引用文件地址
链接:https://pan.baidu.com/s/1KxL2QeVEbEVHU9UxV6LBWw
提取码:cwbd
以上是关于input file 选择Excel的主要内容,如果未能解决你的问题,请参考以下文章