Display percentage values in Excel using POI API

You need to: Set your data as number (floating-point), not as text. Specify cell format as percentage. Something like: cell.setCellValue(0.123); // set value as number CellStyle style = workbook.createCellStyle(); style.setDataFormat(workbook.createDataFormat().getFormat(“0.000%”)); cell.setCellStyle(style); Take a look at user defined formats section of POI quick guide for more details. You may also want to go through the examples … Read more

How to determine empty row?

I’m using the following method in my POI project and it’s working well. It is a variation of zeller’s solution. public static boolean isRowEmpty(Row row) { for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) { Cell cell = row.getCell(c); if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK) return false; } return true; }

Writing a large resultset to an Excel file using POI

Using SXSSF poi 3.8 package example; import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.util.CellReference; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class SXSSFexample { public static void main(String[] args) throws Throwable { FileInputStream inputStream = new FileInputStream(“mytemplate.xlsx”); XSSFWorkbook wb_template = new XSSFWorkbook(inputStream); inputStream.close(); SXSSFWorkbook wb = new SXSSFWorkbook(wb_template); wb.setCompressTempFiles(true); SXSSFSheet sh = (SXSSFSheet) … Read more

Apache POI XSSFColor from hex code

The good news is, if you are using XSSF, as opposed to HSSF, then the solution to your problem is fairly easy. You simply have to cast your style variable to XSSFCellStyle. If you do, then there is a version of setFillForegroundColor that takes an XSSFColor argument, so you need not call getIndexed(). Here is … Read more

get number of columns of a particular row in given excel using Java

There are two Things you can do use int noOfColumns = sh.getRow(0).getPhysicalNumberOfCells(); or int noOfColumns = sh.getRow(0).getLastCellNum(); There is a fine difference between them Option 1 gives the no of columns which are actually filled with contents(If the 2nd column of 10 columns is not filled you will get 9) Option 2 just gives you … Read more

tech