将二进制文件写入. xlsx
本文关键字:xlsx 二进制文件 | 更新日期: 2023-09-27 18:17:00
你好,我有下面的类,我使用在WPF中使用Linq到sql从sql server下载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();
}
}
}
您正在获得错误,因为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)
}
}
}
}
For 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);
}
}
}
}