Auto size height for rows in Apache POI
currentRow.setHeight((short)-1) Works for XSSFCell and Excel 2013
currentRow.setHeight((short)-1) Works for XSSFCell and Excel 2013
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
If you want to get all cells, no matter if they exist or not, then the iterator isn’t for you. Instead, you need to manually fetch the appropriate cells, likely with a missing cell policy for(Row row : sheet) { for(int cn=0; cn<row.getLastCellNum(); cn++) { // If the cell is missing from the file, generate … Read more
The HSSFCell object has methods .setCellType and .setCellFormula which you need to call like this: // “cell” object previously created or looked up String strFormula= “SUM(A1:A10)”; cell.setCellType(HSSFCell.CELL_TYPE_FORMULA); cell.setCellFormula(strFormula);
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; }
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
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
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
I got this to work. I had to set the foreground color to make the background color work (??). So I changed: cellStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index); to: cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); and it worked!
If you’re using Apache POI 4.x, you can do that with: Cell c = row.getCell(3); if (c == null || c.getCellType() == CellType.Blank) { // This cell is empty } For older Apache POI 3.x versions, which predate the move to the CellType enum, it’s: Cell c = row.getCell(3); if (c == null || c.getCellType() … Read more