c# - 包含来自 SSRS 报告的多个工作表的 Excel 文件

标签 c# .net excel reporting-services office-interop

假设我有 3 个字节数组,每个数组代表一个 .xls 文件。我如何将它们组合成一个包含 3 张纸的 xls 文件。 SSRS 报告非常丰富,包括图表 sp oledb 不是一个选项。

性能并不重要,因此如果需要,我可以将它们保存到磁盘,作为最后的手段,我什至可以使用 excel 宏(如果我知道该怎么做)。我尝试使用 microsodt.office.interop.excel,但我只能设法将新工作表添加到文件中,无法添加现有工作表。

如有任何帮助,我们将不胜感激。

最佳答案

在我看来,您只需要一种以编程方式将字节数组写入工作簿的方法。
这是一个将 byte[] 作为工作表写入特定 WorkBook 的方法:

public static void WriteToSheet(string targetBookPath, byte[] fileBytes)
{
    try {
        
        object x = Type.Missing;
        
        //Create a temp file to encapsulate Byte array
        string tmpPath = IO.Path.ChangeExtension(IO.Path.GetTempFileName(), ".xls");
        My.Computer.FileSystem.WriteAllBytes(tmpPath, fileBytes, false);
        
        //Start Excel Application (COM)
        Excel.Application xlApp = new Excel.Application();
        
        //Open target book
        Excel.Workbook targetBook = xlApp.Workbooks.Open(targetBookPath, x, x, x, x, x, x, x, x, x, 
        x, x, x, x, x);
        
        //Open temp file with Excel Interop
        Excel.Workbook sourceBook = xlApp.Workbooks.Open(tmpPath, x, x, x, x, x, x, x, x, x, 
        x, x, x, x, x);
        
        //Get a reference to the desired sheet 
        Excel.Worksheet sourceSheet = (Excel.Worksheet)sourceBook.Worksheets(1);
        
        //Copy the temp sheet into WorkBook specified as "Before" parameter
        Excel.Worksheet targetBefore = (Excel.Worksheet)targetBook.Worksheets(1);
        try {
            sourceSheet.Copy(targetBefore, x);
            
            //Save and Close
            sourceBook.Close(false, x, x);
            targetBook.Close(true, x, x);
            xlApp.Workbooks.Close();
                
            xlApp.Quit();
        }
        catch (Exception ex) {
            Debug.Fail(ex.ToString);
        }
        finally {
            
            //Release COM objects
            //   Source
            DESTROY(sourceSheet);
            DESTROY(sourceBook);
            
            //   Target
            DESTROY(targetBefore);
            DESTROY(targetBook);
            
            //   App
                
            DESTROY(xlApp);
        }
        
        //Kill the temp file
            
        My.Computer.FileSystem.DeleteFile(tmpPath);
    }
    catch (Exception ex) {
        Debug.Fail(ex.ToString);
    }
}

DESTROY 方法释放 COM 内容,这非常重要:

public static void DESTROY(object o)
{
    try {
        System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
    }
    catch (Exception ex) {
        Debug.Fail(ex.ToString);
        ErrorLog.Write(ex.ToString);
    }
    finally {
        o = null;
    }
}

如果我没理解错的话,你需要做的就是:

  1. 创建一个新的工作簿
  2. 遍历字节数组
  3. 为每个字节数组调用 WriteToSheet

关于c# - 包含来自 SSRS 报告的多个工作表的 Excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1863410/

相关文章:

c# - 对于 64 位架构上的算术,.NET 是否将 Int32 提升为 Int64?

vba - 如何在 Excel 中使用 VBA 获取 3 列的所有不同唯一组合?

vba - 将数据透视表添加到具有同一工作表上的数据的新工作表

c# 如何在不停止主线程的情况下在 2 个函数调用之间暂停

c# - Com 调用 64 位服务器上的 32 位应用程序很慢

c# - 以编程方式检测从 C# 代码 checkout 的当前 git 分支

.net - 什么是 System.Web.Mvc.MvcHandler.ProcessAsyncRequest()?

.net - 由于 CSPack 找不到文件而导致 TFS 构建失败

c# - 不考虑 InvokeRequired 调用 Invoke 有什么问题?

excel - 如何通过 VBA 代码填充下拉列表?