Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将工作表转换为内存流?_C#_.net_Excel - Fatal编程技术网

C# 将工作表转换为内存流?

C# 将工作表转换为内存流?,c#,.net,excel,C#,.net,Excel,我想把工作表转换成内存流,这样我就可以在浏览器上下载我的excel了 Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); // Create empty workbook excel.Workbooks.Add(); // Create Worksheet from act

我想把工作表转换成内存流,这样我就可以在浏览器上下载我的excel了

Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();

            // Create empty workbook
            excel.Workbooks.Add();

            // Create Worksheet from active sheet
            Microsoft.Office.Interop.Excel._Worksheet workSheet = excel.ActiveSheet;

            try
            {
                // ------------------------------------------------
                // Creation of header cells
                // ------------------------------------------------
                workSheet.Cells[1, "A"] = "GroupId";
                workSheet.Cells[1, "B"] = "Time";



                // ------------------------------------------------
                // Populate sheet with some real data from "cars" list
                // ------------------------------------------------
                int row = 2; // start row (in row 1 are header cells)
                foreach (var item in Result)
                {
                    workSheet.Cells[row, "A"] = item.MasterID;
                    workSheet.Cells[row, "B"] = item.LocalTime;  
                    row++;
                }
我知道转换成内存流,但在这里

 using (MemoryStream exportData = new MemoryStream())
                {

                    //workSheet.Write(exportData);  //Worksheet doesn't have this Write method as compared to workbook.
                    Response.ContentEncoding = Encoding.GetEncoding("ISO-8859-1");
                    Response.Charset = Encoding.GetEncoding("ISO-8859-1").EncodingName;
                    Response.ContentType = "application/vnd.ms-excel"; //xls
                                                                       // For xlsx, use: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
                    Response.AddHeader("content-disposition", String.Format("attachment; filename={0}.xls", "yourFilename"));
                    Response.Clear();
                    Response.BinaryWrite(exportData.GetBuffer());
                    Response.End();
                }

如何将我的工作表转换为内存流

未找到Microsoft.Office.Interop.Excel的解决方案。除此之外,该文件应首先保存在计算机上,而不是以流形式保存

Spire.xsl有一个方法

工作簿.SaveToStream(memoryStream,FileFormat.Version2016)


注意:spire.xsl免费版本有限制。

简而言之,将其写入文件系统,然后使用类似于@mjwills的东西使用mvc,这都在我的action方法中,@mjwills的controllerPossible duplicate中,可能是@mjwills的副本,但我想转换工作表。如果不先将xlsx文件写入文件系统,就无法从流提供xlsx文件。因此,副本是您的最佳选择(与来自的内容类型相结合)。