日期:2014-05-16  浏览次数:20904 次

asp.net操作读取excel的两种方法

本文通过上传一个excle文件,并规定具体格式,读取数据填充到gridview中

前台页面代码如下:

 <div style=" width:500px; height:400px; text-align:center">
        <form id="form1" runat="server" enctype="multipart/form-data">
        <asp:Label runat="server">我们只支持excel(.xls/.xlsx)的上传,模板点击这里下载</asp:Label>
        <asp:Button ID="LoadTemplate" runat="server" Text="下载模板" OnClick="LoadTemplate_Click"/>
        <div id="MyFile" style=" float:left; margin-top:20px; margin-bottom:20px">
            <input name="File" type="file" class="FileUpStyle" id="File1" />
             <asp:Button runat="server" Text="上传" ID="Upload" BorderColor="Desktop" BorderWidth="1px"
            Height="20px" Width="60px" OnClick="Upload_Click"></asp:Button>
        </div>
        <asp:GridView ID="GridView1" BorderColor="Black" runat="server" AutoGenerateColumns="False"
            Font-Size="12px" Width="530px" AllowSorting="True">
            <Columns>
                <asp:BoundField DataField="holiday" HeaderText="holiday" />
            </Columns>
            <HeaderStyle BackColor="Azure" Font-Size="12px" HorizontalAlign="Center" />
            <RowStyle HorizontalAlign="Center" />
            <PagerStyle HorizontalAlign="Center" />
        </asp:GridView>
        </form>
    </div>

?

方法一是使用office提供的类库来获取:

 static string filePath = string.Empty;

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="path"></param>
        private void DeleteFile(DirectoryInfo path)
        {
            foreach (DirectoryInfo d in path.GetDirectories())
            {
                DeleteFile(d);
            }
            foreach (FileInfo f in path.GetFiles())
            {
                f.Delete();
            }
        } 

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Upload_Click(object sender, EventArgs e)
        {
            HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;

            for (int i = 0; i < files.Count; i++)
            {
                string ShortFileName = files[i].FileName.Substring(files[i].FileName.LastIndexOf("\\") + 1);
                string type = ShortFileName.Substring(files[i].FileName.LastIndexOf(".") + 1);
                if (ShortFileName == "")
                {
                    Response.Write("<script>alert('未选择文件!');</script>");
                }
                else if (!type.ToLower().Equals("xls") && !type.ToLower().Equals("xlsx"))
                {
                    Response.Write("<script>alert('文件格式不正确!必须是(.xls/.xlsx)');</script>");
                }
                else if (ShortFileName != "" && (type.ToLower().Equals("xls") || type.ToLower().Equals("xlsx")))
                {
                    filePath = Server.MapPath("~/bin/Files/ExcelFiles/" + ShortFileName);

                    DeleteFile(new DirectoryInfo(Server.MapPath("~/bin/Files/ExcelFiles/")));

                    files[i].SaveAs(filePath);
                    DataSet ds = ReadExcel(filePath,type);
                    if (ds == null)
                    {
                        Response.Write("<script>alert('文件格式不正确!');</script>");
                        return;
                    }
                    bool folat = ISQualified(ds);
                    if (folat)
                    {
                        Response.Write("<script>alert('日期格式不正确!应该为yyyy-MM-dd');</script>");
                    }
                    else
                    {
                        Save(ds);