把EXCEL上传并BINDING到GRIDVIEW中

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Import Namespace = "System.IO" %>
<script language = "C#" runat = "server">

</script>

<html>
<head>
<title>上传文件</title>
</head>
<body>
<div align="center">

<table width="100%" border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#eeeeee" id="AutoNumber1">
<form id="formFile" method="post" action="" enctype="multipart/form-data" runat="server">
<tr>
<td align="center" style="height: 22px">
<input type="file" id="inputFile" name="inputFile" runat="server" size="64">
</td>
</tr>
<tr>
<td align="center">
<input type="button" value="Upload" OnServerClick="uploadFile" id="Button2" name="Button1" runat="server">
</td>
</tr>
<tr>
<td align="center">
<asp:Label id="labelUpResult" runat="server" text="" font-bold="True" forecolor="#FF0000"/><br>
<asp:Label id="labelFileName" runat="server" text="" font-bold="True" forecolor="#FF0000"/><br>
<asp:Label id="labelFileExt" runat="server" text="" font-bold="True" forecolor="#FF0000"/><br>
<asp:Label id="labelFileSize" runat="server" text="" font-bold="True" forecolor="#FF0000"/><br>
  <asp:GridView ID="GridView1" runat="server" Height="182px" Width="799px">
    </asp:GridView>
</td>
</tr>
</form>
</table>

 

</div>
 
</body>
</html>





using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void uploadFile(object sender, EventArgs E)
    {
        //检查上传文件不为空
        if (inputFile.PostedFile.ContentLength > 0)
        {
            //设定上传文件的保存路径
            string strSaveDir = "./upload/";
            string strName = inputFile.PostedFile.FileName;
            //取得文件名(抱括路径)里最后一个"."的索引
            int intExt = strName.LastIndexOf(".");
            //取得文件扩展名
            string strExt = strName.Substring(intExt);
            //这里我自动根据日期和文件大小不同为文件命名,确保文件名不重复
            /*
            DateTime datNow = DateTime.Now;
            string strNewName = datNow.DayOfYear.ToString() + inputFile.PostedFile.ContentLength.ToString() + strExt; */
            //取得文件名(包括路径)里最后一个"\"的索引
            int intPath = strName.LastIndexOf("\\");
            //取得文件名(不包括路径)
            string strNewName = strName.Substring(intPath);
            //保存文件到你所要的目录,这里是IIS根目录下的upload目录.你可以改变.
            //注意: 我这里用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"必须用"\\"代替
            inputFile.PostedFile.SaveAs(Server.MapPath(strSaveDir + strNewName));
            //得到这个文件的相关属性:文件名,文件类型,文件大小
            this.GridView1.DataSource = ExcelDataSource(Server.MapPath(strSaveDir + strNewName), "Sheet1");
            this.GridView1.DataBind();
            labelUpResult.Text = "上传成功!";
            labelFileName.Text = "文件源:" + strName;
            labelFileExt.Text = "文件类型:" + inputFile.PostedFile.ContentType + "( " + strExt + " )";
            labelFileSize.Text = "文件大小:" + (inputFile.PostedFile.ContentLength / 1024).ToString() + " K Byte(s)";
        }
        else
        {
            labelUpResult.Text = "请选择你要上传的文件!";
            labelFileName.Text = "";
            labelFileExt.Text = "";
            labelFileSize.Text = "";
        }
    }
    //该方法实现从Excel中导出数据到DataSet中,其中filepath为Excel文件的绝对路径,sheetname为表示那个Excel表;
    public DataSet ExcelDataSource(string filepath, string sheetname)
    {
        string strConn;
        strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0;";
        OleDbConnection conn = new OleDbConnection(strConn);
        OleDbDataAdapter oada = new OleDbDataAdapter("select * from [" + sheetname + "$]", strConn);
        DataSet ds = new DataSet();
        oada.Fill(ds);
        return ds;
    }
}




<?xml version="1.0"?>
<!--
    注意: 除了手动编辑此文件以外,您还可以使用
    Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
     “网站”->“Asp.Net 配置”选项。
    设置和注释的完整列表在
    machine.config.comments 中,该文件通常位于
    \Windows\Microsoft.Net\Framework\v2.x\Config 中
-->
<configuration>
 <appSettings/>
 <connectionStrings/>
 <system.web>
  <!--
            设置 compilation debug="true" 将调试符号插入
            已编译的页面中。但由于这会
            影响性能,因此只在开发过程中将此值
            设置为 true。
        -->
  <compilation debug="true">
   <assemblies>
    <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
    <add assembly="System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
    <add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
    <add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies></compilation>
  <!--
            通过 <authentication> 节可以配置 ASP.NET 使用的
            安全身份验证模式,
            以标识传入的用户。
        -->
  <authentication mode="Windows"/>
  <!--
            如果在执行请求的过程中出现未处理的错误,
            则通过 <customErrors> 节可以配置相应的处理步骤。具体说来,
            开发人员通过该节可以配置
            要显示的 html 错误页
            以代替错误堆栈跟踪。

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        -->
    <httpRuntime   maxRequestLength="10240"   />
  </system.web>
</configuration>


posted @ 2009-08-08 12:40  夜色狼  阅读(260)  评论(0编辑  收藏  举报