您的位置:首页 > 编程语言 > ASP

ASP.NET读取Excel文件的一些当时不懂的东西

2013-03-17 21:02 369 查看
1、读Excel文件,使用OleDb进行读取

2、读取Excel2003以前的文件,连接字符串为:"Provider=Microsoft.Jet.OLEDB.4.0;"+ "Data Source=" + strPath + ";" + "Extended Properties=Excel 8.0;" 。读取Excel2007的文件时,连接字符串为:"Provider=Microsoft.Ace.OleDb.12.0;" + "data source="+ strPath + ";Extended
Properties=Excel 12.0;"

3、参数HDR的值:HDR=YES,这代表第一行是标题,不做为数据使用 ,如果用HDR=NO,则表示第一行不是标题,做为数据来使用。系统默认的是YES

     参数Excel 8.0:对于Excel 97以上版本都用Excel 8.0

    IMEX ( IMport EXport mode )设置

    当 IMEX=0 时为“汇出模式”,这个模式开启的 Excel 档案只能用来做“写入”用途。

    当 IMEX=1 时为“汇入模式”,这个模式开启的 Excel 档案只能用来做“读取”用途。

    当 IMEX=2 时为“连結模式”,这个模式开启的 Excel 档案可同时支援“读取”与“写入”用途。

   注:  IMEX=1 解决数字与字符混合时,识别不正常的情况。IMEX的默认值为2,INEX=2时,没办法解决数字与字符混合时识别不正常的问题。当为时间格式例如“2011-11-10”的时候,要确保HDR=YES才可以解决识别不正常的问题。(这个注是转别人的,我没有遇到这种情况)

4、Excel的Sheet的名字就是表名,可通过SQL语句进行操作。

5、用SQL语句操作Excel时,表名是[Sheet1$],注意不要忘了“$”

6、解决文件上传控件的编辑框和按钮的样式问题:

<table>

<tr>

        <td>

            <asp:Label ID="lblAttachmentType" runat="server"></asp:Label></td>

        <td>

            <input readonly="readonly" class="inputNormal" id="<%=this.ClientID %>file" type="text"

                size="36" style="width: 100px;" name="f_file" hidefocus></td>

        <td>

            <div style="position: absolute; z-index: 2; cursor:hand;">

                <input id="fileAttachment"    style="filter: alpha(opacity=0); width: 20px; height:auto; cursor: hand;" type="file"

                    runat="server" />

            </div>

            <div style="position: absolute; z-index: 1; cursor: hand;">

                <img src="<%=ViewUpLoadImgUrl%>" style="cursor: hand;" />

            </div>

                                           

            <asp:ImageButton ID="btnUpload"    runat="server" ImageAlign="Middle"

                ImageUrl="~/App_Themes/Default/Images/btnUpload.gif" OnClick="btnUpload_Click" />

        </td>

        <td>

            <a runat="server" target="_blank" id="contenturl"></a>

        </td>

    </tr>

</table>

上面这段table是转的(网址:http://hi.baidu.com/paopaoface/item/a8665b808a33c2e7e496e0ca

自己使用的情况是:<div class="centerdiv">

            <div style="width:50%; margin:0px auto; margin-top:15px;">

                <span>学生信息文件:</span>

                <asp:TextBox runat="server" ID="tbx_FilePath" ReadOnly="true" Width="40%" CssClass="textBox"></asp:TextBox>  

                <span style=" position:absolute; z-index: 2; cursor:hand;">

                    <input id="file_UpLoad" type="file" runat="server" onchange="UpLoadFile();" accept="application/vnd.ms-excel" style="filter: alpha(opacity=0); width:40px; height:auto; cursor: hand;" />

                </span>

                <span style=" position:relative; z-index: 1;cursor: hand;">

                    <input type="button" value="浏览..." style="width:40px;" class="button" />

                </span>

            </div>

            <div style="width:10%; margin:0px auto; margin-top:15px;">

                <asp:Button runat="server" ID="btn_UpLoad" Text="上传" CssClass="button" OnClick="btn_ClickUpLoad" OnClientClick="return checkFile();" />

            </div>

        </div>

注意file_UpLoad所在的span的position的值和伪浏览按钮所在的span的position的值,其他参数具体使用时具体对待
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: