2014-01-21 4 views
0

로컬 시스템에 Excel 파일을 다운로드하기위한 코드를 작성하고 있습니다.다운로드 한 파일 저장 위치 요청

Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application(); 
Microsoft.Office.Interop.Excel.Workbooks workBooks = excelApp.Workbooks; 
Microsoft.Office.Interop.Excel.Workbook workBook = workBooks.Open(@"D:\Myfile.xlsx"); 
// Microsoft.Office.Interop.Excel.Worksheet workSheet = workBook.Worksheets.get_Item(1); 

workBook.SaveCopyAs(@"D:\Copy_Myfile.xlsx"); 
workBook.Close(); 

지금 요구하는 대신 D 드라이브에 파일을 저장하는, 그것은 위치를 desied에 파일을 저장하기 위해 사용자 요청해야합니다.

아이디어가 있으십니까?

+0

아마도 파일 찾아보기 대화 상자에서 물어볼 수 있습니까? –

+0

사용자는 서버에서 다운로드하여 로컬 드라이브에 저장해야합니까? 'Response.TransmitFile'처럼? – Taosique

답변

0
public static void TransmitFile(Page CurrentPage, string FilePath) 
// CurrentPage = this when called from CodeBehind 
{ 
    if (File.Exists(FilePath)) 
    { 
     string FileName = Path.GetFileName(FilePath); 
     string Extension = Path.GetExtension(FilePath).ToLowerInvariant(); 
     string ContentType; 

     switch (Extension) 
     { 
      case "xlsx": 
       ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
       break; 
      case "xls": 
       ContentType = "application/vnd.ms-excel"; 
       break; 
      case "csv": 
       ContentType = "text/csv"; 
       break; 
      default: 
       ContentType = "application/octet-stream"; 
       break; 
     } 

     if (CurrentPage != null) 
     { 
      CurrentPage.Response.ContentType = ContentType; 
      CurrentPage.Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName); 
      CurrentPage.Response.TransmitFile(FilePath); 
      CurrentPage.Response.End(); 
     } 
     else 
      throw new Exception("File transmission failed: cannot access current page"); 
    } 
    else 
     throw new Exception("File transmission failed: file not found"); 
} 
관련 문제