2012-09-05 38 views
30

Làm thế nào để làm cho toàn bộ hàng khối excel được in đậm văn bản bằng cách sử dụng Apache POI?Làm cách nào để áp dụng kiểu chữ đậm cho toàn bộ hàng bằng cách sử dụng Apache POI?

Ví dụ:
Tiêu đề cột phải được in đậm. Thay vì áp dụng kiểu cho mỗi và mọi ô của hàng tiêu đề, làm cách nào tôi có thể áp dụng một số kiểu cho toàn bộ hàng?

+0

này cũng rất hữu ích: http://thinktibits.blogspot.com/2012/ 12/Java-POI-Định dạng-Excel-Ô-Đậm-Ví dụ-Program.html – NoNaMe

+0

Liên kết này có thể giúp bạn. [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-đậm-văn bản-phong cách-với-nền-màu) – Peter

Trả lời

32

Điều này sẽ hoạt động tốt.

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); 

Nếu bạn không tạo defaultFont tất cả sổ làm việc của bạn sẽ sử dụng một cách khác làm mặc định.

Hy vọng nó giúp,

+0

Rất tiếc, tôi đã quên mất ý tưởng. Tôi đang sử dụng HSSFWorkbook. – Krishnamachary

+2

HSSFWorkbook hwb = new HSSFWorkbook(); HSSFSheet sheet = hwb.crateSheet ("Trang tính mới"); HssfRow headRow = sheet.createRow ((int) 0); kiểu CellStyle = headRow.getRowStyle(); Phông chữ đậmFont = hwb.createFont(); boldFont.setBoldweight (Font.BOLDWEIGHT_BOLD); style.setFont (boldFont); //headRow.setRowStyle(style); -> này không hoạt động tế bào di động = headRow.createCell ((int) 0) cell.setCellStyle (phong cách); -> này đang làm việc Tôi muốn để áp dụng kiểu in đậm để lôi kéo hàng, thay vì các ô riêng lẻ. – Krishnamachary

+1

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

5

Công việc này đối với tôi

tôi đặt kiểu font của trước và làm rowheader thường sau đó tôi đặt trong vòng lặp cho phong cách với phông chữ in đậm trên mỗi tế bào của rowhead. Hàng đầu tiên của Et voilà được in đậm.

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

Bạn có biết cơ hội nào không tại sao row.setRowStyle() không hoạt động? :/ –

11

Hãy tìm thấy bên dưới một cách dễ dàng:

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

Bạn có biết tại sao row.setRowStyle() không hoạt động? :/ –

1

này làm việc cho tôi

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

Bạn có biết tại sao row.setRowStyle() không hoạt động? :/ –

+0

Không có Erdal. Nếu nó hoạt động, tôi sẽ thực hiện từng hàng một Thay vì cột theo cột trên đầu. –

+1

@ErdalG. row.setRowStyle hoạt động nếu được thiết lập sau khi thiết lập các giá trị trong các ô của hàng. Nếu ở trên câu trả lời, setRowStyle được thực hiện sau vòng lặp, nó sẽ hoạt động! Thử nghiệm với phiên bản Poi 3.9. – Champ

Các vấn đề liên quan