1
Answer

Excel interop formatting problem

Tony

Tony

17y
3k
1

Hello all,

I have a problem with formatting an Excel Range object in C# using Interop.Excel. Here is a snippet of the procedure:

private static bool FormatExcel(string filename, int font)
{

Excel.Application excelApp = new Excel.ApplicationClass();
excelApp.DisplayAlerts = false;

try
{

Excel.Workbook excelWkBook = excelApp.Workbooks._Open(filename, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows,"", true, false, 0, true);

Excel.Worksheet excelWkSheet = (Excel.Worksheet)excelWkBook.ActiveSheet;
Excel.Range excelRange = excelWkSheet.UsedRange;

//Problems is here:
excelRange.Cells.Font.Size = 8;
excelRange.Columns.AutoFit();
//No more problems...

excelWkBook.Close(true, filename, null);
excelApp.DisplayAlerts = true;

excelWkBook = null;
return true;
}
finally
{
excelApp = null;
}
}

I have come from VB6 environment and simmilar code worked great in VB6. In C# for some reason <excelRange.Cells.Font.Size = 8; > - puts the whole row into the first cell instead of just changing the font and <excelRange.Columns.AutoFit();> - simply doesn't do anything.

Other than that the code works fine. The file name passed to the procedure is a simple tab-dilimeted .txt file, that I want user to be able to reformat as excel if appropriate flag is specified.

The stack trace shows the following:

StackTrace = " at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)\r\n at Microsoft.Office.Interop.Excel.Range.get_CurrentArray()"

What the .get_currentArray() has to do with anything?

This problem drives me nutts! Any help is greatly apprecited!

Lentyai.

Answers (1)