skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Monday, September 17, 2012

Format a cell in Excel Sheet

Posted by Admin at 11:46 AM – 0 comments
 


public DateTest(String filename) throws IOException { 
        DataFormat format = null; 
        CellStyle cellStyle = null; 
        Workbook workbook = null; 
        Sheet sheet = null; 
        Row row = null; 
        Cell cell = null; 
        FileOutputStream f = null; 
        BufferedOutputStream b = null; 
        StringBuilder refersToFormula = null; 
        if(filename.endsWith(".xlsx")) { 
            workbook = new XSSFWorkbook(); 
        } else { 
            workbook = new HSSFWorkbook(); 
        } 
        try { 
            
            sheet = workbook.createSheet("Test"); 
            
            row = sheet.createRow(0); 
            cell = row.createCell(0); 
            cellStyle = workbook.createCellStyle(); 
            format = workbook.createDataFormat(); 
            cellStyle.setDataFormat(format.getFormat("[$-809]dd mmmm yyyy;@")); 
            cell.setCellStyle(cellStyle); 
            cell.setCellValue(new Date()); 
            
            f = new FileOutputStream(filename); 
            b = new BufferedOutputStream(f); 
            
            workbook.write(b); 
        } 
        finally { 
            if(b != null) { 
                b.close(); 
            } 
        } 
    }



Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger