gpt4 book ai didi

c# - 将 Microsoft.Office.Interop.Excel.Application 转换为 byte[]

转载 作者:行者123 更新时间:2023-11-30 14:56:34 28 4
gpt4 key购买 nike

我正在如下所示的代码中创建一个 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com