我正在如下所示的代码中创建一个 excel 文件
Microsoft.Office.Interop.Excel.Application excelFile = CreateExcelFile();
现在我想将这个 excelFile 转换为 byte[] 而不保存到硬盘。怎么可能?
最佳答案
前段时间遇到过同样的问题。您必须创建一个临时文件,然后将其读入字节数组。
示例代码:
string tempPath = AppDomain.CurrentDomain.BaseDirectory + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond + "_temp";//date time added to be sure there are no name conflicts
workbook.SaveAs(tempPath, workbook.FileFormat);//create temporary file from the workbook
tempPath = workbook.FullName;//name of the file with path and extension
workbook.Close();
byte[] result = File.ReadAllBytes(tempPath);//change to byte[]
File.Delete(tempPath);//delete temporary file
关于c# - 将 Microsoft.Office.Interop.Excel.Application 转换为 byte[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22534745/