input file 選擇Excel
阿新 • • 發佈:2019-01-17
代碼 org src sys pread false 控制 themes mvc4
說明:開發環境 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