将Binary写入.Xlsx

时间:2013-06-05 19:57:46

标签: c# lambda

您好我有以下类用于从Windows服务器使用Linq到Sql下载Excel文件。我在使用该方法时遇到问题。

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();
                    }
                }
            }

2 个答案:

答案 0 :(得分:1)

您收到错误是因为fileBytes仅存在于传递给ForEach的委托中。试试这个:

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)
            }
        }
    }
}

答案 1 :(得分: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);
                    }
                }
            }
        }