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
725
Copy worksheet
posted

I'm working with Infragistics3.Documents.Excel.v11.1 (v11.1.20111.2050)


1.- How can I copy a worksheet from one workbook_orig to another workbook_dest??

2.- And if the worksheet_orig have formulas in some cells, I want copy only the value.
How can I do that?

Thank you in advance

Parents
No Data
Reply
  • 44743
    posted

    Copying a worksheet is currently not supported with the Excel library and I suspect if it was, it probably would copy the formulas as well, so even if it is was implemented, you would need to do this manually. Here is how you might do this:

    Worksheet sourceWorksheet = worksheet_orig.Worksheets["Sheet1"];
    Workbook workbook_dest = new Workbook(WorkbookFormat.Excel2007);
    Worksheet destination = workbook_dest.Worksheets.Add(sourceWorksheet.Name);

    foreach (WorksheetColumn sourceColumn in sourceWorksheet.Columns)
    {
     WorksheetColumn destinationColumn = destination.Columns[sourceColumn.Index];
     destinationColumn.CellFormat.SetFormatting(CreateFormatCopy(workbook_dest, sourceColumn.CellFormat));
     destinationColumn.Width = sourceColumn.Width;
     destinationColumn.Hidden = sourceColumn.Hidden;
    }

    foreach (WorksheetRow sourceRow in sourceWorksheet.Rows)
    {
     WorksheetRow destinationRow = destination.Rows[sourceRow.Index];
     destinationRow.CellFormat.SetFormatting(CreateFormatCopy(workbook_dest, sourceRow.CellFormat));
     destinationRow.Height = sourceRow.Height;
     destinationRow.Hidden = sourceRow.Hidden;

     foreach (WorksheetCell sourceCell in sourceRow.Cells)
     {
      WorksheetCell destinationCell = destinationRow.Cells[sourceCell.ColumnIndex];
      destinationCell.CellFormat.SetFormatting(CreateFormatCopy(workbook_dest, sourceCell.CellFormat));
      destinationCell.Value = sourceCell.Value;
     }
    }


    static IWorksheetCellFormat CreateFormatCopy(Workbook workbook, IWorksheetCellFormat sourceCellFormat)
    {
     IWorksheetCellFormat copy = workbook.CreateNewWorksheetCellFormat();
     copy.SetFormatting(sourceCellFormat);
     return copy;
    }

Children