Click here to Skip to main content
15,881,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

 
Share this answer
 
Comments
hansikaat 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);
Solution:

C#
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);
                   }
               }
 
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