sysperfana/heapanalyser/Libraries/Engine/HeapComparisonLib/CSV/Excel/CSVExcelExporterTwoDataSets.cs
changeset 8 15296fd0af4a
equal deleted inserted replaced
7:8e12a575a9b5 8:15296fd0af4a
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 *
       
     5 * Redistribution and use in source and binary forms, with or without
       
     6 * modification, are permitted provided that the following conditions are met:
       
     7 *
       
     8 * - Redistributions of source code must retain the above copyright notice,
       
     9 *   this list of conditions and the following disclaimer.
       
    10 * - Redistributions in binary form must reproduce the above copyright notice,
       
    11 *   this list of conditions and the following disclaimer in the documentation
       
    12 *   and/or other materials provided with the distribution.
       
    13 * - Neither the name of Nokia Corporation nor the names of its contributors
       
    14 *   may be used to endorse or promote products derived from this software
       
    15 *   without specific prior written permission.
       
    16 *
       
    17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
       
    18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
       
    20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
       
    21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
       
    22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
       
    23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
       
    24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
       
    25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
       
    26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
       
    27 * POSSIBILITY OF SUCH DAMAGE.
       
    28 * 
       
    29 * Initial Contributors:
       
    30 * Nokia Corporation - initial contribution.
       
    31 *
       
    32 * Contributors:
       
    33 *
       
    34 * Description: 
       
    35 *
       
    36 */
       
    37 
       
    38 using System;
       
    39 using System.IO;
       
    40 using System.Threading;
       
    41 using System.Runtime.InteropServices;
       
    42 using Excel = Microsoft.Office.Interop.Excel;
       
    43 using System.Reflection; 
       
    44 using SymbianExcelUtils;
       
    45 using SymbianUtils;
       
    46 
       
    47 namespace HeapComparisonLib.CSV
       
    48 {
       
    49     internal class CSVExcelExporterTwoDataSets : DisposableObject
       
    50     {
       
    51         #region Constructors & destructor
       
    52         public CSVExcelExporterTwoDataSets( string aOutputFileName, string aCol1FileName, string aCol2FileName )
       
    53         {
       
    54             iFileName = aOutputFileName;
       
    55             iCol1FileName = aCol1FileName;
       
    56             iCol2FileName = aCol2FileName;
       
    57             //
       
    58             iExcelApp = new Excel.Application();
       
    59             if ( iExcelApp != null )
       
    60             {
       
    61                 iExcelApp.Visible = false;
       
    62                 iExcelApp.DisplayAlerts = false;
       
    63 
       
    64                 // Prepare sheets
       
    65                 PrepareWorksheetReadyForData();
       
    66             }
       
    67             else
       
    68             {
       
    69                 throw new Exception( "Microsoft Excel not available" );
       
    70             }
       
    71         }
       
    72         #endregion
       
    73 
       
    74         #region API
       
    75         public void CompareThread( CSVThread aThread1, CSVThread aThread2 )
       
    76         {
       
    77             // Do comparison
       
    78             iComparerHeapSize.Compare( aThread1, aThread2 );
       
    79             iComparerCellCounts.Compare( aThread1, aThread2 );
       
    80             iComparerLargestCells.Compare( aThread1, aThread2 );
       
    81             iComparerFragmentation.Compare( aThread1, aThread2 );
       
    82             iComparerSlackSpace.Compare( aThread1, aThread2 );
       
    83         }
       
    84         #endregion
       
    85 
       
    86         #region From DisposableObject
       
    87         protected override void CleanupManagedResources()
       
    88         {
       
    89             try
       
    90             {
       
    91                 if ( iExcelApp != null )
       
    92                 {
       
    93                     // We're finished
       
    94                     PrepareWorsheetForSaving();
       
    95 
       
    96                     // Save excel workbook
       
    97                     SaveWorkbook();
       
    98 
       
    99                     // Close app
       
   100                     Utils.CloseExcel( iExcelApp );
       
   101                 }
       
   102 
       
   103                 iExcelApp = null;
       
   104             }
       
   105             finally
       
   106             {
       
   107                 base.CleanupManagedResources();
       
   108             }
       
   109         }
       
   110         #endregion
       
   111 
       
   112         #region Internal methods
       
   113         private void PrepareWorksheetReadyForData()
       
   114         {
       
   115             Excel.Workbooks workbooks = iExcelApp.Workbooks;
       
   116             workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
       
   117             Excel.Workbook workbook = workbooks.get_Item(workbooks.Count);
       
   118             Excel.Sheets sheets = workbook.Worksheets;
       
   119 
       
   120             Excel.Worksheet sheet = null;
       
   121             //
       
   122             sheet = (Excel.Worksheet)sheets.Add(Type.Missing, sheets.get_Item(sheets.Count), 1, Excel.XlSheetType.xlWorksheet);
       
   123             iComparerHeapSize = new Comparers.HeapSize( sheet, iCol1FileName, iCol2FileName );
       
   124             iComparerHeapSize.PrepareWorksheetReadyForData();
       
   125             //
       
   126             sheet = (Excel.Worksheet)sheets.Add(Type.Missing, sheets.get_Item(sheets.Count), 1, Excel.XlSheetType.xlWorksheet);
       
   127             iComparerCellCounts = new Comparers.CellCounts( sheet, iCol1FileName, iCol2FileName );
       
   128             iComparerCellCounts.PrepareWorksheetReadyForData();
       
   129             //
       
   130             sheet = (Excel.Worksheet)sheets.Add(Type.Missing, sheets.get_Item(sheets.Count), 1, Excel.XlSheetType.xlWorksheet);
       
   131             iComparerLargestCells = new Comparers.LargestCells( sheet, iCol1FileName, iCol2FileName );
       
   132             iComparerLargestCells.PrepareWorksheetReadyForData();
       
   133             //
       
   134             sheet = (Excel.Worksheet)sheets.Add(Type.Missing, sheets.get_Item(sheets.Count), 1, Excel.XlSheetType.xlWorksheet);
       
   135             iComparerFragmentation = new Comparers.Fragmentation( sheet, iCol1FileName, iCol2FileName );
       
   136             iComparerFragmentation.PrepareWorksheetReadyForData();
       
   137             //
       
   138             sheet = (Excel.Worksheet)sheets.Add(Type.Missing, sheets.get_Item(sheets.Count), 1, Excel.XlSheetType.xlWorksheet);
       
   139             iComparerSlackSpace = new Comparers.SlackSpace( sheet, iCol1FileName, iCol2FileName );
       
   140             iComparerSlackSpace.PrepareWorksheetReadyForData();
       
   141         }
       
   142 
       
   143         private void PrepareWorsheetForSaving()
       
   144         {
       
   145             // Finalise comparison sheets
       
   146             iComparerHeapSize.FinaliseWorksheet();
       
   147             iComparerCellCounts.FinaliseWorksheet();
       
   148             iComparerLargestCells.FinaliseWorksheet();
       
   149             iComparerFragmentation.FinaliseWorksheet();
       
   150             iComparerSlackSpace.FinaliseWorksheet();
       
   151 
       
   152             // Create summary sheet
       
   153             Excel.Workbooks workbooks = iExcelApp.Workbooks;
       
   154             Excel.Workbook workbook = workbooks.get_Item(workbooks.Count);
       
   155             Excel.Sheets sheets = workbook.Worksheets;
       
   156             Excel.Worksheet sheet = (Excel.Worksheet)sheets.get_Item(1);
       
   157             CreateSummary( sheet );
       
   158             sheet.Activate();
       
   159         }
       
   160 
       
   161         private void CreateSummary( Microsoft.Office.Interop.Excel.Worksheet aSheet )
       
   162         {
       
   163             aSheet.Name = "Summary";
       
   164             //
       
   165             int row = 1;
       
   166             Utils.SetValue( row, 2, aSheet, "Delta" );
       
   167             Utils.MakeBoxedTitleRow( row, 2, aSheet, 0xFF0000 );
       
   168             row++;
       
   169             //
       
   170             row++;
       
   171             Utils.SetValue( row, 1, aSheet, "File 1:" );
       
   172             Utils.SetValue( row, 2, aSheet, iCol1FileName );
       
   173             //
       
   174             row++;
       
   175             Utils.SetValue( row, 1, aSheet, "File 2:" );
       
   176             Utils.SetValue( row, 2, aSheet, iCol2FileName );
       
   177             //
       
   178             row++;
       
   179             Utils.SetValue( row, 1, aSheet, "Heap size:" );
       
   180             Utils.SetValue( row, 2, aSheet, iComparerHeapSize.DeltaFormula( 0 ) );
       
   181             //
       
   182             row++;
       
   183             Utils.SetValue( row, 1, aSheet, "Free space:" );
       
   184             Utils.SetValue( row, 2, aSheet, iComparerHeapSize.DeltaFormula( 1 ) );
       
   185             //
       
   186             row++;
       
   187             Utils.SetValue( row, 1, aSheet, "Alloc space:" );
       
   188             Utils.SetValue( row, 2, aSheet, iComparerHeapSize.DeltaFormula( 2 ) );
       
   189             //
       
   190             row++;
       
   191             Utils.SetValue( row, 1, aSheet, "Heap size:" );
       
   192             Utils.SetValue( row, 2, aSheet, iComparerHeapSize.DeltaFormula( 0 ) );
       
   193             //
       
   194             row++;
       
   195             Utils.SetValue( row, 1, aSheet, "Free cell count:" );
       
   196             Utils.SetValue( row, 2, aSheet, iComparerCellCounts.DeltaFormula( 0 ) );
       
   197             //
       
   198             row++;
       
   199             Utils.SetValue( row, 1, aSheet, "Allocated cell count:" );
       
   200             Utils.SetValue( row, 2, aSheet, iComparerCellCounts.DeltaFormula( 1 ) );
       
   201             //
       
   202             row++;
       
   203             Utils.SetValue( row, 1, aSheet, "Fragmentation:" );
       
   204             Utils.SetValue( row, 2, aSheet, iComparerFragmentation.DeltaFormula( 0 ) );
       
   205             //
       
   206             row++;
       
   207             Utils.SetValue( row, 1, aSheet, "Slack space:" );
       
   208             Utils.SetValue( row, 2, aSheet, iComparerSlackSpace.DeltaFormula( 0 ) );
       
   209             Utils.BoldColumn( 1, aSheet );
       
   210 
       
   211             // Calculate column sizes
       
   212             Utils.AutoFitColumn( 1, aSheet );
       
   213             Utils.AutoFitColumn( 2, aSheet );
       
   214         }
       
   215 
       
   216         private void SaveWorkbook()
       
   217         {
       
   218             string path = Path.GetDirectoryName( iFileName );
       
   219             System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo( path );
       
   220             if ( !dirInfo.Exists )
       
   221             {
       
   222                 dirInfo.Create();
       
   223             }
       
   224 
       
   225             System.IO.FileInfo fileInfo = new System.IO.FileInfo( iFileName );
       
   226             if ( fileInfo.Exists )
       
   227             {
       
   228                 try
       
   229                 {
       
   230                     fileInfo.Delete();
       
   231                 }
       
   232                 catch ( Exception )
       
   233                 {
       
   234                 }
       
   235             }
       
   236 
       
   237             Microsoft.Office.Interop.Excel.Workbooks workbooks = iExcelApp.Workbooks;
       
   238             Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.get_Item( workbooks.Count );
       
   239 
       
   240             try
       
   241             {
       
   242                 workbook.SaveAs( iFileName,
       
   243                                     Excel.XlFileFormat.xlExcel9795,
       
   244                                     Type.Missing,
       
   245                                     Type.Missing,
       
   246                                     Type.Missing,
       
   247                                     Type.Missing,
       
   248                                     Excel.XlSaveAsAccessMode.xlNoChange,
       
   249                                     Type.Missing,
       
   250                                     Type.Missing,
       
   251                                     Type.Missing,
       
   252                                     Type.Missing,
       
   253                                     Type.Missing );
       
   254             }
       
   255             catch ( System.IO.IOException )
       
   256             {
       
   257             }
       
   258 
       
   259             workbook.Close( false, Type.Missing, Type.Missing );
       
   260         }
       
   261         #endregion
       
   262 
       
   263         #region Data members
       
   264         private readonly string iFileName;
       
   265         private readonly string iCol1FileName;
       
   266         private readonly string iCol2FileName;
       
   267         private Excel.Application iExcelApp;
       
   268         private Comparers.HeapSize iComparerHeapSize;
       
   269         private Comparers.CellCounts iComparerCellCounts;
       
   270         private Comparers.LargestCells iComparerLargestCells;
       
   271         private Comparers.Fragmentation iComparerFragmentation;
       
   272         private Comparers.SlackSpace iComparerSlackSpace;
       
   273         #endregion
       
   274     }
       
   275 }