1

3 つのバイト配列があり、それぞれが .xls ファイルで表現されているとしましょう。それらを 3 つのシートを持つ 1 つの xls ファイルに結合する方法を教えてください。SSRS レポートは非​​常に豊富で、図表が含まれています。

パフォーマンスは重要ではないので、必要に応じてディスクに保存できます。最後の手段として、Excel マクロを使用することもできます (その方法を知っていれば)。microsodt.office.interop.excel を使用しようとしましたが、ファイルに新しいシートを追加することしかできず、既存のシートを追加できませんでした。

どんな助けでも大歓迎です。

4

3 に答える 3

5

プログラムでByte配列をWorkBookに書き込む方法が必要なようです。
以下は、byte[]をシートとして特定のワークブックに書き込むメソッドです。

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を呼び出します
于 2009-12-07T23:45:29.383 に答える
0

SpreadsheetGearfor.NETはそれを行うことができます。

  • SpreadsheetGear.Factory.GetWorkbookSet()。Workbooks.OpenFromMemory(byte [])を使用して、バイト配列からソースワークブックをロードします。バイト配列のソースによっては、GetWorkbookSet()。Workbooks.OpenFromStream(System.IO.Stream stream)を使用してストリームから直接ロードすることをお勧めします。
  • Factory.GetWorkbook()を使用して新しい宛先ワークブックを作成するか、Factory.GetWorkbook(string filename)を使用してテンプレート宛先ワークブックを開きます。
  • SpreadsheetGear.ISheet.CopyAfterまたはISheet.CopyBeforeを使用して、各ソースワークブックからワークシートをコピーします。
  • 宛先のワークブックをIWorkbook.SaveAs(string filename、FileFormat fileFormat)、IWorkbook.SaveToMemory(FileFormat fileFormat)、またはIWorkbook.SaveToStream(System.IO.Stream stream、FileFormat fileFormat)で保存します。

多数のライブASP.NETサンプルをここで確認し、無料トライアルをここからダウンロードできます。

「グラフを含むワークシートからグラフを含む複数のワークシート」のサンプルがあります。これは、Excelレポートのサンプルページにいくらか役立つ場合があります

免責事項:私はSpreadsheetGearLLCを所有しています

于 2009-12-08T16:14:20.057 に答える