简体   繁体   English

使用apache POI的Excel工作表格式

[英]Excel sheet formatting using apache POI

I am working on creating an excel report from java code using apache POI library. 我正在使用apache POI库从java代码创建excel报告。 I have encountered a problem while formatting excel cell. 我在格式化excel单元格时遇到问题。 Please find below piece of code. 请在下面找到一段代码。

    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet("coverageData");
    int rownum = 0,cellnum=0;
    Row row1 = sheet.createRow(rownum++);
    Cell cell10 = row1.createCell(cellnum++);
    cell10.setCellValue("cell data");
    XSSFCellStyle row1style = (XSSFCellStyle)cell10.getCellStyle();
    XSSFColor grayColor = new XSSFColor(Color.DARK_GRAY);
    row1style.setFillBackgroundColor(grayColor);
    cell10.setCellStyle(row1style);
    try         
    {             
        //Write the workbook in file system
        FileOutputStream out = new FileOutputStream(new File("P:\\automation\\Spreadsheet.xlsx"));
        workbook.write(out); 
        out.close(); 
        System.out.println("Spreadsheet.xlsx written successfully on disk."); 
    }   
    catch (Exception e)
    {  
        e.printStackTrace();
    } 

The problem here is i am setting cell10 style in last line of code, but it is not getting affected in the excel sheet created by the program. 这里的问题是我在最后一行代码中设置了cell10样式,但它没有受到程序创建的excel表的影响。

Thanks in advance. 提前致谢。

Try it, bellow work fine for me: 尝试一下,对我来说工作正常:

import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class TestClass {
    public static void main(String[] args){
        Workbook wb = new XSSFWorkbook();
        Sheet sheet = wb.createSheet("coverageData");
        int rownum = 0,cellnum=0;
        Row row1 = sheet.createRow((short)rownum++);
        //Set Color style start
        CellStyle row1style = wb.createCellStyle();
        row1style.setFillBackgroundColor(IndexedColors.LIGHT_GREEN.getIndex());
        row1style.setFillPattern(CellStyle.BIG_SPOTS);
        //Set Color style end
        Cell cell10 = row1.createCell((short)cellnum++);
        cell10.setCellStyle(row1style);
        cell10.setCellValue("cell data");
        try{             
            //Write the workbook in file system
            FileOutputStream out = new FileOutputStream(new File("Spreadsheet.xlsx"));
            wb.write(out); 
            out.close(); 
            System.out.println("Spreadsheet.xlsx written successfully on disk."); 
        } catch (Exception e) { e.printStackTrace(); } 
    }
}

You need to set the color like this: 你需要设置这样的颜色:

final XSSFCellStyle description = (XSSFCellStyle) workbook.createCellStyle();
description.setFillForegroundColor( yourColor );
description.setFillPattern( FillPatternType.SOLID_FOREGROUND );

This is, you need a fill pattern 这是,你需要一个填充模式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM