2013年6月5日 星期三

C# Excel to GridView

[C#]Excel to GridView

HTML:
<form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" />
        <br />
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
        <asp:Label ID="lbl_Err" runat="server" Text="Label"></asp:Label><br />
        <div runat="server" clientidmode="Static" id="DIV1">
        </div>
    </div>
</form>

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Test_FileUpload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
     
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            string FileFullPath = string.Empty;
            DataSet ds = null;
            DataTable dt = null;

            File_Upload flu = new File_Upload();
            flu.Set_AllowedExtension = "EXCEL";
            flu.UploadXLS(FileUpload1);
         
            ImportData imp = new ImportData();
            ds = imp.GetExcelData(flu.Get_SavePath + flu.Get_FileName);
         
            dt = ds.Tables[0];
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        catch (Exception ex)
        {
            lbl_Err.Text = ex.Message;
        }
    }
}

沒有留言:

張貼留言