我正在代码中创建一个excel文件,如下所示
Microsoft.Office.Interop.Excel.Application excelFile = CreateExcelFile();
现在我想将此 excel 文件转换为 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)