layui学习之路——数据表格上传(C#)
前端 toolbarDemo里面的是工具栏选项
<script type="text/html" id="toolbarDemo">
<div class="layui-btn-container">
<asp:button class="layui-btn layui-btn-sm" lay-event="delete">删除</asp:button>
<asp:button type="button" class="layui-btn layui-btn-sm" id="test1">
<i class="layui-icon"></i>上传
</asp:button>
</div>
</script>
<table id="demo" lay-filter="test" ></table>
js
$(document).ready(function() {
reloadTable();
});
function reloadTable(){
layui.use(['upload', 'element', 'layer','table'],function(){
var table=layui.table;
var index = layer.load(1);
table.render({
elem:'#demo',
height:600,
url:'global/DataService_Test.ashx' ,
loading:false,
method:'post',
page:false,
toolbar: '#toolbarDemo',
defaultToolbar: [],
request: {
pageName: 'page',
limitName: 'limit'
},
id: 'table1',
parseData: function(res) {
return {
"code": res.code,
"msg": res.msg,
"count": res.count,
"data": res.data
};
},
cols:[[
{type:'radio'}
,{field: 'col1', title: 'col1', align:'center'}
,{field: 'col2', title: 'col2',align:'center'}
,{field: 'col3', title: 'col3',width:100, align:'center'}
]],
done: function(res, curr, count) {
console.log(res);
console.log(curr);
console.log(count);
layer.close(index);
}
});
upload.render({
elem: '#test1'
,url: 'global/DataService_Test.ashx?action=upload'
,accept: 'file'
,exts:'xlsx'
,done:function(res){
if(res==2){
layer.msg('上传成功!');
layui.table.reload(table1);
window.parent.location.reload();
}
else{
layer.msg('上传失败!');
}
}
});
});
}
ashx只贴上传代码了,这部分和之前用gridview上传的差不多,就是接收文件方式变了。
public void ProcessRequest(HttpContext context)
{
string action = context.Request["action"].ToString();
if(action=="upload"){
SqlConnection DB = new SqlConnection(WebConfigurationManager.AppSettings["key1"].ToString());
HttpFileCollection files = context.Request.Files;
string filename = System.IO.Path.GetFileName(files[0].FileName);
if (Directory.Exists(HttpContext.Current.Server.MapPath("/Content/excel")) == false)
{
Directory.CreateDirectory(HttpContext.Current.Server.MapPath("/Content/excel"));
}
string url = "/Content/excel/" + filename;
string path = HttpContext.Current.Server.MapPath(url);
files[0].SaveAs(path);
string conn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1;'";
OleDbConnection thisconnection = new OleDbConnection(conn);
thisconnection.Open();
string Sql = "select * from [Sheet1$]";
OleDbDataAdapter mycommand = new OleDbDataAdapter(Sql, thisconnection);
DataSet ds3 = new DataSet();
mycommand.Fill(ds3, "[Sheet1$]");
thisconnection.Close();
DB.Open();
int count = ds3.Tables["[Sheet1$]"].Rows.Count;
for (int i = 0; i < count; i++)
{
string col1,col2,col3;
col1= ds3.Tables["[Sheet1$]"].Rows[i]["col1"].ToString().Trim();
col2= ds3.Tables["[Sheet1$]"].Rows[i]["col2"].ToString().Trim();
col3= ds3.Tables["[Sheet1$]"].Rows[i]["col3"].ToString().Trim();
string excelsql = "insert into table(col1,col2,col3)values('"+col1+"','"+col2+"','"+col3+"')";
}
context.Response.Write("2");
}
}
如果要判断excel内容,需要在插入数据库那块用try catch,传递到js,js根据获取的值进行相应提示。暂时懒得写。
|