Click here to Skip to main content
16,013,082 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to select all the data rows in Col F - Col I
and change the number format to have 2 decimal places.
The following code does not work. Can someone help me out?
The code compiles, but blows up with a COM exception(which I can't figure out)


C#
Excel.Range workSheet_range = sheet.get_Range("F", "I");
workSheet_range.NumberFormat = "0.00";
Posted
Updated 17-Jan-12 11:54am
v2
Comments
Orcun Iyigun 17-Jan-12 17:54pm    
Added tags.

1 solution

I think you are having the exception in this line:
C#
Excel.Range workSheet_range = sheet.get_Range("F", "I");

Try changing your code to following it should work:

C#
using Excel;
using System.Reflection; // Needed for Missing.Value

C#
// Code to get to the point of selecting the range
Excel.Application excelApp = new Excel.ApplicationClass();
Excel.Workbook workbook = excelApp.Workbooks.Open("c:\yourexcelfile.xls", 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
Excel.Worksheet worksheet = (Excel.Worksheet) workbook.Worksheets.get_Item("sheet1");
// Select a multi column range
Excel.Range dataRange =worksheet.get_Range("F:F,I:I", Missing.Value);


Also have a look at these two links:
Link 1[^]
Link 2[^]
It will help you solve your problem.

Good luck,
OI
 
Share this answer
 
v3
Comments
thatraja 17-Jan-12 22:41pm    
Right, 5!
BTW I like your profile picture(Angry face) :)
Orcun Iyigun 18-Jan-12 12:02pm    
Haha thanks. hi from Marvin the Martian :P
thatraja 22-Jan-12 10:26am    
/*hi from Marvin the Martian*/
Then what is Orcun Iyigun?
Orcun Iyigun 23-Jan-12 11:53am    
you know I have to keep a low profile since I am the Martian. thats how i hide my self. :)
thatraja 23-Jan-12 12:02pm    
:D Right, be safe here

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900