Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
85
'Convert to Range' after 'Format as Table'
posted

Dear reader,

In Excel there is an option to format some data as a table using 'Format as Table'. This can be more or less reversed by using 'Convert to Range' which will convert the table back to regular data. However, the table styling is kept in place. I'd like to achieve exactly this effect using the infragistics Excel engine. I'd like to apply table formatting, but only to trigger the related style changes. I wish to avoid triggering any Excel functionality related to 'table formatting', such as sorting, filtering, table cell references, unique column names, etc. 

Is it possible to achieve this using the excel engine? Either by triggering something similar to 'Convert to Range' or by other means?

Many thanks,
Remy.

Parents
No Data
Reply
  • 44743
    Suggested Answer
    posted

    Hi Remy, I think I can help here. With our table support, you can add a table, specifying the style you'd like to use, and then you can remove the table and opt to leave the styling in place, just as "Convert to Range" works. The code for this might look something like this:

    var workbook = new Workbook(WorkbookFormat.Excel2007);

    var worksheet = workbook.Worksheets.Add("Sheet1");

    var table = worksheet.Tables.Add("A1:B20",

           tableHasHeaders: true,

           tableStyle: workbook.StandardTableStyles["TableStyleMedium6"]);

    worksheet.Tables.Remove(table, convertToRange: true);

    This will not perform any sorting, filtering, or formula manipulation using table cell references, but it will enforce unique column names, which you have stated you do you want. So, we can modify this slightly and instead say that the table does not have headers when we add it. This will cause header cells to get inserted on our behalf with the unique names, and then we can simply hide those header cells, which will delete them and shift the table back up:

    var workbook = new Workbook(WorkbookFormat.Excel2007);

    var worksheet = workbook.Worksheets.Add("Sheet1");

    var table = worksheet.Tables.Add("A1:B20",

           tableHasHeaders: false,

           tableStyle: workbook.StandardTableStyles["TableStyleMedium6"]);

    table.IsHeaderRowVisible = false;

    worksheet.Tables.Remove(table, convertToRange: true);

    But this presents two additional problems:

    1. The table insert will fail if there are other entities below the table which cannot be shifted down to allow the header cells to be inserted. For example, if the table we are adding spans columns A and B, but there is another table below it that spans columns A, B, and C, the table add will fail because the existing table cannot be split to only shift down cells in columns A and B. 
    2. The range at the end of all of this will not have header row formatting, since the header row was not visible when the table was converted to a range.

    The first problem is unavoidable. If there are other entities below the table that intersect with, but are not completed contained in the new table's columns, an exception will be thrown. In this case, we will have to add the table with headers and work around the fact that the column names will be made unique. The second problem can be fixed by copying the header format to the cells at the top of the range after the table has been removed:

    var workbook = new Workbook(WorkbookFormat.Excel2007);

    var worksheet = workbook.Worksheets.Add("Sheet1");

    var style = workbook.StandardTableStyles["TableStyleMedium6"];

    var table = worksheet.Tables.Add("A1:B20",

           tableHasHeaders: false,

           tableStyle: style);

    table.IsHeaderRowVisible = false;

    worksheet.Tables.Remove(table, convertToRange: true);

     

    var temp = workbook.CreateNewWorksheetCellFormat();

    temp.SetFormatting(style.AreaFormats[WorksheetTableStyleArea.HeaderRow]);

    foreach (var cell in worksheet.GetRegion("A1:B1"))

    {

           cell.CellFormat.SetFormatting(temp);

    }

    I hope this helps. Please let me know if you have any other questions.

Children