Skip to main content

Posts

Showing posts with the label Apache POI

Basic object detection in python using OpenCv and Numpy - by Dustbiin

Custom cell color in Excel Apache POI - By Dustbiin

Create custom color for cell background by using java color class Create custom color- XSSFWorkbook workBook = new XSSFWorkbook (); IndexedColorMap colorMap = workbook.getStylesSource().getIndexedColors(); Font tableHeadOneFontStyle = workbook.createFont();  tableHeadOneFontStyle.setBold( true ); tableHeadOneFontStyle.setColor( IndexedColors.BLACK.getIndex() ); XSSFCellStyle tableHeaderOneColOneStyle = workbook.createCellStyle(); tableHeaderOneColOneStyle.setFont( tableHeadOneFontStyle ); tableHeaderOneColOneStyle.setFillForegroundColor( new XSSFColor( new java.awt.Color( 255, 231, 153 ), colorMap ) ); tableHeaderOneColOneStyle.setFillPattern( FillPatternType.SOLID_FOREGROUND ); tableHeaderOneColOneStyle = setLeftRightBorderColor( tableHeaderOneColOneStyle ); tableHeaderOneColOneStyle = alignCenter( tableHeaderOneColOneStyle );

Styling the excel sheet data like font size, color, font family, and text alignment - by Dustbiin

Apache poi provide very powerful fetcher for data formation and changing the color, size and font family- Create workspace and sheet- XSSFWorkbook workBook = new XSSFWorkbook (); Sheet sheet = workBook.createSheet("My sheet"); Create Font object and set font styling- Font font = workbook.createFont(); font.setBold( true ); font.setFontHeight( (short) ( 25 * 10 ) ); // set font family font.setFontName( "Courier New" ); // set font color font.setColor( IndexedColors.WHITE.getIndex() ); Create CellStyle object and set alignment of data in cell and set font to the data- CellStyle cellStyle = new CellStyle(); cellStyle.setFont(font); // use when you want to wrap text in multipal line cellStyle.setWrapText(true); Create Row and Cell and set cellStyle for the cell data- XSSFRow row = sheet.createRow( 1 ); Cell cell = row.createCell(1); cell.setCellValue("Hello World"); cell.setCellStyle( cellStyle );

Merging cells in Excel using Apache POI - Dustbiin

Merge cells in a row or merge cells in column - Create a workbook and sheet in the workbook- XSSFWorkbook workBook = new XSSFWorkbook (); Sheet sheet = workBook.createSheet("My sheet"); sheet . addMergedRegion ( new CellRangeAddress ( startRowIndx ,   endRowIndx ,   startColIndx ,   endColIndx )); try (FileOutputStream outputStream = new FileOutputStream( "demo.xlsx" )) {     workbook.write( outputStream ); } finally {     workbook.close(); } Make sure the CellRangeAddress does not coincide with other merged regions as that will throw an exception. If you want to merge cells one above another, keep column indexes same If you want to merge cells which are in a single row, keep the row indexes same Indexes are zero-based, it means row and column start from zero. Start index of ...

Add multiple sheets in an excel file in JAVA by using Apache POI. - Dustbiin

Adding multiple sheets in excel file- Create new workbook first- XSSFWorkbook workBook = new XSSFWorkbook (); Add sheets in the workbook Sheet sheet1 = workbook.createSheet("Sheet name 1"); Sheet sheet2 = workbook.createSheet("Sheet name 2"); Sheet sheet3 = workbook.createSheet("Sheet name 3"); Sheet sheet4 = workbook.createSheet("Sheet name 4"); Sheet sheet5 = workbook.createSheet("Sheet name 5");     try (FileOutputStream outputStream = new FileOutputStream( "demo.xlsx" )) {     workbook.write( outputStream ); } finally {     workbook.close(); } Output -  Output with five sheet int excel file  

Create Excel (.xlsx) file in JAVA by Apache Poi - dustbiin

Create an excel file in java by apache poi easy and it is a very powerful API for excel creation. Step-1: Create a maven project (ExcelGenerator) and Import apache poi dependency in pom.xml <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> <dependency>     <groupId>org.apache.poi</groupId>     <artifactId>poi</artifactId>     <version>4.1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> <dependency>     <groupId>org.apache.poi</groupId>     <artifactId>poi-ooxml</artifactId>     <version>4.1.2</version> </dependency>     Go to maven package site Step-2: Create a class to generate an excel file package com.demo.client; import java.io.FileNotFoundException; import java.io.FileOutputStream; import ...