Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge and align center cell using apache poi

I want to export data to excel using Apache poi.
Now the problem that I am facing is that I am unable to merge rows and align them in the center.

Code for export data is:

List<LinkedHashMap<String,Object>> lstReportHeader = null; HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet();  //Set Header Font HSSFFont headerFont = wb.createFont(); headerFont.setBoldweight(headerFont.BOLDWEIGHT_BOLD); headerFont.setFontHeightInPoints((short) 12);  //Set Header Style CellStyle headerStyle = wb.createCellStyle(); headerStyle.setFillBackgroundColor(IndexedColors.BLACK.getIndex()); headerStyle.setAlignment(headerStyle.ALIGN_CENTER); headerStyle.setFont(headerFont); headerStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM); int rowCount= 0; Row header; header = sheet.createRow(0);//its for header  Cell cell ;//= header.createCell(0); for(int j = 0;j < 4; j++) {     cell = header.createCell(j);     if(j == 0) {         cell.setCellValue("ItemWise List");     }     cell.setCellStyle(headerStyle); } sheet.addMergedRegion(new CellRangeAddress(rowCount, rowCount, 0, lstReportFormHeader.size()-1)); header = sheet.createRow(0);         cell = header.createCell(0); cell.setCellValue("Sr. No");         cell = header.createCell(1); cell.setCellValue("Item Name");         cell = header.createCell(2); cell.setCellValue("Qty");         cell = header.createCell(3); cell.setCellValue("Rate"); 

Now I want to ItemWise List merge and make it align center.

like image 694
shrey Avatar asked Nov 20 '13 10:11

shrey


People also ask

How do I center align with Apache POI in Excel?

Use CellUtil. setAlignment(cell, HorizontalAlignment. CENTER); , the method setAlignment(Cell cell, Workbook workbook, short align) is deprecated now from 3.15-beta2.

How do I merge cells in Apache POI?

Firstly, we can use four zero-based indexes to define the top-left cell location and the bottom-right cell location: sheet = // existing Sheet setup int firstRow = 0; int lastRow = 0; int firstCol = 0; int lastCol = 2 sheet. addMergedRegion(new CellRangeAddress(firstRow, lastRow, firstCol, lastCol));


2 Answers

My solution was to merge the cells by their positions, then created a cell (reference to the first block of the merged cells) to assign a value and then set the alignment throught the CellUtil

// Merges the cells CellRangeAddress cellRangeAddress = new CellRangeAddress(start, start, j, j + 1); sheet.addMergedRegion(cellRangeAddress);  // Creates the cell Cell cell = CellUtil.createCell(row, j, entry.getKey());  // Sets the allignment to the created cell CellUtil.setAlignment(cell, workbook, CellStyle.ALIGN_CENTER); 
like image 156
pedromendessk Avatar answered Oct 11 '22 00:10

pedromendessk


Merge like:::

 Workbook wb = new HSSFWorkbook(); Sheet sheet = wb.createSheet("new sheet");  Row row = sheet.createRow((short) 1); Cell cell = row.createCell((short) 1); cell.setCellValue("This is a test of merging");  sheet.addMergedRegion(new CellRangeAddress(         1, //first row (0-based)         1, //last row  (0-based)         1, //first column (0-based)         2  //last column  (0-based) ));  // Write the output to a file FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close(); 

For aligning also check the below official link of Apache poi:::

http://poi.apache.org/spreadsheet/quick-guide.html#Alignment

like image 26
dev Avatar answered Oct 10 '22 23:10

dev