Bordes en Excel
Publicado por
Will (1 intervención) el 24/05/2012 18:43:35
Puedes utilizar el siguiente código que esta escrito en C# utilizando el objeto Microsoft Excel 14.0 Object Library como referencia agregada.
private string ExportarBitacora()
{
//ABRIMOS UNA INSTANCIA DE MICROSOFT EXCEL
Excel.ApplicationClass excel = new Excel.ApplicationClass();
//CREAMOS UN NUEVO LIBRO
Excel._Workbook oWB = (Excel._Workbook)(excel.Workbooks.Add(Missing.Value));
//AL CREAR EL LIBRO EN EXCEL SE AGREGAN AUTOMATICAMENTE 3 HOJAS. DEBIDO A QUE EL LIBRO DEBE CONTENER AL MENOS UNA HOJA SOLO PUEDO ELIMINAR
//LAS DOS PRIMERAS HOJAS. AL ELIMINAR UNA HOJA SE VA RE-AJUSTANDO EL ARREGLO POR ESO SIEMPRE ELIMINO EL INDICE 1
((Excel._Worksheet)oWB.Sheets[1]).Delete();
((Excel._Worksheet)oWB.Sheets[1]).Delete();
//COMO ME QUEDO SIEMPRE CON UNA HOJA, LA ASIGNO A MI PRIMER OBJETO HOJA CON EL QUE VOY A TRABAJAR Y LE DOY UN NOMBRE
Excel._Worksheet oSheetBitIni = (Excel._Worksheet)oWB.Sheets[1];
oSheetBitIni.Name = "Bitacora de Inicio"; //EL NOMBRE DE LA HOJA NO DEBE SOBREPASAR LOS 31 CARACTERES
//COMO AGREGAR UNA NUEVA HOJA
//Microsoft.Office.Interop.Excel._Worksheet oSheet2 = (Microsoft.Office.Interop.Excel._Worksheet)oWB.Sheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value);
oSheetBitIni.Cells[2, 13] = "FECHA:";
// AQUI SE APLICA EL BORDE INFERIOR:
oSheetBitIni.get_Range("N2", "N2").Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Excel.XlLineStyle.xlContinuous; //Borde inferior en una celda
}
NOTA: Si estas usando Visual Basic creo que debes reemplazar los corchetes de la propiedad "Borders" por parentesis.
Ej: .Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle
Otros datos interesantes para formateo de datos y celdas en Excel desde C#:
oSheetBitIni.Cells[2, 3] = "BITACORA DIARIA INSPECCION INICIO JORNADA";
oSheetBitIni.get_Range("C2", "C2").RowHeight = 25.5; //Alto de fila
oSheetBitIni.get_Range("A11", "A11").ColumnWidth = 6.57; //Ancho de la columna
oSheetBitIni.get_Range("A1", "N1").Interior.ColorIndex = 16; //Color de fondo (1-255 colores)
oSheetBitIni.get_Range("A1", "N1").Interior.Color = Color.FromArgb(255, 255, 255); //Color de fondo (millones de colores)
oSheetBitIni.get_Range("K3", "K3").Font.ColorIndex = 7; //Color de fuente (1-255 colores)
oSheetBitIni.get_Range("K3", "K3").Font.Color = Color.FromArgb(128, 128, 128); //Color de fuente (millones de colores)
oSheetBitIni.get_Range("C2", "C2").Font.Name = "Tahoma"; //Tipo de fuente
oSheetBitIni.get_Range("C2", "C2").Font.Size = 20; //Tamaño de fuente
oSheetBitIni.get_Range("C2", "C2").Font.Bold = true; //EN NEGRITAS
oSheetBitIni.get_Range("A11", "N11").WrapText = true; //El texto se ajusta al tamaño de la celda
oSheetBitIni.get_Range("A11", "N11").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;//Alineación horizontal
oSheetBitIni.get_Range("A11", "N11").VerticalAlignment = Excel.XlHAlign.xlHAlignCenter;//Alineación horizontal
oSheetBitIni.get_Range("A5", "N5").MergeCells = true; //COMBINAR LAS CELDAS
oSheetBitIni.get_Range("C3", "C3").Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Excel.XlLineStyle.xlContinuous; //Borde inferior en una celda
oSheetBitIni.get_Range("C3", "C3").Borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle = Excel.XlLineStyle.xlContinuous; //Borde superior en una celda
oSheetBitIni.get_Range("C3", "C3").Borders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle = Excel.XlLineStyle.xlContinuous; //Borde izquierdo en una celda
oSheetBitIni.get_Range("C3", "C3").Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous; //Borde derecho en una celda
oSheetBitIni.get_Range("K5", "L9").BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium, Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic); //BORDE COMPLETO (todo el contorno de la celda)
FUENTES:
http://www.functionx.com/vbaexcel/cells/Lesson5.htm
http://www.functionx.com/vbaexcel/cells/Lesson4.htm
Para cada lenguaje solo hay que adaptar el código. Esto esta escrito en C#.
Espero que te de una buena idea de como se soluciona tu problema.
Salu2 desde Celaya, gto., Mex.