상세 컨텐츠

본문 제목

데이터셋 엑셀다운로드, CSV다운로드 2가지

C++,C#, ASP.NET

by 김일국 2012. 10. 3. 16:18

본문

#region 사용자 함수
    protected void Csv_Download(object sender, ImageClickEventArgs e)
    {
        Hashtable hash = new Hashtable();
        string filename = SpreadHelper.spGetCodeList(spList, true, 3).ToString();
        hash["EDUCATIONAL_GUBN"] = EducationalGubn;
        hash["YEAR"] = GetValue(ddlSearchYear);
        hash["HAGGI_GUBN"] = GetValue(ddlSearchHaggiGubn);
        hash["SUBJECT_CD"] = SpreadHelper.spGetCodeList(spList, true, 9).ToString();
        hash["USER_NO"] = SpreadHelper.spGetCodeList(spList, true, 10).ToString();
        hash["CLASS_CD"] = SpreadHelper.spGetCodeList(spList, true, 12).ToString();
        DataSet ds;
        using (CECDac dac = new CECDac())
        {
            ds = dac.SelectSC250M(hash);
        }
        /* 엑셀로 다운로드
        DataGrid dgExcel = new DataGrid();
        dgExcel.ShowHeader = true;
        dgExcel.Caption = "수강생명부사전보고(" + filename + ")";
        dgExcel.DataSource = ds.Tables[0];
        dgExcel.DataBind();
        dgExcel.HeaderStyle.BackColor = System.Drawing.Color.AntiqueWhite;
        dgExcel.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
        dgExcel.HeaderStyle.Height = 25;
        System.IO.StringWriter sWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(sWriter);
        dgExcel.RenderControl(htmlWriter);
        string fileName = HttpUtility.UrlPathEncode(filename) + ".xls";

        System.Web.HttpContext.Current.Response.Clear();
        System.Web.HttpContext.Current.Response.AddHeader("content", "text/html; charset=utf-8");
        System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment;filename={0}", fileName));
        System.Web.HttpContext.Current.Response.ContentType = "application/vnd.msexcel";
        System.Web.HttpContext.Current.Response.Write(sWriter.ToString());
        System.Web.HttpContext.Current.Response.Flush();
        System.Web.HttpContext.Current.Response.Close();
        System.Web.HttpContext.Current.Response.End();

        dgExcel.Dispose();
         */
        /* CSV로 다운로드 */
        string strFilePath = Server.MapPath("~/download.csv");
        System.IO.StreamWriter sw = new System.IO.StreamWriter(strFilePath, false, System.Text.Encoding.UTF8);
        // First we will write the headers.
        DataTable dt = ds.Tables[0];
        int iColCount = dt.Columns.Count;
        for (int i = 0; i < iColCount; i++)
        {
            sw.Write(dt.Columns[i]);
            if (i < iColCount - 1)
            {
                sw.Write(",");
            }
        }
        sw.Write(sw.NewLine);
        // Now write all the rows.
        foreach (DataRow dr in dt.Rows)
        {
            for (int i = 0; i < iColCount; i++)
            {
                if (!Convert.IsDBNull(dr[i]))
                {
                    sw.Write(dr[i].ToString());
                }
                if (i < iColCount - 1)
                {
                    sw.Write(",");
                }
            }
            sw.Write(sw.NewLine);
        }
        sw.Close();
        string fileName = HttpUtility.UrlPathEncode(filename) + ".csv";
        Response.Clear();
        Response.ContentType = "application/csv";
        Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
        Response.WriteFile(strFilePath);
        Response.Flush();
        Response.End();
    }
    #endregion

관련글 더보기

댓글 영역