如何将使用封闭的xml创建的Excel文件转换为字节格式 [英] how to convert created excel file using closed xml into bytes format

查看:131
本文介绍了如何将使用封闭的xml创建的Excel文件转换为字节格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用closedxML DLL导出到excel我有静态方法如下所示

Hi I am using closedxML DLL for exporting to excel i have static method like this below

    public static void WriteToExcel(string fileName, List<CP> pages)
    {
        var wb = new XLWorkbook();
        byte[] file;


        var ws = wb.Worksheets.Add("CPs");
        WriteCostHeader(ws);

        ////write all the header columns
        //for (int i = 0; i < pages.Count; i++)
        int iRow = 2;
        foreach(var page in pages)
        {
            WriteCostPage(ws, page, iRow++);                

            WriteCostItemHead(ws, iRow++);

            foreach(var item in page.Items)
            {
                WriteCostItem(ws, item, iRow++);
            }
            iRow++;
        }

       wb.SaveAs(fileName);           
    }

我在下面的方法中调用上面的函数

I am calling above function in a method like this below

public  static List<CP> Init()
{

    //binding items to to list
}

static void Main(string[] args)
{
      byte[] file;
      file = ExcelProvider.WriteToExcel("D:\\Temp\\Test1.xls", Init());
      //Console.WriteLine("done");
      //Console.ReadLine();
 }

 public byte[] CreatePackage()
 {
     string fileName = string.Format("{0}.xlsx", "Generated");
     byte[] excelFile;         
     // getting error here cannot convert void to byte[]  
     excelFile =  ExcelProvider.WriteToExcel(fileName, Init());            
 }

但是我需要得到这个结果excel表单字节,我需要存储在内存流中,我可以用于进一步的目的..

but i need to get that result excel sheet in bytes, I need to store it in memory stream later i can use for further purpose ..

但我不知道如何可以以字节格式获取创建的excel文件...

But I am not sure how can i get the created excel file in bytes format...

任何人都可以提供任何想法或解决方案在这一个...
非常感谢提前...

Would any one pls give any idea or solutions on this one ... Many thanks In advance ...

推荐答案

请执行以下操作: -

Please do this:-

//Open the File into file stream
FileStream fileStream = new FileStream(Server.MapPath(fileName), FileMode.Open, FileAccess.Read, FileShare.Read);

//Create and populate a memorystream with the contents of the
MemoryStream mstream = StreamToMemory(fileStream);

// delete the file when it is been added to memory stream
File.Delete(Server.MapPath(fileName));

//Convert the memorystream to an array of bytes.
byte[] byteArray = mstream.ToArray();

//Clean up the memory stream
mstream.Flush();
mstream.Close();

// Clear all content output from the buffer stream
Response.Clear();

// Add a HTTP header to the output stream that specifies the default filename
// for the browser's download dialog
Response.AddHeader("Content-Disposition", "attachment; filename=QatargasTimesheet-" + ((DateTime)rdDate.SelectedDate).ToString("MMM yyyy") + ".xls");

// Add a HTTP header to the output stream that contains the 
// content length(File Size). This lets the browser know how much data is being transfered
Response.AddHeader("Content-Length", byteArray.Length.ToString());

// Set the HTTP MIME type of the output stream
Response.ContentType = "application/octet-stream";

// Write the data out to the client.
Response.BinaryWrite(byteArray);

这篇关于如何将使用封闭的xml创建的Excel文件转换为字节格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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