将Microsoft.Office.Interop.Excel.Application转换为byte [] [英] Convert Microsoft.Office.Interop.Excel.Application to byte[]

查看:110
本文介绍了将Microsoft.Office.Interop.Excel.Application转换为byte []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在如下所示的代码中创建一个excel文件

I am creating a excel file in the code as Shown Below

 Microsoft.Office.Interop.Excel.Application excelFile = CreateExcelFile();

现在,我想将此excelFile转换为byte []而不保存到硬盘驱动器.怎么可能?

now I want to convert this excelFile to byte[] without saving to hard drive. How is it possible?

推荐答案

前段时间有相同的问题.您必须创建一个临时文件,然后将其读取到字节数组.

had the same problem some time ago. You have to create a temporary file, and then read it to a byte array.

示例代码:

            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

这篇关于将Microsoft.Office.Interop.Excel.Application转换为byte []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆