【问题标题】:How to add background color for a specific row or header in excel sheets using apache poi如何使用apache poi在excel表格中为特定行或标题添加背景颜色
【发布时间】:2019-12-04 06:37:20
【问题描述】:

如何在下面的代码中使用 apache poi 为 Excel 表格中的特定行或标题添加背景颜色,在官方 giude 中作为示例给出

package com.raz.api.engine.apiengine.util;
import java.io.File;    
import java.io.FileOutputStream;

import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class xlGen {
public static void main(String[] args) throws Exception {

  //Create blank workbook
  XSSFWorkbook workbook = new XSSFWorkbook(); 

  //Create a blank sheet
  XSSFSheet spreadsheet = workbook.createSheet(" Employee Info ");

  //Create row object
  XSSFRow row;

  //Colors
  CellStyle style = workbook.createCellStyle();
    style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
    //style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    Font font = workbook.createFont();
    font.setColor(IndexedColors.RED.getIndex());
    style.setFont(font);


  //This data needs to be written (Object[])
  Map < String, Object[] > empinfo = 
  new TreeMap < String, Object[] >();
  empinfo.put( "1", new Object[] { "EMP ID", "EMP NAME", "DESIGNATION" });
  empinfo.put( "2", new Object[] { "tp01", "Gopal", "Technical Manager" });
  empinfo.put( "3", new Object[] { "tp02", "Manisha", "Proof Reader" });
  empinfo.put( "4", new Object[] { "tp03", "Masthan", "Technical Writer" });
  empinfo.put( "5", new Object[] { "tp04", "Satish", "Technical Writer" });
  empinfo.put( "6", new Object[] { "tp05", "Krishna", "Technical Writer" });

  //Iterate over data and write to sheet
  Set < String > keyid = empinfo.keySet();
  int rowid = 0;

  for (String key : keyid) {
     row = spreadsheet.createRow(rowid++);
     Object [] objectArr = empinfo.get(key);
     int cellid = 0;

     for (Object obj : objectArr) {
        Cell cell = row.createCell(cellid++);
        cell.setCellValue((String)obj);
     }
  }
  //cell1.setCellStyle(style);


  //Write the workbook in file system
  FileOutputStream out = new FileOutputStream(new File("Writesheet.xlsx"));
  workbook.write(out);
  out.close();
  System.out.println("Writesheet.xlsx written successfully");
}
}

不要阅读下面的这一段.. 所以我需要给标题一些颜色,我想学习一些在 apache poi 中使用的特性和方法 在本页中,我们将学习如何在 XLSX 中设置颜色。通常我们需要在excel文件中为我们的行和单元格设置背景颜色和字体颜色。设置颜色可用作标题或列名,增加 Excel 文件的可读性。在这个例子中,我们将从头开始了解如何在 XLSX 中着色。我们只需要获取 CellStyle 的实例,然后将所需的颜色设置为 CellStyle,然后将其分配给 XLSX 单元格。创建一个 XSSFWorkbook。从 XSSFWorkbook 获取 CellStyle,如下所示。

【问题讨论】:

标签: java excel apache-poi


【解决方案1】:

总是很好阅读:Busy Developers' Guide to HSSF and XSSF Features

在你的情况下,尤其是Fills and colors

你的问题的文字不是很清楚。但我怀疑您的代码示例需要为标题单元格设置特殊样式。

需要在工作簿级别创建 CellStyle。然后在创建单元格时,这个CellStyle 需要使用Cell.setCellStyle 应用于适当的单元格。

以下示例使用当前的apache poi 4.1.1

import java.io.File;    
import java.io.FileOutputStream;

import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class XlGen {

 public static void main(String[] args) throws Exception {

  //Create blank workbook
  Workbook workbook = new XSSFWorkbook(); 

  //Create cell style for header row
  CellStyle style = workbook.createCellStyle();
  style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
  style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  Font font = workbook.createFont();
  font.setColor(IndexedColors.YELLOW.getIndex());
  style.setFont(font);

  //Create a blank sheet
  Sheet spreadsheet = workbook.createSheet(" Employee Info ");

  //This data needs to be written (Object[])
  Map < String, Object[] > empinfo = 
  new TreeMap < String, Object[] >();
  empinfo.put( "1", new Object[] { "EMP ID", "EMP NAME", "DESIGNATION" });
  empinfo.put( "2", new Object[] { "tp01", "Gopal", "Technical Manager" });
  empinfo.put( "3", new Object[] { "tp02", "Manisha", "Proof Reader" });
  empinfo.put( "4", new Object[] { "tp03", "Masthan", "Technical Writer" });
  empinfo.put( "5", new Object[] { "tp04", "Satish", "Technical Writer" });
  empinfo.put( "6", new Object[] { "tp05", "Krishna", "Technical Writer" });

  //Iterate over data and write to sheet
  Set < String > keyid = empinfo.keySet();
  int rowid = 0;

  //Create row object
  Row row;
  //Create cell object
  Cell cell;

  for (String key : keyid) {
   row = spreadsheet.createRow(rowid++);
   Object [] objectArr = empinfo.get(key);
   int cellid = 0;

   for (Object obj : objectArr) {
    cell = row.createCell(cellid++);
    cell.setCellValue((String)obj);

    if (rowid == 1) { // it is header row
     cell.setCellStyle(style); // set style for header cells
    }
   }
  }

  for (int c = 0; c < empinfo.get("1").length; c++) {
   spreadsheet.autoSizeColumn(c);
  }

  //Write the workbook in file system
  FileOutputStream out = new FileOutputStream(new File("Writesheet.xlsx"));
  workbook.write(out);
  out.close();
  System.out.println("Writesheet.xlsx written successfully");
 }

}

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    • 2014-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多