【问题标题】:Excel sheet formatting using apache POI使用 apache POI 格式化 Excel 工作表
【发布时间】:2014-07-02 08:43:07
【问题描述】:

我正在使用 apache POI 库从 java 代码创建一个 excel 报告。格式化excel单元格时遇到问题。请在下面找到一段代码。

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

这里的问题是我在最后一行代码中设置了 cell10 样式,但它在程序创建的 excel 表中没有受到影响。

提前致谢。

【问题讨论】:

    标签: java excel apache-poi


    【解决方案1】:

    试一试,下面对我来说很好用:

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

    【讨论】:

    • 感谢迈克尔的回复。我也尝试使用“CellStyle”,但同样的问题,虽然我可以在单元格中看到单元格数据,但格式没有受到影响。
    • 是的,迈克尔,您的代码适用于普通的“工作表”,但对于我的 xssfsheet,上面带有 getIndex 的代码无法正常工作,并且从@verbose 回答中我认为我们需要明确使用“setFillPattern” .
    【解决方案2】:

    你需要这样设置颜色:

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

    这是,你需要一个填充图案

    【讨论】:

    • 感谢@verbose-mode 这对填充模式很好。我们是否有类似的方法来添加边框和使单元格数据加粗?
    • 您需要在样式中添加字体 (XSSFFont valueCellFont = (XSSFFont) workbook.createFont()) - 您可以将字体配置为粗体。 XSSFCellStyle 也有设置边框的方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多