上传Excel文件

前台代码:

<fieldset id="f2" style="width: 780">
            <legend class="BodyLegent">
                <asp:Label ID="lblMessg" runat="server" ForeColor="#000000">[[Batch Add Org Data]]</asp:Label></legend>
            <br />
            <div class="Title" align="center">
                <asp:HyperLink ID="hylDownLoad" runat="server" Font-Size="Small" NavigateUrl="~/doc/OrgDataInputTemplate.xls"
                    Target="_blank">[[UploadDataDownloadTemplates]]</asp:HyperLink></div>
            <div class="list">
                <div class="Text">
                    <asp:Label ID="lblDocument" runat="server">[[ChooseToImportExcelFile]]</asp:Label></div>
                <div class="Control" style="width: 61.39%; height: 24px">
                    <input id="fileSource" style="width: 436px; height: 22px" type="file" name="fileSource"
                        runat="server" class="fileUpLoad" /></div>
            </div>
            <div class="list">
                <div class="Text">
                    <asp:Label ID="lblTableName" runat="server">[[TabelName]]</asp:Label></div>
                <div class="Control">
                    <asp:TextBox ID="txtTableName" runat="server"></asp:TextBox></div>
            </div>
            <div class="Control" style="width: 10%; height: 24px">
                 <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="[[Import]]" />
            </div>
        </fieldset>

 

后台代码:

using System.IO;

#region Excel文件上传
            if (fileSource.PostedFile.FileName == null || fileSource.PostedFile.FileName == "")
            {
                AlertMessage(language["please_select_file"]);
                return;
            }
            string tableName = txtTableName.Text.Trim();
            FileInfo fileTag = new FileInfo(fileSource.PostedFile.FileName);
            string fileType = fileTag.Extension;

            if (fileType != ".xls")
            {
                AlertMessage(language["please_select_xls_file"]);
                return;
            }

            string physicalPath = CommonManager.GetFilePath();
            string fileTime = DateTime.Now.ToString("yyyy_MM_dd_hh_mm_ss");
            string fileName = fileTime + fileType;
            //将上传文件保存到指定的文件夹下
            fileSource.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath) + physicalPath + fileName);
            #endregion

 

#region GetFilePath
    /// <summary>
    /// 获取文件上传路径
    /// </summary>
    /// <returns></returns>
    public static string GetFilePath()
    {
        return getConfigurationSettingsKey("fileUploadPath");
    }
    #endregion

 

    #region getConfigurationSettingsKey
    /// <summary>
    /// 返回Config中定义的Value
    /// </summary>
    /// <param name="SourceKey"></param>
    /// <returns></returns>
    private static string getConfigurationSettingsKey(string SourceKey)
    {
        return ConfigurationSettings.AppSettings[SourceKey];
    }
    #endregion

转载于:https://www.cnblogs.com/black263/archive/2010/06/15/1758547.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值