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
35
How to read one Excel and output to another Excel keeping partial formatting?
posted

Hi,

Is there a way to read from one Excel with Infragistics and output the same data to another Excel, while keeping the exact same formatting as the original? For example, if part of the text has strikethrough (see above, circle added by me) or some of the text is black and some is red, how do I end up with the same visual effect in the new spreadsheet I'm writing to? Thanks!

Alex

Parents Reply
  • 22852
    Offline posted in reply to Alex Bratu

    Alex,

    The Value property of a cell can return various types depending on what is in the cell and in the case of Book2.xlsx, when the value isn't a formatted string it is expected that you would get a NullReferenceException in the code provided.  The following screen shot shows that the cell in the second row returns a string while the cell in the fifth row returns a FormattedString:

    Note that in the code example provided the values were being read from and written to the same WorkBook.  You will need to check if the value in the source WorkBook is a FormattedString and if so Clone it otherwise just set the value to the same Value.  For example:

    static void Main(string[] args)
    {
        Workbook wb = Workbook.Load("Book2.xlsx");
        Workbook wb2 = new Workbook(WorkbookFormat.Excel2007);
        wb2.Worksheets.Add("sheet");
        for (int rowIndex = 1; rowIndex <= wb.Worksheets[0].Rows.Count() - 1; rowIndex++)
        {
            object value = wb.Worksheets[0].Rows[rowIndex].Cells[2].Value;
            if (value is FormattedString)
            {
                value = ((FormattedString) value).Clone();
            }
    
            wb2.Worksheets[0].Rows[rowIndex + 10].Cells[2].Value = value;
        }
        wb2.Save(@"Book3.xlsx");
    }
    

    Let me know if you have any questions.

Children