2012-09-05 202 views
30

¿Cómo hacer que todo un texto de las celdas de fila de Excel sea negrita usando Apache POI?¿Cómo aplicar el estilo de texto en negrita para toda una fila usando Apache POI?

por ejemplo:
encabezamientos de las columnas deben estar en negrita. En lugar de aplicar estilo para todas y cada una de las celdas de la fila del encabezado, ¿cómo puedo aplicar un estilo a una fila completa?

+0

esto también es útil: http://thinktibits.blogspot.com/2012/ 12/Java-POI-Format-Excel-Cell-Bold-Example-Program.html – NoNaMe

+0

Este enlace puede ayudarlo. [http://stackoverflow.com/questions/37188540/java-code-for-excel-row-in-bold-text-style-with-background-color](http://stackoverflow.com/questions/37188540/ java-code-for-excel-row-in-bold-text-style-with-background-color) – Peter

Respuesta

32

Esto debería funcionar bien.

Workbook wb = new XSSFWorkbook("myWorkbook.xlsx"); 
    Row row=sheet.getRow(0); 
    CellStyle style=null; 

    XSSFFont defaultFont= wb.createFont(); 
    defaultFont.setFontHeightInPoints((short)10); 
    defaultFont.setFontName("Arial"); 
    defaultFont.setColor(IndexedColors.BLACK.getIndex()); 
    defaultFont.setBold(false); 
    defaultFont.setItalic(false); 

    XSSFFont font= wb.createFont(); 
    font.setFontHeightInPoints((short)10); 
    font.setFontName("Arial"); 
    font.setColor(IndexedColors.WHITE.getIndex()); 
    font.setBold(true); 
    font.setItalic(false); 

    style=row.getRowStyle(); 
    style.setFillBackgroundColor(IndexedColors.DARK_BLUE.getIndex()); 
    style.setFillPattern(CellStyle.SOLID_FOREGROUND); 
    style.setAlignment(CellStyle.ALIGN_CENTER); 
    style.setFont(font); 

Si no se crea defaultFont todo el libro va a utilizar el otro como predeterminado.

creo que sirve,

+0

Lo siento, me olvidé de mentntion. Estoy usando HSSFWorkbook. – Krishnamachary

+2

HSSFWorkbook hwb = new HSSFWorkbook(); HSSFS sheet = hwb.crateSheet ("Nueva hoja"); HssfRow headRow = sheet.createRow ((int) 0); CellStyle style = headRow.getRowStyle(); Fuente boldFont = hwb.createFont(); boldFont.setBoldweight (Font.BOLDWEIGHT_BOLD); style.setFont (boldFont); //headRow.setRowStyle(style); -> Esto no está funcionando celular celular = headRow.createCell ((int) 0) cell.setCellStyle (estilo); -> esto está funcionando quiero para aplicar el estilo en negrita para crear filas, en lugar de celdas individuales. – Krishnamachary

+1

http://stackoverflow.com/a/11792648/1211000 – swamy

5

Este trabajo para mí

puse la fuente de estilo antes y hago normalmente rowheader entonces me encuentra en bucle para el estilo de letra en negrita con en cada célula del rowhead. Et voilà primera fila está en negrita.

HSSFWorkbook wb = new HSSFWorkbook(); 
HSSFSheet sheet = wb.createSheet("FirstSheet"); 
HSSFRow rowhead = sheet.createRow(0); 
HSSFCellStyle style = wb.createCellStyle(); 
HSSFFont font = wb.createFont(); 
font.setFontName(HSSFFont.FONT_ARIAL); 
font.setFontHeightInPoints((short)10); 
font.setBold(true); 
style.setFont(font); 
rowhead.createCell(0).setCellValue("ID"); 
rowhead.createCell(1).setCellValue("First"); 
rowhead.createCell(2).setCellValue("Second"); 
rowhead.createCell(3).setCellValue("Third"); 
for(int j = 0; j<=3; j++) 
rowhead.getCell(j).setCellStyle(style); 
+1

¿Sabe por casualidad? ¿Por qué row.setRowStyle() no funciona? :/ –

11

A continuación el camino más fácil:

XSSFCellStyle style = workbook.createCellStyle(); 
style.setBorderTop((short) 6); // double lines border 
style.setBorderBottom((short) 1); // single line border 
XSSFFont font = workbook.createFont(); 
font.setFontHeightInPoints((short) 15); 
font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD); 
style.setFont(font);     

Row row = sheet.createRow(0); 
Cell cell0 = row.createCell(0); 
cell0.setCellValue("Nav Value"); 
cell0.setCellStyle(style); 
for(int j = 0; j<=3; j++) 
row.getCell(j).setCellStyle(style); 
+2

¿Sabe por casualidad por qué row.setRowStyle() no funciona? :/ –

1

Esto funcionó para mí

Object[][] bookData = { { "col1", "col2", 3 }, { "col1", "col2", 3 }, { "col1", "col2", 3 }, 
      { "col1", "col2", 3 }, { "col1", "col2", 3 }, { "col1", "col2", 3 } }; 

    String[] headers = new String[] { "HEader 1", "HEader 2", "HEader 3" }; 

    int noOfColumns = headers.length; 
    int rowCount = 0; 

    Row rowZero = sheet.createRow(rowCount++); 
    CellStyle style = workbook.createCellStyle(); 
    Font font = workbook.createFont(); 
    font.setBoldweight(Font.BOLDWEIGHT_BOLD); 
    style.setFont(font); 
    for (int col = 1; col <= noOfColumns; col++) { 
     Cell cell = rowZero.createCell(col); 
     cell.setCellValue(headers[col - 1]); 
     cell.setCellStyle(style); 
    } 
+0

¿Sabe por casualidad por qué row.setRowStyle() no está funcionando? :/ –

+0

Sin Erdal. Si funcionó, habría hecho fila por fila solamente en lugar de columna por columna. –

+1

@ErdalG. row.setRowStyle funciona si se establece después de establecer valores en las celdas de la fila. Si en la respuesta anterior, setRowStyle se hace después de for loop, ¡funcionará! Probado con la versión Poi 3.9. – Champ

Cuestiones relacionadas