質問

3 バイトの配列があり、それぞれが .xls ファイルで表されているとします。3 つのシートを含む 1 つの xls ファイルに結合するにはどうすればよいですか。SSRS レポートは非​​常に豊富で、グラフも含まれています。 sp oledb はオプションではありません。

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

助けていただければ幸いです。

役に立ちましたか?

解決

プログラムでバイト配列を WorkBook に書き込む方法が必要なだけのようです。
以下は、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. Byte 配列ごとに WriteToSheet を呼び出します

他のヒント

シートが別々のレポートとして生成し、その後組み合わせることする必要がありますか? SSRSの統合それはあなたが直接マルチシートレポートを作成できるようになります。 Excelアドインでは、あなたが右のExcelのワークシート3とのレポートを設計し、SSRSにそれを公開することができOfficeWriter Designerを使用。

免責事項:私はSoftArtisansのために働く。

.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 レポートのサンプル ページには、多少役立つ可能性がある「グラフのあるワークシートからグラフのある複数のワークシートへ」というサンプルがあります。 ここ.

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top