Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys. I just want to format the Excel sheet rows in C#.
I am using Excel Object Library 12.0.

What I actually need is, I want to select 3 entire rows together to set the properties only for the selected 3 rows. The same things for the column also.

Is it possible?

Actually
<pre>WorkSheet.Rows</pre>
is used to select the rows but it returns the range of whole rows,but i need to select 2 to 3 rows only.

If anyone know the answer please let me know.

Thanks in advance!

-- AGK --
Posted
Updated 8-Jan-19 23:45pm
v3
Comments
Nuri Ismail 3-Jan-11 10:59am    
No need to publish your e-mail, you will automatically receive notification messages to your e-mail for each answer, posted to your question.
This is a public forum, please do not publish your e-mail id unless you are a spam fan. :)

Now that it is the new year, you really should take the time to learn how to use an internet search engine.

You really would be amazed at what you can find out by selecting and using a suitable search phrase.

For example, I found this[^] by using c# excel formatting, and there were loads more like it too.

Go on, give it a try!
 
Share this answer
 
 
Share this answer
 
First we should add the Excel object library

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel;
namespace ConsoleApplication9
{
    class Program
    {
        static void Main(string[] args)
        {

Create application object

Microsoft.Office.Interop.Excel.Application obj = new Microsoft.Office.Interop.Excel.Application();

Create object for workbook
Microsoft.Office.Interop.Excel.Workbook book;

Visibility should be true to view the Excel application

obj.Visible = true;

Create worksheet

Microsoft.Office.Interop.Excel.Worksheet hi = new Microsoft.Office.Interop.Excel.Worksheet();

Adding workbook to excel application

book = obj.Workbooks.Add(1);
hi = (Microsoft.Office.Interop.Excel.Worksheet)book.Sheets[1];

create a range object which will be used later

Microsoft.Office.Interop.Excel.Range range;

Get the current worksheet in worksheet object shee
Microsoft.Office.Interop.Excel.Worksheet shee =(Microsoft.Office.Interop.Excel.Worksheet)obj.ActiveSheet;
           shee.Name = "hi";

Set some cell value

shee.Cells[1, 1] = "hello";
           shee.Cells[2, 2] = "hi";
           shee.Cells[3, 3] = "Welcome";

Here is the main picture
Here i am selecting rows 1 to 3 to format.
For that
1.Select the range of rows 1 to 3 to range object

range = shee.Rows.get_Range("1:3","1:3");

2.Select the range object ,wthis will select the region of rows 1 to 3
range.Select();

3.format the region properties
range.Font.Bold = true;
            range.Font.Italic = true;

4.Save the book
book.Save();

       }
   }
}
 
Share this answer
 

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