Setting filter on headers of an Excel sheet via POI

Save the first and last cell from filter area, and execute: sheet.setAutoFilter(new CellRangeAddress(firstCell.getRow(), lastCell.getRow(), firstCell.getCol(), lastCell.getCol())); For example, from the below sheet. >x (x, y) 0123456 0|–hhh–| h = header 1|–+++–| + = values 2|–+++–| – = empty fields 3|–+++–| 4|——-| first cell will be the header above the first + (2,1) cell. The last … Read more

How can I convert POI HSSFWorkbook to bytes?

As that mailing list post said Invoking HSSFWorkbook.getBytes() does not return all of the data necessary to re- construct a complete Excel file. You can use the write method with a ByteArrayOutputStream to get at the byte array. ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { workbook.write(bos); } finally { bos.close(); } byte[] bytes = bos.toByteArray(); … Read more

How to insert a row between two rows in an existing excel with HSSF (Apache POI)

Helper function to copy rows shamelessly adapted from here import org.apache.poi.hssf.usermodel.*; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.util.CellRangeAddress; import java.io.FileInputStream; import java.io.FileOutputStream; public class RowCopy { public static void main(String[] args) throws Exception{ HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(“c:/input.xls”)); HSSFSheet sheet = workbook.getSheet(“Sheet1”); copyRow(workbook, sheet, 0, 1); FileOutputStream out = new FileOutputStream(“c:/output.xls”); workbook.write(out); out.close(); } private static void … Read more

How to apply bold text style for an entire row using Apache POI?

This should work fine. Workbook wb = new XSSFWorkbook(“myWorkbook.xlsx”); Row row=sheet.getRow(0); CellStyle style=null; XSSFFont defaultFont= wb.createFont(); defaultFont.setFontHeightInPoints((short)10); defaultFont.setFontName(“Arial”); defaultFont.setColor(IndexedColors.BLACK.getIndex()); defaultFont.setBold(false); defaultFont.setItalic(false); XSSFFont font= wb.createFont(); font.setFontHeightInPoints((short)10); font.setFontName(“Arial”); font.setColor(IndexedColors.WHITE.getIndex()); font.setBold(true); font.setItalic(false); style=row.getRowStyle(); style.setFillBackgroundColor(IndexedColors.DARK_BLUE.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setAlignment(CellStyle.ALIGN_CENTER); style.setFont(font); If you do not create defaultFont all your workbook will be using the other one as default.

tech