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

标签 c# interop

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

相关文章:

arrays - 将 C 字符数组转换为字符串

c# - 使用C#复制excel中的单元格

python - 如何从 C 代码调用 Python 代码?

c++ - 只能看到单个白点 CUDA/OpenGL interop

c# - 如何按工作日的顺序(如日历周)而不是字母顺序(在 C# 中)排序?

c# - 反序列化时 Newtonsoft Json.NET JsonConverter 属性保留引用问题

c# - 使用 Task.Factory.StartNew 启动存储过程

c# - string[] 计数被传递给 ComboBox,但 string[] 值没有

c# - 在 WinService 或 WinForms 应用程序中通过 URL 目录获取文件名

java - Scala - Java 互操作 : can Scala emit enums in bytecode for Java to consume?