Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Excel
I need to add borders to excel cells in Automatic colour using xlWorkSheet.Cells.BorderAround() method. Given below is the coding that i use. But it doesn't add any borders to the cell. Can you please let me know what i have done wrong here:
 
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlApp.Visible = false;
xlWorkBook = xlApp.Workbooks.Open(textBox1.Text, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
 
int i = 0;
int j = 0;
 
for (i = 0; i <= dataGridView1.RowCount - 1; i++)
{
for (j = 0; j <= dataGridView1.ColumnCount - 1; j++)
{
DataGridViewCell cell = dataGridView1[j, i];
xlWorkSheet.Cells["19", "I"] = "Availablility";
xlWorkSheet.Cells[i + 20, j + 9] = cell.Value;
xlWorkSheet.Cells.BorderAround(Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlMedium, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.FromArgb(79, 129, 189)));
}
}
Posted 18-Oct-12 22:04pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
hansika attanayake at 19-Oct-12 6:25am
   
Thanks. I referred the links and updated my coding as shown below. But it doesn't add borders to the excel sheet
xlWorkSheet.Cells.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic);
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Solution:
 
for (i = 0; i <= dataGridView1.RowCount - 4; i++)
               {
                   for (j = 0; j <= dataGridView1.ColumnCount - 1; j++)
                   {
                       DataGridViewCell cell = dataGridView1[j, i];
                       xlWorkSheet.Cells["19", "I"] = "Availablility";
                       xlWorkSheet.Cells[i + 20, j + 9] = cell.Value;
                       xlRange = xlWorkSheet.get_Range("I19", xlWorkSheet.Cells[i + 20, j + 9]);
                       xlRange.Cells.Borders.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Black);
                   }
               }
  Permalink  

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

  Print Answers RSS
0 Nirav Prabtani 384
1 OriginalGriff 289
2 Sergey Alexandrovich Kryukov 225
3 _Amy 195
4 Kumarbs 144
0 OriginalGriff 7,799
1 Sergey Alexandrovich Kryukov 6,966
2 Maciej Los 3,994
3 Peter Leow 3,708
4 CHill60 2,742


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 24 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100