If there's no sheet.CanAutoFit()
then you can always do this:
try {
curSheet.UsedRange.EntireColumn.AutoFit();
} catch (TheSpecificExcelException exc)
{ ... }
Question
While working with Excel Interop
, i come up with an issue. Before Saving I Autofit columns of worksheet with the following code.
Excel.Worksheet curSheet = (Excel.Worksheet)wsEnumerator.Current;
curSheet.UsedRange.EntireColumn.AutoFit();
And it works fine for me. Then i encountered an issue with some specific Excel sheet. If i autofit it says AutoFit method of Range class failed
.
The excel sheet which cause the issue is available here
Now what i want to know is, whether there is some way to check if an excel sheet support Autofit or not. Something like
if(sheet.CanAutoFit())
curSheet.UsedRange.EntireColumn.AutoFit();
Any help would be appreciated.
Solution
If there's no sheet.CanAutoFit()
then you can always do this:
try {
curSheet.UsedRange.EntireColumn.AutoFit();
} catch (TheSpecificExcelException exc)
{ ... }
OTHER TIPS
The issue was sheet was protected. And to handle that i have used try
catch
exception handling. So it has been resolved.