Java的code为与背景颜色粗体文字样式的Excel行 [英] Java code for excel row in Bold text style with Background color

查看:892
本文介绍了Java的code为与背景颜色粗体文字样式的Excel行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Google搜索一些code和发现了一些答案,但没能得到我的Excel文件输出大胆并设置背景颜色。我曾尝试与以下code。你能告诉我我要去哪里错了?请看一下。谢谢你。


  

FYI:我准备做第一排的BOLD用蓝色或浅色
  背景。如果你知道,请与code帮助。


  // Excel文件生成code
            HSSFWorkbook工作簿=新HSSFWorkbook();
            HSSFSheet表= workbook.createSheet(读经);
            CellStyle风格= workbook.createCellStyle();
            字体字型= workbook.createFont();
            font.setFontHeightInPoints((短)11);
            font.setFontName(HSSFFont.FONT_ARIAL);
            font.setBoldweight(HSSFFont.COLOR_NORMAL);
            font.setBold(真);
            font.setColor(HSSFColor.DARK_BLUE.index);            style.setFont(字体);            sheet.createFreezePane(0,1); //冻结第一排sheet.createFreezePane(INT colSplit,诠释rowSplit,诠释leftmostColumn,诠释topRow)                HSSFRow行= sheet.createRow(1);
                HSSFRow rowhead = sheet.createRow((短)0);
                rowhead.setRowStyle(样式);                rowhead.createCell(0).setCellValue(RUN);
                rowhead.createCell(1).setCellValue(数字);


解决方案

您在以下做错了:

1 - 您没有设置任何背景颜色;

2 - 当你创建它们覆盖行样式的新的细胞,所以你需要设置
风格为您创建的每一个新细胞;

下面是工作code:

 的FileOutputStream FILEOUT =新的FileOutputStream(POI-TEST.XLS);
HSSFWorkbook工作簿=新HSSFWorkbook();
HSSFSheet表= workbook.createSheet(读经);
CellStyle风格= workbook.createCellStyle();
字体字型= workbook.createFont();
font.setFontHeightInPoints((短)11);
font.setFontName(HSSFFont.FONT_ARIAL);
font.setBoldweight(HSSFFont.COLOR_NORMAL);
font.setBold(真);
font.setColor(HSSFColor.DARK_BLUE.index);style.setFont(字体);
//添加这些行
style.setFillForegroundColor(IndexedColors.AQUA.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);sheet.createFreezePane(0,1); //冻结第一排sheet.createFreezePane(INT colSplit,诠释rowSplit,诠释leftmostColumn,诠释topRow)
 HSSFRow rowhead = sheet.createRow((短)0);
 rowhead.setRowStyle(样式);
 //设置CELL0风格
 HSSFCell CELL0 = rowhead.createCell(0);
 cell0.setCellStyle(样式);
 cell0.setCellValue(行);
 //设置CELL1风格
 HSSFCell CELL1 = rowhead.createCell(1);
 cell1.setCellStyle(样式);
 cell1.setCellValue(数字); workbook.write(FILEOUT);

文件输出:

结果

I have googled some code and found some answers but not able to get my excel file output in Bold and set background color. I have tried with following code. can you please tell me where am I going wrong? Please take a look. Thanks.

FYI: I am going to make 1st Row in BOLD with blue or any Light color background. If you know please help with the code.

// Excel file generation code
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet("Readings");
            CellStyle style = workbook.createCellStyle();
            Font font = workbook.createFont();
            font.setFontHeightInPoints((short)11);
            font.setFontName(HSSFFont.FONT_ARIAL);
            font.setBoldweight(HSSFFont.COLOR_NORMAL);
            font.setBold(true);
            font.setColor(HSSFColor.DARK_BLUE.index);

            style.setFont(font);

            sheet.createFreezePane(0, 1); // Freeze 1st Row   sheet.createFreezePane(int colSplit, int rowSplit, int leftmostColumn, int topRow)

                HSSFRow row = sheet.createRow(1);
                HSSFRow rowhead = sheet.createRow((short) 0);
                rowhead.setRowStyle(style);

                rowhead.createCell(0).setCellValue("RUN");
                rowhead.createCell(1).setCellValue("NUMBER");

解决方案

You are doing wrong in the following:

1- You are not setting any Background color;

2- When you create the new cells they override the row style, so you need to set the style for each new cell you create;

Below is the working code:

FileOutputStream fileOut = new FileOutputStream("poi-test.xls");
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Readings");
CellStyle style = workbook.createCellStyle();
Font font = workbook.createFont();
font.setFontHeightInPoints((short)11);
font.setFontName(HSSFFont.FONT_ARIAL);
font.setBoldweight(HSSFFont.COLOR_NORMAL);
font.setBold(true);
font.setColor(HSSFColor.DARK_BLUE.index);

style.setFont(font);
//Add these lines     
style.setFillForegroundColor(IndexedColors.AQUA.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);

sheet.createFreezePane(0, 1); // Freeze 1st Row   sheet.createFreezePane(int colSplit, int rowSplit, int leftmostColumn, int topRow)


 HSSFRow rowhead = sheet.createRow((short) 0);
 rowhead.setRowStyle(style);
 //Set the cell0 Style        
 HSSFCell cell0 = rowhead.createCell(0);
 cell0.setCellStyle(style);
 cell0.setCellValue("ROW");
 //Set the cell1 Style        
 HSSFCell cell1 = rowhead.createCell(1);
 cell1.setCellStyle(style);
 cell1.setCellValue("NUMBER");

 workbook.write(fileOut);

file output:

这篇关于Java的code为与背景颜色粗体文字样式的Excel行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆