2

I have a jsp page populated with tables from a data base. I have to throw the data of this table in excel file. Also I have to customize the excel file like border colors, fonts merging cell cell colors etc along with the data. How can I do it? Is there any java script for this work? or are there any open source tools for this task?

I have found one like dataTables jquery package but it did not customize the excel file only throws data to excel. or do I have to use java libraries??

2
  • 1
    Do it inside Java, not using Javascript. Jun 1, 2012 at 12:41
  • @nischal You could do it with JavaScript, but only works in Internet Explorer. Jun 2, 2012 at 4:31

4 Answers 4

3

You can use Apache POI for that.

For Reference -

public class xyzExcelExportView extends AbstractExcelView {

    private static final xyzService xyzService = ApplicationContextProvider.getxyzServiceImpl();

    @SuppressWarnings({ "unchecked" })
    protected void buildExcelDocument(Map model, HSSFWorkbook workbook,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        CurrencyService currencyService = ApplicationContextProvider.getCurrencyService();
        Currency currency = null;
        //set Response
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "Attachment;Filename=\""+"ExcelExport"+"."+"xls" + "\"");
        // Create Sheet.
        HSSFSheet sheet = workbook.createSheet("Software Contract Database");
        // Create Style for sheet.
        HSSFCellStyle headerStyle = workbook.createCellStyle();
        HSSFCellStyle dateStyle = workbook.createCellStyle();       
        HSSFCellStyle datePatternStyle = workbook.createCellStyle();
        HSSFDataFormat format1 = workbook.createDataFormat();
        HSSFCellStyle dataStyle = workbook.createCellStyle();
        HSSFCellStyle numberStyle = workbook.createCellStyle();     
        dateStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
        dataStyle.setWrapText(true);
        datePatternStyle.setDataFormat(HSSFDataFormat
                .getBuiltinFormat("d-mmm-yy"));
        numberStyle.setDataFormat(format1.getFormat("#,##0"));
        // Font setting for sheet.
        HSSFFont font = workbook.createFont();
        HSSFPalette palette = workbook.getCustomPalette();
        palette.setColorAtIndex(HSSFColor.LAVENDER.index,
                (byte) 204,  
                (byte) 204,    
                (byte) 255);
        font.setBoldweight((short) 700);
        sheet.setDefaultColumnWidth((short) 30);
        List<Type> dataList = // YOur Data list     
        int currentRow = 0;
        // WRITE ROW FOR HEADER
        HSSFCell header = null;
        for (short i = 0; i < getHeader().size(); i++) {
            header = getCell(sheet, currentRow, i);
            headerStyle.setFillForegroundColor(HSSFColor.LAVENDER.index);
            headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
            headerStyle.setFont(font);
            header.setCellStyle(headerStyle);
            setText(header, getxyzHeader().get(i));
        }       

        HSSFRow row = null;
        HSSFCell cellNumber = null;
        HSSFCell cellString = null;
        HSSFCell cellNumberWitnComma = null;
        HSSFCell cellDate = null;

        for(Type xyz : dataList){
                currentRow++;
                short i = 0;
                row = sheet.createRow(currentRow);
                cellNumber = row.createCell(i);
                cellNumber = fillxyzExcelExportCell(xyz.getId() == null ? 0 : xyz.getId(), 1 , cellNumber);
                cellNumber.setCellStyle(dataStyle);

                cellString = row.createCell(++i);
                cellString = fillxyzExcelExportCell(xyz.getStatus() == null ? "" : xyz.getStatus() , 3 , cellString);   
                cellString.setCellStyle(dataStyle); 

                cellString = row.createCell(++i);
                cellString = fillxyzExcelExportCell(xyz.getxyzStatus() , 3 , cellString);   
                cellString.setCellStyle(dataStyle);

                cellString = row.createCell(++i);
                cellString = fillxyzExcelExportCell(xyz.getName() == null ? "" : xyz.getName(), 3 , cellString);
                cellString.setCellStyle(dataStyle);     
            }
        }   
        workbook.write(response.getOutputStream());
    }
0
2

Apache POI would be well suited to this task - it's a Java library and has very good excel support. It's free and open source.

1

JExcelApi is the open source library that allows you to read ,write and customize the excel sheets. It might accomplish your requirement.

0

The following answers may be helpful:

You can also export an HTML file to Excel by changing the Content-Type and Content-Disposition in your JSP. Since Excel 2003, you can open HTML files. My favorite way is Apache POI for XLS or XLSX.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.