Exemple #1
0
    /// <summary>
    /// ds2012
    /// 循环遍历DataTable数据,导出Excel
    /// </summary>
    /// <param name="dt"></param>
    /// <param name="title"></param>
    /// <param name="colsName"></param>
    public static void OutToExcel(System.Data.DataTable dt, string title, string[] colsName)
    {
        System.Web.UI.Page    page = new System.Web.UI.Page();
        System.Data.DataTable ddt  = new System.Data.DataTable();
        for (int i = 0; i < colsName.Length; i++)
        {
            DataColumn dc = new DataColumn(colsName[i].Split('=')[1]);
            ddt.Columns.Add(dc);
        }
        for (int k = 0; k < dt.Rows.Count; k++)
        {
            DataRow dr = ddt.NewRow();
            for (int i = 0; i < colsName.Length; i++)
            {
                dr[colsName[i].Split('=')[1]] = dt.Rows[k][colsName[i].Split('=')[0]];
            }
            ddt.Rows.Add(dr);
        }

        System.Web.UI.WebControls.DataGrid dataGrid = new System.Web.UI.WebControls.DataGrid();
        dataGrid.DataSource                  = ddt.DefaultView;
        dataGrid.AllowPaging                 = false;
        dataGrid.HeaderStyle.BackColor       = System.Drawing.Color.Gray;
        dataGrid.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
        dataGrid.HeaderStyle.Font.Bold       = true;
        dataGrid.ItemStyle.HorizontalAlign   = HorizontalAlign.Left;
        dataGrid.DataBind();
        System.IO.StringWriter       tw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
        dataGrid.RenderControl(hw);

        if (!Directory.Exists(page.Server.MapPath("../UserExcel/")))
        {
            Directory.CreateDirectory(page.Server.MapPath("../UserExcel/"));
        }
        string fullpath = page.Server.MapPath("../UserExcel/FileName.xls");

        System.IO.StreamWriter sw = System.IO.File.CreateText(fullpath);

        sw.Write(tw.ToString());
        sw.Close();

        string where = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
        //压缩后的目标文件
        string   zipPath = page.Server.MapPath("../UserExcel/") + where + ".rar";
        ZipClass Zc      = new ZipClass();
        string   err     = "";

        Zc.ZipFile(fullpath, zipPath, out err);

        //删除压缩前的文件
        System.IO.File.Delete(fullpath);

        System.IO.FileInfo file = new System.IO.FileInfo(zipPath);

        System.Web.HttpContext.Current.Response.Clear();
        System.Web.HttpContext.Current.Response.ClearContent();
        System.Web.HttpContext.Current.Response.ClearHeaders();
        System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(file.Name).Replace('+', ' '));
        System.Web.HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
        System.Web.HttpContext.Current.Response.AddHeader("Content-Transfer-Encoding", "binary");
        System.Web.HttpContext.Current.Response.ContentType     = "application/zip";
        System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
        System.Web.HttpContext.Current.Response.WriteFile(file.FullName);
        System.Web.HttpContext.Current.Response.Flush();
        System.Web.HttpContext.Current.Response.End();
    }
Exemple #2
0
    protected void btnUpLoad_Click(object sender, EventArgs e)
    {
        ResourcesBLL resources = new ResourcesBLL();

        string mfileName = "";

        if (this.txtjianjie.Text.Trim().Length > 50)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "alert('" + GetTran("001502", "输入的简介超过50个字符") + "')");
            return;
        }

        if (this.txtResName.Text.Trim() == "")
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + GetTran("006938", "资料名称和资料简介不能为空!") + "')</script>");
            return;
        }

        if (!System.IO.Directory.Exists(Server.MapPath("~/Company/upLoadRes/")))
        {
            System.IO.Directory.CreateDirectory(Server.MapPath("~/Company/upLoadRes/") + "\\");
        }

        if (Request.QueryString["action"] != "edit")
        {
            string filepath = upFile.PostedFile.FileName;//获取要上传的文件的说有字符,包括文件的路径,文件的名称文件的扩展名称

            //验证上传文件后缀名
            string s = filepath.Substring(filepath.LastIndexOf(".") + 1);
            s = s.ToLower();
            if (s != "rar" && s != "zip")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('您上传的文件格式不正确,请重新上传!')</script>");
                return;
            }


            try
            {
                string mPath = Server.MapPath("~/Company/upLoadRes/");
                mfileName = filepath.Substring(filepath.LastIndexOf("\\") + 1);

                if (mfileName.Length > 30)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('资料文件名不得大于30个字符!')</script>");
                }
                else
                {
                    int sizeLength = upFile.PostedFile.ContentLength;
                    if (sizeLength > 4 * 1024 * 1024)
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "", "alert('上传文件不能超过4MB')", true);
                        return;
                    }
                    string ressize;//文件大小
                    if (sizeLength <= 1203)
                    {
                        ressize = upFile.PostedFile.ContentLength.ToString() + "B";
                    }
                    else
                    {
                        ressize = Convert.ToString(upFile.PostedFile.ContentLength / 1204) + "K";
                    }

                    string resname  = this.txtResName.Text.Trim();
                    string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
                    string resdes   = this.txtjianjie.Text.Trim();


                    string realName = DateTime.Now.Millisecond.ToString() + mfileName;
                    upFile.PostedFile.SaveAs(mPath + "\\" + realName);

                    if (CheckFileForm(mPath + "\\" + realName) <= 0)
                    {
                        File.Delete(mPath + "\\" + realName);
                        ClientScript.RegisterStartupScript(this.GetType(), "", "alert('" + GetTran("000823", "上传文件格式不正确!") + "');", true);
                        return;
                    }//图片文件上传后进行压缩再删除原有图片
                    else if (CheckFileForm(mPath + "\\" + realName) == 2)
                    {
                        ZipClass zc       = new ZipClass();
                        string   err      = "";
                        string   zipPath1 = mPath + realName;
                        string   zipPath  = zipPath1.Replace(zipPath1.Substring(zipPath1.LastIndexOf(".") + 1), "rar");
                        realName = realName.Replace(realName.Substring(realName.LastIndexOf(".") + 1), "rar");
                        bool res = zc.ZipFile(zipPath1, zipPath, out err);
                        if (res)
                        {
                            File.Delete(zipPath1);
                        }
                        else
                        {
                            labUpInfo.Text = err;
                            return;
                        }
                    }

                    RecordResInfo(resname, realName, resdes, ressize, DateTime.UtcNow.ToLongDateString());
                    this.txtjianjie.Text = "";
                    this.txtResName.Text = "";
                }
            }
            catch (Exception)
            {
                //throw;
                labUpInfo.Text = "上传失败!该资料的操作权限不够,请设置权限再进行上传!";
            }
        }
        else
        {
            string resname = this.txtResName.Text.Trim();
            string resdes  = this.txtjianjie.Text.Trim();

            UpdateResInfo(resname, "", resdes, "");
            this.labUpInfo.Text = GetTran("001505", "资料修改成功") + "!";
        }
    }