我有一个c#脚本成功打开指定的Excel工作簿,删除第一行并将其保存为csv格式。
我有一个问题,CSV保存为8.43的标准col宽度我想在保存csv之前将col J的col宽度更改为17.00。
有人可以告诉我如何将col J的Col宽度设置为17。
由于此过程,我无法使用VBA /宏。
我正在使用;
myWorkSheet.Columns["J:J"].ColumnWidth = 17.57;
然而,这导致对象不包含' ColumnWidth'的定义。附上截图。
以下完整代码;
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();
}
答案 0 :(得分:1)
我认为您不需要为此设置范围:
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();