1、点击导入按钮
2、 //打开dialog 与处理导入Excel
function ImportExcel()
{
$("#ImportExcel").show();
$("#ImportExcel").dialog({
title: "导入车型",
width: 1000,
height: 600,
draggable: true,
resizable: false,
modal: true,
buttons:
[
{
text: '保存',
iconCls: 'icon-save',
handler: function () {
var list = $("#ImportExcelCarTypeDT").datagrid("getRows");
AjaxCall("/BaseInfo/CarType/SaveImortExcel", { list: list }, function (res) {
if (res.ErrorCode == 1) {
$.messager.alert('提示', "保存成功!", 'info');
_rowIndex = 0;
GetCarType(_pageIndex, _pageSize);
}
else { $.messager.alert('提示', "保存失败!", 'error'); }
});
}
},
{
text: '取消',
handler: function () {
$("#ImportExcel").dialog('close');
}
}
]
});
}
3、点击导入
将数据发到控制器取操作
//利用nopi表单提交后导入excel
function ImportExcelByNPOI() {
$("#npoiload").form("submit", {
url: '/BaseInfo/CarType/ImportExcel/',
onSubmit: function () {},
success: function (data) { $('#ImportExcelCarTypeDT').datagrid('loadData', JSON.parse(data)); }
}
);
}
public object ImportExcel()
{
string errorMsg = "";
CarTypeBll bll = new CarTypeBll();
try
{
HttpPostedFileBase file = Request.Files["npoiload"];
Stream fs = file.InputStream;
IWorkbook wk = null;
try
{
wk = new HSSFWorkbook(fs);
}
catch (Exception ex)
{
wk = new HSSFWorkbook(fs);
}
ISheet st = wk.GetSheetAt(0);
int rowCount = st.LastRowNum;
List<EHECD_CarType> ct=new List<EHECD_CarType>();
List<EHECD_CarType> newct = new List<EHECD_CarType>();
List<EHECD_CarType> allct = bll.GetCarTypeImport();
for (int i = 1; i <= rowCount; i++)
{
EHECD_CarType cm = new EHECD_CarType();
cm.inportType = "";
cm.sFirstLetter = st.GetRow(i).GetCell(0).ToString();
cm.sBrand = st.GetRow(i).GetCell(1).ToString();
cm.sFirm = st.GetRow(i).GetCell(2).ToString();
cm.sModel = st.GetRow(i).GetCell(3).ToString();
cm.iYearsStyle = st.GetRow(i).GetCell(4).ToString();//100
cm.sDisplacement = st.GetRow(i).GetCell(5).ToString();//20
cm.sTransmissionMmodel = st.GetRow(i).GetCell(6).ToString();//50
cm.sEngine = st.GetRow(i).GetCell(7).ToString();//50
if (!bll.HasChinese(st.GetRow(i).GetCell(8).ToString()))
{
cm.iGuidedPrice = Decimal.Parse(st.GetRow(i).GetCell(8).ToString());//18
}
else
{
cm.iGuidedPrice =0;
cm.inportType ="价格中有中文";
}
if (cm.sFirstLetter.Length > 10 )
{
cm.inportType = "首字母字段过长";
}
if (cm.sBrand.Length > 50 )
{
cm.inportType = "品牌字段过长";
}
if (cm.sFirm.Length > 50)
{
cm.inportType = "厂商字段过长";
}
if (cm.sModel.Length > 50)
{
cm.inportType = "型号字段过长";
}
if (cm.iYearsStyle.Length > 100 )
{
cm.inportType = "年代款型字段过长";
}
if (cm.sDisplacement.Length > 20)
{
cm.inportType = "排量字段过长";
}
if (cm.sTransmissionMmodel.Length > 50)
{
cm.inportType = "变速箱字段过长";
}
if (cm.sEngine.Length > 50)
{
cm.inportType = "发动机类型字段过长";
}
if (cm.sFirstLetter.Length == 0 || cm.sBrand.Length == 0 || cm.sFirm.Length == 0 || cm.sModel.Length == 0
|| cm.iYearsStyle.Length == 0 || cm.sDisplacement.Length == 0
|| cm.sTransmissionMmodel.Length == 0 || cm.sEngine.Length == 0)
{
cm.inportType = "字段不允许为空";
}
if (cm.inportType == "" || cm.inportType == null)
{
cm.inportType = "可正常导入";
}
这里判断在数据库是否存在
if (cm.inportType== "可正常导入")
{
foreach (var item in allct)
{
if (cm.sFirstLetter == item.sFirstLetter && cm.sBrand == item.sBrand && cm.sFirm == item.sFirm && cm.sModel == item.sModel
&& cm.iYearsStyle == item.iYearsStyle && cm.sDisplacement == item.sDisplacement
&& cm.sTransmissionMmodel == item.sTransmissionMmodel && cm.sEngine == item.sEngine)
{
cm.inportType = "数据库已存在";
break;
}
}
}
ct.Add(cm);
}
newct = ct.Distinct(new CarTypeComparer()).ToList();
var result = new { total = newct.Count, rows = newct };
return MyJson.JsonSerializer(result);
}
catch (Exception ex)
{
errorMsg = "导入失败,请检查EXCEL格式" + ex.Message;
}
return Json(new {ErrorCode=2,ErrorMessage=errorMsg });
}
、、、、、、、、、、、、返回信息加载 easyUI
@* 导入 *@
<div id="ImportExcel" style="display:none;">
<table id="ImportExcelCarTypeDT" class="easyui-datagrid" data-options="singleSelect:true,toolbar:'#ImportExcelToolBar'" border="0">
<thead>
<tr>
<th data-options="field:'sFirstLetter',width:80">首字母</th>
<th data-options="field:'sBrand',width:80">品牌</th>
<th data-options="field:'sFirm',width:100">厂商</th>
<th data-options="field:'sModel',width:100">型号</th>
<th data-options="field:'iYearsStyle',width:100">年代款型</th>
<th data-options="field:'sDisplacement',width:100">发动机排量</th>
<th data-options="field:'sTransmissionMmodel',width:100">变速箱</th>
<th data-options="field:'sEngine',width:100">发动机类型</th>
<th data-options="field:'iGuidedPrice',width:100">指导价(万)</th>
<th data-options="field:'inportType',width:100">导入前判断情况</th>
</tr>
</thead>
</table>
</div>
@*工具栏 *@
<div id="ImportExcelToolBar">
<form id="npoiload" method="post" enctype="multipart/form-data">
<input id="nopiupload" type="file" name="npoiload" style="width: 250px; background: White" />
<input id="Button4" type="button" value="导入" οnclick="ImportExcelByNPOI()" />
</form>
</div>
4、
5、点击保存
获取LIST 发送到 控制层 SaveImortExcel 方法 保存