79190865

Date: 2024-11-15 01:37:01
Score: 0.5
Natty:
Report link

I finally used @rotabor's answer to build mine without having to copy the whole worksheet.

Code is as follows :

Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
                app.Visible = true;

                Workbook FichierCE = app.Workbooks.Open(currentFileTravail,UpdateLinks:true);
                Worksheet fichierTab = FichierCE.Worksheets[tabCEEntiteName];
                Workbook wBFichierTab = app.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
                
                Worksheet fichierTabTab = wBFichierTab.Worksheets[1];
                                    
                fichierTabTab.Name = tabCEEntiteName; 

                Range data = fichierTab.UsedRange;
                int rows = data.Rows.Count;
                //int cols = data.Columns.Count;

                for (int r = 4; r <= rows; r++)
                {
                    for (int c = 1; c <= 2; c++)
                    {
                        if (data.Cells[r, c].Value2 == null)
                        {
                            fichierTabTab.Cells[r - 3, c] = "";
                        }
                        else
                        {
                            fichierTabTab.Cells[r - 3, c] = data.Cells[r, c].Value2.ToString();
                        }
                    }
                }

I just needed the first 2 columns and only the rows starting from row 4

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @rotabor's
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: d_anass