简体   繁体   English

将二进制文件写入.Xlsx

[英]Writing Binary to .Xlsx

Hello I have the below class I am using to download Excel Files from sql server using Linq to Sql in WPF. 您好,我有下面的类,我正在使用此类从Linq到WPF中的Sql从sql服务器下载Excel文件。 I am having problems getting the method to work. 我在使方法起作用时遇到问题。

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

Property 属性

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

The method (Error - fileBytes does not appear in the current context) 方法(错误-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();
                    }
                }
            }

You are getting the error because fileBytes only exists within the delegate passed to ForEach. 您收到错误消息是因为fileBytes仅存在于传递给ForEach的委托中。 Try this: 尝试这个:

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

For WPF 对于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);
                    }
                }
            }
        }  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM