asp.net项目下载Excel文件

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

单sheet,传入Dataset:

 private void CreateExcel(DataSet ds)
        {
            string time = DateTime.Now.ToString("yyMMddHHmmss");
            string filename = "f" + time;

            HttpResponse resp;
            resp = Page.Response;
            resp.ContentEncoding = Encoding.GetEncoding("utf-8");
            resp.AppendHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");
            Response.ContentType = "application/ms-excel";
            string colHeaders = "", lsItem = "";
            DataTable dt = ds.Tables[0];
            DataRow[] myRow = dt.Select("");
            int i = 0;
            //取得数据表各列标题,各标题之间以\t分割,最后一个列标题后加回车符 
            for (i = 0; i < dt.Columns.Count; i++)
            {
                if (i == dt.Columns.Count - 1)
                {
                    colHeaders += dt.Columns[i].Caption + "\n";
                }
                else
                {
                    colHeaders += dt.Columns[i].Caption + "\t";
                }
            }
            resp.Write(colHeaders);
            foreach (DataRow row in myRow)
            {
                for (i = 0; i < dt.Columns.Count; i++)
                {
                    if (i == dt.Columns.Count - 1)
                    {
                        lsItem += row[i] + "\n";
                    }
                    else
                    {
                        lsItem += row[i] + "\t";
                    }
                }
                // 
                resp.Write(lsItem);
                lsItem = "";
            }
        }

多sheet,传入Dataset[]

private void CreateExcel(DataSet[] ds)
        {
            Response.ClearContent();
            Response.BufferOutput = true;
            Response.Charset = "utf-8";
            Response.ContentType = "application/ms-excel";
            Response.AddHeader("Content-Transfer-Encoding", "binary");
            Response.ContentEncoding = Encoding.UTF8;
            string time = DateTime.Now.ToString("yyMMddHHmmss");
            string filename = "data" + time;
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");
            Response.Write("");
            Response.Write(@"");
            for (int i = 0; i < ds.Length; i++)
            {
                DataTable dt = ds[i].Tables[0];
                DataRow[] myRow = dt.Select("");
                Response.Write("");
                Response.Write("");
                Response.Write("\r\n");
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    Response.Write("" + dt.Columns[j].ColumnName +
                                   "");
                }
                Response.Write("\r\n");
                foreach (DataRow row in myRow)
                {
                    Response.Write("");
                    for (int k = 0; k < dt.Columns.Count; k++)
                    {
                        Response.Write("" + row[k] +
                                       "");
                    }
                    Response.Write("");
                }
                Response.Write("");
                Response.Write("");
                //Response.Flush();
            }
            Response.Write("");
            Response.End();
        }


转载于:https://my.oschina.net/mandy4107/blog/480753

你可能感兴趣的:(asp.net项目下载Excel文件)