C# - How to set border for each celll in worksheet

Hi,

I am printing some value in worksheet from data table.

How to set border for each  cell (for all the records from data table).


for example,

i want to print all the cell with border at all the records including header. (with header font is bold).

see my code..

  var rngXL = xlWorkSheet.Cells[10, "E"];

            foreach (DataColumn dc in resOutput.Columns)
            {
                rngXL.Value = dc.ToString();
                rngXL = rngXL.Offset[0, 1];
            }

            rngXL = xlWorkSheet.Cells[11Output_Excel.png, "E"];

            foreach (DataRow dr in resOutput.Rows)
            {
                var arr = dr.ItemArray;
                rngXL.Resize[1, resOutput.Columns.Count].Value = arr;
                rngXL = rngXL.Offset[1, 0];
            }
Ganesh STech Lead cum developerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

pls try
rngXL.BorderAround2();

Open in new window

Regards
0
NorieAnalyst Assistant Commented:
So the entire 'table' should have borders and the headers should be bold?
0
Ganesh STech Lead cum developerAuthor Commented:
Yes.exactly...
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Rgonzo1971Commented:
then try
rngXL.BorderAround2();
rngXL.Font.Bold = true;

Open in new window

0
Ganesh STech Lead cum developerAuthor Commented:
getting error. PFA.

Error - 'System.__ComObject' does not contain a definition for 'BorderAround2'
0
Rgonzo1971Commented:
then try
rngXL.BorderAround(missing, Excel.XlBorderWeight.xlThick, 
        Excel.XlColorIndex.xlColorIndexAutomatic);

Open in new window

0
NorieAnalyst Assistant Commented:
Here's an update to the original code.
private void button1_Click(object sender, EventArgs e)
        {
            var appXL = new Excel.Application
            {
                Visible = true
            };

            Excel.Workbook wbXL = appXL.Workbooks.Open("C:\\Test\\EETestWB.xlsx");

            Excel.Worksheet wsXL = (Excel.Worksheet) wbXL.Sheets[1];

            Excel.Range rngXL = (Excel.Range) wsXL.Cells[12, "C"];
            
            DataTable dt = (DataTable)(dataGridView1.DataSource);
            
            foreach (DataColumn dc in dt.Columns)
            {
                rngXL.Value = dc.ToString();
                rngXL = rngXL.Offset[0, 1];
            }

            rngXL = (Excel.Range)wsXL.Cells[13, "C"];

            foreach (DataRow  dr in dt.Rows)
            {
                object[] arr = dr.ItemArray;
                rngXL.Resize[1, dt.Columns.Count].Value = arr;
                rngXL = rngXL.Offset[1, 0];
            }

            Excel.Range rngTable = wsXL.Cells[12, "C"].Resize[dt.Rows.Count+1, dt.Columns.Count];

            rngTable.Rows[1].font.bold = true;

            rngTable.Cells.Borders.Weight = Excel.XlBorderWeight.xlThin;


            GC.Collect();

            GC.WaitForPendingFinalizers();

            wbXL.Save();

            appXL.UserControl = true;

        }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ganesh STech Lead cum developerAuthor Commented:
Excellent solution
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.