Krishnamachary
¿Cómo hacer un texto en negrita de celdas de fila de Excel completo usando Apache POI?
P.ej:
Los encabezados de las columnas deben estar en negrita. En lugar de aplicar estilo para todas y cada una de las celdas de la fila de encabezado, ¿cómo puedo aplicar algo de estilo a una fila completa?
ArtiBucco
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 creas defaultFont
todo su libro de trabajo usará el otro como predeterminado.
-
Lo siento, olvidé mencionar. Estoy usando HSSFWorkbook.
– Krishnamachary
8 sep 2012 a las 18:21
-
HSSFWorkbook hwb=nuevo HSSFWorkbook(); HSSFSheet sheet=hwb.crateSheet(“Nueva hoja”); HssfRow headRow=hoja.createRow((int)0); Estilo CellStyle=headRow.getRowStyle(); Fuente boldFont=hwb.createFont(); boldFont.setBoldweight(Fuente.BOLDWEIGHT_BOLD); estilo.setFont(fuente en negrita); //headRow.setRowStyle(style);–> esto no funciona Cell cell=headRow.createCell((int)0) cell.setCellStyle(style);–> esto funciona Quiero aplicar el estilo en negrita a la entidad fila, en lugar de celdas individuales.
– Krishnamachary
8 sep 2012 a las 18:47
-
row.getRowStyle()
me devuelve nulo– Danny Lo
1 de noviembre de 2014 a las 18:19
-
incluso si las columnas se crean todavía fila.getRowStyle() devuelve nulo
– Un poco
7 mayo 2016 a las 8:20
-
stackoverflow.com/questions/25437431/…
– Alfaz Jikani
11 de octubre de 2017 a las 13:27
satanás
Por favor, encuentre a continuación la manera 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);
-
¿Sabe por casualidad por qué row.setRowStyle() no funciona? :/
– Erdal G.
29 de julio de 2016 a las 17:55
Adhamo
este trabajo para mi
Configuro la fuente del estilo antes y hago el encabezado de fila normalmente, luego configuro un bucle para el estilo con la fuente en negrita en cada celda del encabezado de fila. Et voilà, la 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);
-
¿Sabe por casualidad por qué row.setRowStyle() no funciona? :/
– Erdal G.
29 de julio de 2016 a las 17:59
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);
}
Un ejemplo trabajado, completo y sencillo:
package io.github.baijifeilong.excel;
import lombok.SneakyThrows;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
/**
* Created by BaiJiFeiLong@gmail.com at 2019/12/6 11:41
*/
public class ExcelBoldTextDemo {
@SneakyThrows
public static void main(String[] args) {
new XSSFWorkbook() {{
XSSFRow row = createSheet().createRow(0);
row.setRowStyle(createCellStyle());
row.getRowStyle().getFont().setBold(true);
row.createCell(0).setCellValue("Alpha");
row.createCell(1).setCellValue("Beta");
row.createCell(2).setCellValue("Gamma");
}}.write(new FileOutputStream("demo.xlsx"));
}
}
-
Que es
createCellStyle()
?– usuario13182598
2 sep 2020 a las 11:50
Laurel
public class ExcelReader {
private XSSFWorkbook workBook;
private XSSFSheet workSheet;
public ExcelReader(String path, String sheetName){
File file = new File(path);
try {
FileInputStream inputStream = new FileInputStream(file);
workBook = new XSSFWorkbook(inputStream);
workSheet = workBook.getSheet(sheetName);
workBook.close();
}catch (Exception e){
e.printStackTrace();
}
}
public Object[][] getData(){
int rows = workSheet.getLastRowNum(); // returns number of rows
int cols = workSheet.getRow(0).getLastCellNum(); //returns number of cols
Object[][] data = new Object[rows][1];
for (int i = 0; i < rows; i++) {
Map<String,String> map = new HashMap<>();
for (int j = 0; j < cols; j++) {
//each column name is a key
XSSFCell cell = workSheet.getRow(i + 1).getCell(j);// might be null sometimes if the cell is empty
if (cell == null){
System.out.println();
}
map.put(workSheet.getRow(0).getCell(j).toString(),
// each cell under column name will be value
cell == null ? "" : cell.toString() );
}
data[i][0] = map;
}
return data;
}
}
-
Que es
createCellStyle()
?– usuario13182598
2 sep 2020 a las 11:50
esto también es útil: thinktibits.blogspot.com/2012/12/…
– Sin nombre
27 de febrero de 2015 a las 10:03
Este enlace podría ayudarte. http://stackoverflow.com/questions/37188540/java-code-for-excel-row-in-bold-text-style-with-background-color
– Pedro
13 de mayo de 2016 a las 5:35