欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net上传execl文件后,在页面上加载显示(示例代码)

程序员文章站 2024-02-27 13:15:27
复制代码 代码如下:    #region 上传execl文件    protected void button...

复制代码 代码如下:

    #region 上传execl文件
    protected void button1_click(object sender, eventargs e)
    {
        if (fileupload1.hasfile)
        {
            string newfilename = string.empty;
            string errormess = uploadfile(fileupload1, ".xls|.xlsx", 1024 * 5, server.mappath("/report/socialapply/"), 1, out newfilename);
            if (string.isnullorempty(errormess))
            {
                label1.text = "√文件上传成功";
                viewstate["uploadfile"] = "/report/socialapply/" + newfilename;
                try
                {
                    filestream file = new filestream
                        (server.mappath(viewstate["uploadfile"] as string),
                        filemode.open, fileaccess.read,
                        fileshare.readwrite);
                    datatable dt1 = fairhr.util.xmlexcelreport.readexceltodatatable(file, 0, 0);

                    viewstate.add("viewdt", dt1);
                    gridview1.datasource = viewstate["viewdt"] as datatable;
                    gridview1.databind();
                    file.close();
                    //maticsoft.common.messagebox.responsescript(page, "$.messager.alert('系统提示', '操作成功!', 'info');");
                }
                catch
                {
                    maticsoft.common.messagebox.responsescript(page, "$.messager.alert('系统提示', '请重新上传execl文件再操作', 'warning');");
                }
            }
            else
            {
                label1.text = "×文件上传失败";
            }
        }
        else
        {
            label1.text = "×请先选择上传文件";
        }
        scriptmanager.registerclientscriptblock(page, this.gettype(), "idalert1", "closeload();", true);
    }
    #endregion

    #region 上传文件
    /// <summary>
    /// 上传文件
    /// </summary>
    /// <param name="fu">上传文件</param>
    /// <param name="type">上传文件类型,例如.jpg|.gif|.bmp</param>
    /// <param name="size">限制上传文件大小,单位为k</param>
    /// <param name="path">上传路径,需使用server.mappath</param>
    /// <param name="nametype">1为自动命名,0用原名</param>
    /// <returns></returns>
    public static string uploadfile(fileupload fu, string type, int size, string path, int nametype, out string newfilename)
    {
        newfilename = null;
        string erorr = null;
        int size = fu.postedfile.contentlength / 1024;
        if (size > size)
        {
            erorr = "上传文件太大!";
            return erorr;
        }
        string type = fu.filename;
        if (type.indexof(".") == -1) { erorr = "上传文件类型有误!"; return erorr; }
        type = type.substring(type.lastindexof(".")).toupper();
        type = type.toupper();
        if (type.indexof(type) == -1) { erorr = "上传文件类型有误!"; return erorr; }

        string filename = "";
        if (nametype == 1)
        {
            string nowdate = datetime.now.tostring();
            nowdate = nowdate.replace(":", "").replace(" ", "").replace("-", "").trim();
            random r = new random();
            int a = r.next(1000);
            filename = nowdate + a.tostring() + type;
            newfilename = filename;
        }
        else
        {
            filename = fu.filename;
            if (system.io.file.exists(path + filename)) { erorr = "此文件名已经存在!"; return erorr; }
        }

        fu.saveas(path + filename);
        return erorr;//返回有错的错误信息,没有错误返回null
    }
    #endregion