Excel Writer (JAVA)


package writer;

import java.io.File;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class ExcelWriter
{
                public void writeExcel(String filepath,String values[][]) throws Exception
                {
                                File file = new File(filepath);
                                WritableWorkbook writableWorkbook =null;
                                if(!file.exists())
                                {
                                                writableWorkbook = Workbook.createWorkbook(file);
                                }
                                else
                                                writableWorkbook = Workbook.createWorkbook(file,Workbook.getWorkbook(new File(filepath)));
                                int noOfSheets = writableWorkbook.getNumberOfSheets();
                                writableWorkbook.createSheet("Sheet"+noOfSheets,noOfSheets);
                                WritableSheet writableSheet = writableWorkbook.getSheet(noOfSheets);
                                WritableCellFormat headingCellFormat = getHeadingCellFormat();
                                WritableCellFormat contentCellFormat = getContentCellFormat();
                                int curcol = writableSheet.getColumns();
                                int currow = writableSheet.getRows();
                                for(int j=0;j<values[0].length;j++)
                                                if(curcol<=values[0].length)
                                                                writeCell(writableSheet,headingCellFormat,j,0,values[0][j]);
                                if(currow==0)
                                                currow++;
                                for(int i=1;i<values.length;i++)
                                                for(int j=0;j<values[0].length;j++)
                                                                writeCell(writableSheet,contentCellFormat,j,currow+i-1,values[i][j]);
                                writableWorkbook.write();
                                writableWorkbook.close();
                }

                private WritableCellFormat getHeadingCellFormat() throws Exception
                {
                                WritableCellFormat wcf = new WritableCellFormat(new WritableFont(WritableFont.TIMES,10,
                                                                                                                                                                                                                WritableFont.BOLD,false,UnderlineStyle.SINGLE,Colour.BLACK));
                                wcf.setWrap(true);
                                wcf.setAlignment(Alignment.CENTRE);
                                return wcf;
                }

                private WritableCellFormat getContentCellFormat() throws Exception
                {
                                WritableCellFormat wcf = new WritableCellFormat(new WritableFont(WritableFont.TIMES,10,
                                                                WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.BLACK));
                                wcf.setWrap(true);
                                return wcf;
                }

                private void writeCell(WritableSheet ws,WritableCellFormat wcf,int col,int row,String str)throws Exception
                {
                                Label label = new Label(col,row,str,wcf);
                                ws.addCell(label);
                }

                public static void main(String[] args)
                {
                                String[][] values = {{"Name","Age","Gender"},{"Ram","24","M"},{"Shyam","36","M",}};
                                ExcelWriter excelWriter = new ExcelWriter();
                                try
                                {
                                                excelWriter.writeExcel("e:\\FileToWrite.xls",values);
                                }
                                catch (Exception e)
                                {
                                                System.err.println("Error Occuered: " + e.getMessage());
                                }
                }
}

Comments

Popular posts from this blog

Index MySQL datadase table in Solr

Hibernate Sharding Example

Shallow Copy