2014-01-26 4 views
1
Microsoft.Office.Interop.Excel.Application xlApp; 
Microsoft.Office.Interop.Excel.Workbook xlWorkBook; 
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet; 
object misValue = System.Reflection.Missing.Value; 
xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass(); 
xlWorkBook = xlApp.Workbooks.Add(misValue); 

xlWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 
xlWorkSheet.Cells[1, 1] = "http://csharp.net-informations.com"; 

xlWorkBook.SaveAs("csharp-Excel.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); 
xlWorkBook.Close(true, misValue, misValue); 
xlApp.Quit(); 

여기에 C# 버튼 코드가 ​​있습니다. 나는 버튼 클릭만으로이 파일을 자동으로 다운로드하고 싶다. 이 코드는 엑셀 파일 만 생성합니다. 다운로드 코드를 추가하는 방법. 나는 모른다. 예를 들어 Google 크롬에서 아무 것도 다운로드 한 다음 항목 시작 다운로드를 클릭합니다. 브라우저 다운로드 파일의 맨 아래에 당신은 당신이 저장하는 엑셀 파일 경로를 알고 아래에이 작업을 수행하는 방법을 ,, 엑셀 당신이 만든 것을 알고 이제 동적으로 Excel 파일을 만들고 자동으로 다운로드하십시오.

xlWorkBook.SaveAs("csharp-Excel.xls" 

파일 코드에서 사전

+0

안녕하세요 @의 user2849657을 생성하는 객체의 인스턴스를 생성 위치 (~/CSHARP-Excel.xls) 에있는 서버에 저장 . http://meta.stackexchange.com/a/5235/253846 – Fllo

답변

1

나에게 감사를 도와주세요 당신을 위해 일하는 아래의 대답은 그것을 받아 들여야 경우 엑셀 파일은 Excel 파일을

string path = Server.MapPath("~/csharp-Excel.xls"); 
    System.IO.FileInfo file = new System.IO.FileInfo(path); 
    string Outgoingfile = "FileName.xlsx"; 
    if (file.Exists) 
     { 
      Response.Clear(); 
      Response.ClearContent(); 
      Response.ClearHeaders(); 
      Response.AddHeader("Content-Disposition", "attachment; filename=" + Outgoingfile); 
      Response.AddHeader("Content-Length", file.Length.ToString()); 
      Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
      Response.WriteFile(file.FullName); 
      Response.Flush(); 
      Response.Close(); 

     } 
    else 
     { 
      Response.Write("This file does not exist."); 
     } 
+0

Thanks Brother :)를 참조하십시오. –

관련 문제