3

I have a c# script which successfully opens a specified Excel workbook, delete the first row and save it down as a csv format.

I have an issue that the CSV saves down as a standard col width of 8.43 I would like to change the col width of col J as 17.00 before saving down the csv.

Can someone please advise how I set the Col width of col J to 17.

I am unable to use VBA / Macros due to the process.

I am using;

myWorkSheet.Columns["J:J"].ColumnWidth = 17.57;

This however leads to an object does not contain definition for 'ColumnWidth' screenshot attached. enter image description here

Full code below;

        public void DeleteRows(string OriginalFileName,String NewFileName)
    {
        Microsoft.Office.Interop.Excel.Application myApp;
        Microsoft.Office.Interop.Excel.Workbook myWorkBook;
        Microsoft.Office.Interop.Excel.Worksheet myWorkSheet;
        Microsoft.Office.Interop.Excel.Range range;
        myApp = new Microsoft.Office.Interop.Excel.Application();
        myWorkBook = myApp.Workbooks.Open(OriginalFileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
        myWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)myWorkBook.Worksheets.get_Item(1);
        range = (Microsoft.Office.Interop.Excel.Range)myWorkSheet.Application.Rows[1, Type.Missing];
        range.Select();
        range.Delete(Microsoft.Office.Interop.Excel.XlDirection.xlUp);
        //Microsoft.Office.Interop.Excel.Range ThisRange1 = myWorkSheet.get_Range("J:J",System.Type.Missing);
        myWorkSheet.Columns["J:J"].ColumnWidth = 17.57; //ERRORLINE
        myApp.DisplayAlerts = false;
        myWorkSheet.SaveAs(NewFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlCSVWindows, Type.Missing, Type.Missing, false, false, false, XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing);
        myWorkBook.Close(false);
        myApp.Quit();
    }
4
  • why are you using a range if you want to set the column width ? Apr 10, 2018 at 14:52
  • just add reference to excel interop , that's all Apr 10, 2018 at 15:26
  • Be sure to update your VS version. Office programming in C# is pretty brutal in any version less than VS2010. And hard to get help with it, SO users just don't understand why you have such a problem. Apr 10, 2018 at 15:37
  • I am using VS2015 so hoping this shouldn't be the issue Apr 10, 2018 at 15:54

1 Answer 1

6

I don't think you need to set a range for this :

 Using Excel=Microsoft.Office.Interop.Excel;
 Excel.Worksheet myWorkSheet = new Excel.Worksheet;

 myWorkSheet.Columns["J:J"].ColumnWidth = 17.57;

///Or set width to AutoFit

 myWorkSheet.Columns.AutoFit();
10
  • Thanks! I have changed my code to 'myWorkSheet.Columns["J:J"].ColumnWidth = 17.57;' however ColumnWidth has come up with an error. "Object" does not contain definition for ColumnWidth. Apr 10, 2018 at 15:12
  • I have inserted 'sheet.Columns["J:J"].ColumnWidth = 17.57;' however sheet does not exist in the current context Apr 10, 2018 at 15:20
  • and please mark the answer as answer if it solved your issue :) Apr 10, 2018 at 15:21
  • just add reference to excel interop , that's all Apr 10, 2018 at 15:26
  • Thanks - I've edited the orginal post. I tried "myWorkSheet.Columns["J:J"].ColumnWidth = 17.57;" However it came up with the ColumnWidth object error which is now seen in the orginal post. Apr 10, 2018 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.