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?
 
NorieVBA ExpertCommented:
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
 
Rgonzo1971Commented:
Hi,

pls try
rngXL.BorderAround2();

Open in new window

Regards
0
 
NorieVBA ExpertCommented:
So the entire 'table' should have borders and the headers should be bold?
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Ganesh STech Lead cum developerAuthor Commented:
Yes.exactly...
0
 
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
 
Ganesh STech Lead cum developerAuthor Commented:
Excellent solution
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.