2013-06-05 4 views
0

안녕하세요 저는 SQL Server에서 Linq를 사용하여 SQL에서 WPF의 Excel 파일을 다운로드하는 데 사용하고있는 아래 클래스가 있습니다. 방법을 작동시키는 데 문제가 있습니다.이진 쓰기 .XLSX

public class Tables 
      { 
       public Guid Id { get; set; } 
       public byte[] Data { get; set; } 
       public string Notes{ get; set; }    
      } 

재산권이

public ObservableCollection<Tables> Table 
     { 
      get 
      { 
       return mTables; 
      } 
     } 

방법 - fileBytes 만에 전달 대리자 내에 존재하기 때문에 오류를 얻고있다

private void executeSaveAttachment(object parameter) 
      { 
       //Enables the apperance of a Dialog, where the user can specify where to save the file 
       SaveFileDialog textDialog = new SaveFileDialog(); 

       //save the file in a bite array 

       // byte[] fileBytes = Table.ToList().ForEach(p => p.Data); 
       Table.ToList().ForEach(p => 
       { 
        byte[] fileBytes = p.Data; 
       }); 

       //Open dialog where the user determines where to save the file. 
       bool? result = textDialog.ShowDialog(); 
       if (result == true) 
       { 
        using (Stream fs = (Stream)textDialog.OpenFile()) 
        { 
         fs.Write(fileBytes, 0, fileBytes.Length); 
         fs.Close(); 
        } 
       } 
      } 
+1

가 아래에'if' 문장의 맥락에서 사용할 수 없습니다 귀하의'fileBytes' 배열은 루프 내에서 초기화를 들어. –

답변

1

을 (오류 fileBytes이 현재 컨텍스트에 표시되지 않습니다) 각각. 이 시도 :

private void executeSaveAttachment(object parameter) 
{ 
    using (var dlg = new SaveFileDialog()) 
    { 
     foreach (var table in Table) 
     { 
      if (dlg.ShowDialog() ?? false) 
      { 
       File.WriteAllBytes(dlg.FileName, table.Data) 
      } 
     } 
    } 
} 
+0

'Microsoft.Win32.SaveFileDialog': using 문에 사용 된 형식이 'System.IDisposable'로 암시 적으로 변환 가능해야합니다. – KeyboardFriendly

+1

.NET 프레임 워크에 SaveFileDialog 클래스가 3 개 있습니다. 하나는 WinForms (System.Windows.Forms.SaveFileDialog), WPF (System.Windows.Controls.SaveFileDialog)와 Microsoft.Win32 네임 스페이스에 대한 설명 (마지막 부분이 무엇인지에 대한 확실한 설명이 아님) 당신이 WinForms를 사용하고 있다면, 첫 번째 것을 사용하십시오. 그것은 일회용이므로, using 키워드를 사용하십시오. 다른 키워드 중 하나를 사용하는 경우 일회용 키워드가 아니므로 using 키워드를 삭제할 수 있습니다. –

0

WPF

private void executeSaveAttachment(object parameter) 
     { 
      SaveFileDialog dlg = new SaveFileDialog(); 
      { 
       foreach (var table in Table) 
       { 
        if (dlg.ShowDialog() ?? false) 
        { 
         File.WriteAllBytes(dlg.FileName, table.Data); 
        } 
       } 
      } 
     }