Excel导入与fileupload控件

  当时知道FileUpload控件的时候并没有学习UpdatePanel,所以也不知道不可以异步提交,虽然通过搜索园子,知道可以用iframe来实现,但是个人感觉,上传完刷新一下可能感觉更好,那么就需要具有一个postback的控件(比如Button控件),然后在UpdatePanel中的Trigger一个postback的button控件,就可以在UpdatePanel中使用了。

  Excel导入的话可以采用以下方式,需要服务器端提供microsoft.ace.oled组件,我采用的是ACCESS DATA ENGINE 2007版的,似乎2010版的在64位的Win2008上会造成上传假死。

///<summary>
/// 获取Excel中的表名
///</summary>
///<param name="path"></param>
private static string GetTableName(string path, FileType fileType)
{
string strConn = GetConnectString(path, fileType);
OleDbConnection conn = null;
using (conn = new OleDbConnection(strConn))
{
conn.Open();
DataTable schemaTable = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
return schemaTable.Rows[0][2].ToString().Trim();
}
}


读取Excel中此表名的去除重复的数据

 1         private DataSet GetDataFromExcel(string path, string tableName, FileType fileType)
2 {
3 string strConn = GetConnectString(path, fileType);
4 OleDbConnection conn = null;
5 using (conn = new OleDbConnection(strConn))
6 {
7 conn.Open();
8 string strExcel = "";
9 OleDbDataAdapter myCommand = null;
10 DataSet ds = null;
11 strExcel = "select distinct * from [" + tableName + "]";
12 myCommand = new OleDbDataAdapter(strExcel, strConn);
13 ds = new DataSet();
14 myCommand.Fill(ds, "table1");
15 return ds;
16 }
17 }
 

 连接字符串,因为jet组件已经到4.0版本停用,所以试着用了几个字符串,最后决定使用2007推荐的字符串,其对2010的EXCEL支持的也很好,当然了一贯的微软兼容性的体现,其对excel97-2003支持的也很好。

private static string GetConnectString(string path, FileType fileType)
{
//if (fileType == FileType.excel97_2003)
// return "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + path + ";Extended Properties=Excel 8.0;";
return "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0";
//return "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0 Xml;HDR=No;IMEX=1\"";
}

 

再说说fileupload控件,因为原型上用的是导入按钮来表示文件导入,所以测试部的同学也要求我来这样做,所以又复习了下css。。。
 用div的方式来掩盖住浏览器

<div class="myFileUploadDiv">
<input type="button" class="but_00 but_06 margin_right5px" style="position:absolute;top:0;left:0;" value="导入" />
<asp:FileUpload ID="fileUpload1" runat="server" CssClass="hiddenFileUpload" οnchange="uploadFile();"/>
</div>

样式表是这样的

<style type="text/css">
.hiddenFileUpload
{
position
: absolute;
top
: 0;
left
: 0;
width
: 61px;
height
: 26px;
line-height
: 32px;
padding
: 0px 0px 0px 20px;
font-size
: 14px;
font-weight
: bold;
color
: #FFF;
border
: none;
cursor
: pointer;
overflow
: hidden;
display
: block;
background-position
: 0px -544px;
padding
: 0px 0px 0px 5px;
margin-right
: 5px;
opacity
: 0;
z-index
: 1;
}
.myFileUploadBtn
{
position
: absolute;
top
: 0;
left
: 0;
}
.myFileUploadDiv
{
width
: 91px;
height
: 34px;
line-height
: 32px;
padding
: 0px 0px 0px 20px;
font-size
: 14px;
font-weight
: bold;
color
: #FFF;
border
: none;
overflow
: hidden;
display
: block;
position
: relative;
}
</style>

js是这样的,效果就是在选择完要上传的文件之后,自动点击隐藏的button

        function uploadFile() {
if ($("#<%=fileUpload1.ClientID %>").val() != "")
$("#<%=btnFileUpload.ClientID %>").click();
}

以上的样式与JS就会让浏览按钮隐藏在导入按钮之上,可能这样之后会导致按钮没有按住的效果,所以应该可以尝试用fileupload的点击事件去模拟按钮的效果,这里就不再进行尝试了。

以上就是最近的项目中用到的,聊以记录,备忘。


 

转载于:https://www.cnblogs.com/green-4984/archive/2011/11/05/2237064.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值