Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Generate Excel file using Apache POI in java

    • 0
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 1.26k
    Comment on it

    Hello Guys
    Bellow code will help you to generate excel file in java.
    Herebelow I have used apache poi jar file version: poi3.8.jar .
    You can download jar file from given bellow link
    http://poi.apache.org/download.html

    Now Create ExcelExample.java and put bellow code

    import java.io.File;
    import java.io.FileOutputStream;
    import java.util.HashMap;
    import java.util.Map;
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.CellStyle;
    import org.apache.poi.ss.usermodel.Font;
    import org.apache.poi.ss.usermodel.IndexedColors;
    import org.apache.poi.ss.usermodel.PrintSetup;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.ss.util.CellRangeAddress;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    import com.sun.xml.internal.ws.client.RequestContext;
    
    public class ExcelExample {
    
    
        public static void main(String ars[]) throws Exception {
    
            String[] titles = {"User Name", "Case Number", "Assigned on", "Last Modified" };
    
            Workbook wb;
    
            wb = new XSSFWorkbook();
    
            Map<String, CellStyle> styles = createStyles(wb);
    
            Sheet sheet = wb.createSheet("Report-Task By User");
            PrintSetup printSetup = sheet.getPrintSetup();
            printSetup.setLandscape(true);
            sheet.setFitToPage(true);
            sheet.setHorizontallyCenter(true);
    
            //title row
            Row titleRow = sheet.createRow(0);
            titleRow.setHeightInPoints(45);
            Cell titleCell = titleRow.createCell(0);
            titleCell.setCellValue("Report-Task By User");
            titleCell.setCellStyle(styles.get("title"));
            sheet.addMergedRegion(CellRangeAddress.valueOf("$A$1:$D$1"));
    
            //header row
            Row headerRow = sheet.createRow(1);
            headerRow.setHeightInPoints(40);
            Cell headerCell;
            for (int i = 0; i < titles.length; i++) 
            {
                headerCell = headerRow.createCell(i);
                headerCell.setCellValue(titles[i]);
                headerCell.setCellStyle(styles.get("header"));
                     //finally set column widths, the width is measured in units of 1/256th of a character width
                sheet.setColumnWidth(i, 30*256);
            }
    
           Cell dataCell;
    
           for(int i=2;i<10;i++)
           {
    
             Row dataRow = sheet.createRow(i);
    
             dataCell = dataRow.createCell(0);
             dataCell.setCellValue("Bhagwan"+i);
             dataCell.setCellStyle(styles.get("cell"));
    
             dataCell = dataRow.createCell(1);
             dataCell.setCellValue("123"+1);
             dataCell.setCellStyle(styles.get("cell"));
    
             dataCell = dataRow.createCell(2);
             dataCell.setCellValue(i+"/01/2015");
             dataCell.setCellStyle(styles.get("cell"));
    
             dataCell = dataRow.createCell(3);
             dataCell.setCellValue(i+"/01/2014");
             dataCell.setCellStyle(styles.get("cell"));
    
           }
    
            // Write the output to a file
    
            String file ="excelsheet.xls";
    
    
            if(wb instanceof XSSFWorkbook) file += "x";
            FileOutputStream out = new FileOutputStream(file);
            wb.write(out);
            out.close();
            System.out.print("success..........................");
            return file;
        }
    
        /**
         * Create a library of cell styles
         */
        private Map<String, CellStyle> createStyles(Workbook wb){
            Map<String, CellStyle> styles = new HashMap<String, CellStyle>();
            CellStyle style;
            Font titleFont = wb.createFont();
            titleFont.setFontHeightInPoints((short)18);
            titleFont.setBoldweight(Font.BOLDWEIGHT&#95;BOLD);
            style = wb.createCellStyle();
            style.setAlignment(CellStyle.ALIGN&#95;CENTER);
            style.setVerticalAlignment(CellStyle.VERTICAL&#95;CENTER);
            style.setFont(titleFont);
            styles.put("title", style);
    
            Font monthFont = wb.createFont();
            monthFont.setFontHeightInPoints((short)11);
            monthFont.setColor(IndexedColors.WHITE.getIndex());
            style = wb.createCellStyle();
            style.setAlignment(CellStyle.ALIGN&#95;CENTER);
            style.setVerticalAlignment(CellStyle.VERTICAL&#95;CENTER);
            style.setFillForegroundColor(IndexedColors.BLUE.getIndex());
            style.setFillPattern(CellStyle.SOLID&#95;FOREGROUND);
            style.setFont(monthFont);
            style.setWrapText(true);
            styles.put("header", style);
    
            style = wb.createCellStyle();
            style.setAlignment(CellStyle.ALIGN&#95;CENTER);
            style.setWrapText(true);
            style.setBorderRight(CellStyle.BORDER&#95;THIN);
            style.setRightBorderColor(IndexedColors.BLACK.getIndex());
            style.setBorderLeft(CellStyle.BORDER&#95;THIN);
            style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
            style.setBorderTop(CellStyle.BORDER&#95;THIN);
            style.setTopBorderColor(IndexedColors.BLACK.getIndex());
            style.setBorderBottom(CellStyle.BORDER&#95;THIN);
            style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
            styles.put("cell", style);
    
    
            return styles;
        }
    
    
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: