2011-09-22 2 views
1

데이터를 테이블에 다운로드하고 그림을 Excel 스프레드 시트에 다운로드해야합니다. 현재 웹 사이트에서 Excel 스프레드 시트를 만들 수 있지만 데이터를 스프레드 시트에 어떻게 가져 옵니까?ASP.Net 웹 페이지에서 Excel 파일로 데이터를 다운로드하는 방법은 무엇입니까?

protected void btn_ExcelDownload_Click(object sender, EventArgs e) 
{ 
    string path = Server.MapPath(""); 
    path = path + @"\Resources\MessageStatus.xlsx"; 
    string name = Path.GetFileName(path); 
    Response.AppendHeader("content-disposition", "attachment; filename=" + name); 
    Response.ContentType = "Application/msword"; 
    Response.WriteFile(path); 
    Response.End(); 
} 
+0

를 사용하여이 일을했다. 그리드를 사용하고 있다면 쉽게 할 수 있습니다. 그렇지 않으면 자신 만의 내보내기 논리를 작성해야합니다. – Asdfg

답변

1

나는이 클래스는 엑셀 시트에 콘텐츠를 수출해야

void WriteToXls(string fromfilePath, string targetFileName) 
    { 
     if (!String.IsNullOrEmpty(fromfilePath)) 
     { 
      HttpResponse response = HttpContext.Current.Response; 
      response.Clear(); 
      response.Charset = "utf-8"; 
      response.ContentType = "text/xls"; 
      response.AddHeader("content-disposition", string.Format("attachment; filename={0}", targetFileName)); 
      response.BinaryWrite(File.ReadAllBytes(fromfilePath)); 
      response.End(); 
     } 
    } 
관련 문제