来自SSRS报告的多个工作表的Excel文件

假设我有3个字节的数组,每个数组代表一个.xls文件。 我怎样才能把他们合并成一张单一的xls文件与3张。 SSRS报告非常丰富,包括图表oledb不是一个选项。

性能并不重要,所以我可以将它们保存到磁盘,如果需要的话,作为最后的手段,我甚至可以使用Excelmacros(如果我知道如何做到这一点)。 我试图使用microsodt.office.interop.excel但我只能设法添加一个新的工作表,我不能添加一个现有的工作表。

任何帮助,将不胜感激。

在我看来,你只是需要一种方法编程写入字节数组到工作簿。
这是一个方法,将一个字节[]作为一个工作簿的工作簿:

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. 创build一个新的工作簿
  2. 通过字节数组循环
  3. 为每个Byte数组调用WriteToSheet

是否必须生成单独的报告,然后结合? SoftArtisans OfficeWriter可以以编程方式组合字节数组中的多个电子表格,但是我们也有SSRS集成 ,可以让您直接创build多页报表。 使用OfficeWriter Designer Excel加载项,您可以在Excel中直接devise包含3个工作表的报表,并将其发布到SSRS。

免责声明:我为SoftArtisans工作

SpreadsheetGear for .NET可以做到这一点:

  • 使用SpreadsheetGear.Factory.GetWorkbookSet(),从字节数组加载源工作簿。Workbooks.OpenFromMemory(byte [])。 根据字节数组的来源,您可能更喜欢直接从GetWorkbookSet()。WorkBooks.OpenFromStream(System.IO.Streamstream)的stream中加载。
  • 使用Factory.GetWorkbook()创build新的目标工作簿,或使用Factory.GetWorkbook(string文件名)打开模板目标工作簿。
  • 使用SpreadsheetGear.ISheet.CopyAfter或ISheet.CopyBefore将每个源工作簿中的工作表复制。
  • 使用IWorkbook.SaveAs(string文件名,FileFormat fileFormat),IWorkbook.SaveToMemory(FileFormat fileFormat)或IWorkbook.SaveToStream(System.IO.Streamstream,FileFormat fileFormat)保存目标工作簿。

你可以在这里看到大量的ASP.NET实例,并在这里下载免费试用版。

有一个“带图表的多个工作表的工作表”示例,这可能对我们的Excel报告示例页面有所帮助。

免责声明:我自己的SpreadsheetGear LLC