代码之家  ›  专栏  ›  技术社区  ›  Paul Creasey

包含来自SSRS报告的多张工作表的Excel文件

  •  1
  • Paul Creasey  · 技术社区  · 15 年前

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

    任何帮助都将不胜感激。

    3 回复  |  直到 15 年前
        1
  •  5
  •   Joey    15 年前

    在我看来,您只需要一种通过编程将字节数组写入工作簿的方法。

    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
  •  1
  •   Aviva M.    15 年前

    表格是否必须作为单独的报告生成,然后合并?SoftArtisans OfficeWriter可以通过编程方式组合来自字节数组的多个电子表格,但我们也有 SSRS integration 这将允许您直接创建多页报告。使用OfficeWriter Designer Excel加载项,您可以在Excel中设计包含3个工作表的报告,并将其发布到SSRS。

    免责声明:我为软件工匠工作

        3
  •  0
  •   Joe Erickson    15 年前

    SpreadsheetGear for .NET

    • 使用SpreadsheetGear.Factory.GetWorkbookSet().workbooks.OpenFromMemory(字节[])从字节数组加载源工作簿。根据字节数组的来源,您可能更喜欢使用GetWorkbookSet().Workbooks.OpenFromStream(System.IO.stream stream)直接从流中加载。
    • 使用SpreadsheetGear.ISheet.CopyAfter或ISheet.CopyBefore从每个源工作簿复制工作表。
    • 使用IWorkbook.SaveAs(字符串文件名、文件格式、文件格式)、IWorkbook.SaveToMemory(文件格式、文件格式)或IWorkbook.SaveToStream(System.IO.Stream、文件格式)保存目标工作簿。

    here here .

    here .