java 如何使用 Apache POI 选择和加粗整个工作表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5465607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:14:40  来源:igfitidea点击:

How to select and bold the whole worksheet with Apache POI

javaexcelapache-poi

提问by mememoremore

I am a beginner with Apache POI library.

我是 Apache POI 库的初学者。

in VBA, I know I can select and bold the whole worksheet with following code

在 VBA 中,我知道我可以使用以下代码选择并加粗整个工作表

Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets(1)
ws.Cells.Font.Bold = True

May I know how to select and bold the whole sheet by coding with Apache POI library?

我可以知道如何通过使用 Apache POI 库进行编码来选择和加粗整个工作表吗?

thanks

谢谢

回答by CoolBeans

There is a pretty good example on this link.

这个链接上有一个很好的例子。

Sheet sheet = wb.createSheet("test");
CellStyle cs = wb.createCellStyle();
Font f = wb.createFont();
f.setBoldweight(Font.BOLDWEIGHT_BOLD);
cs.setFont(f);
sheet.setDefaultColumnStyle(1,cs); //set bold for column 1

回答by romeara

The default font for a workbook can be retrieved from index 0. So to modify the font bold setting default for the workbook:

可以从索引 0 中检索工作簿的默认字体。因此要修改工作簿的字体粗体设置默认值:

private void setWorkbookDefaultFontToBold(Workbook workbook){
    Font defaultFont = workbook.getFontAt(0);
    defaultFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
}

It's a really obscure piece of information - it's in the POI SheetJavadoc for setColumnWidth, in the second or so line:

这是一条非常晦涩的信息 - 它在setColumnWidth的 POI SheetJavadoc 中,在第二行左右:

"...can be displayed in a cell that is formatted with the standard font (first font in the workbook)."

“...可以显示在使用标准字体(工作簿中的第一种字体)格式化的单元格中。”

I haven't had to use it heavily, so it may have just happened to work for me (the location and non-prevalence of documentation on it makes me slightly leary of recommending depending on it) but it's somewhere you could start looking

我不必大量使用它,所以它可能刚好对我有用(它的位置和文档的不流行让我对推荐依赖它有点犹豫)但它是你可以开始寻找的地方

回答by swamy

   private HSSFFont createAndSetFontStyle(HSSFWorkbook wb) {
    HSSFFont font = wb.createFont();
    font.setFontName(XSSFFont.DEFAULT_FONT_NAME);
    font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
    font.setFontHeightInPoints((short)10);
    return font;
}


    HSSFCellStyle cellStyle = workBook.createCellStyle();
    HSSFFont createfont = createAndSetFontStyle(workBook);
    cellStyle.setFont(createfont);

    cell.setCellStyle(cellStyle);