Tuesday, 26 February 2013

Format cell style in Excel document? Java Export Excel

package org.xxx.example.poi;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;

import java.io.FileOutputStream;
import java.io.File;
import java.io.IOException;

public class ExcelCellFormat {
    public static void main(String[] args) {
        //
        // Create an instance of workbook and sheet
        //
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();

        //
        // Create an instance of HSSFCellStyle which will be use to format the
        // cell. Here we define the cell top and bottom border and we also
        // define the background color.
        //
        HSSFCellStyle style = workbook.createCellStyle();
        style.setBorderTop((short) 6); // double lines border
        style.setBorderBottom((short) 1); // single line border
        style.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);

        //
        // We also define the font that we are going to use for displaying the
        // data of the cell. We set the font to ARIAL with 20pt in size and
        // make it BOLD and give blue as the color.
        //
        HSSFFont font = workbook.createFont();
        font.setFontName(HSSFFont.FONT_ARIAL);
        font.setFontHeightInPoints((short) 20);
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        font.setColor(HSSFColor.BLUE.index);
        style.setFont(font);

        //
        // We create a simple cell, set its value and apply the cell style.
        //
        HSSFRow row = sheet.createRow(1);
        HSSFCell cell = row.createCell(1);
        cell.setCellValue(new HSSFRichTextString("Hi there... It's me again!"));
        cell.setCellStyle(style);      
        sheet.autoSizeColumn((short) 1);

        //
        // Finally we write out the workbook into an excel file.
        //
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(new File("ExcelDemo.xls"));
            workbook.write(fos);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.flush();
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Export Excel from JAVA using POI

use POI jar for the same

package xx;

import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
public class ExportToExcel {

    public HSSFWorkbook createExcelFile() {
        return new HSSFWorkbook();
    }

    public HSSFSheet creatExcelSheet(String sheetName, HSSFWorkbook wb) {
        return wb.createSheet(sheetName);
    }

    public HSSFWorkbook createExcelForImport(String header[], List data, String sheetname) {

        HSSFWorkbook wb = createExcelFile();
        HSSFSheet hSSFSheet = creatExcelSheet(sheetname, wb);
        //set header
        HSSFRow row = hSSFSheet.createRow((short) 0);
        for (int i = 0; i < header.length; i++) {
            String headerTitle = header[i];
            HSSFCell cell = row.createCell((short) i);
            HSSFCellStyle style = wb.createCellStyle();
            style.setFillBackgroundColor(HSSFColor.DARK_YELLOW.index);
            HSSFFont font = wb.createFont();
//            font.setFontHeightInPoints((short) 20);
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            style.setFont(font);
            cell.setCellStyle(style);
            cell.setCellValue(headerTitle);
        }
        //setData
        int counter = 0;
        int sheetCounter = 1;
        for (int i = 0; i < data.size(); i++) {
            counter++;
            if (counter == 32766) {
                hSSFSheet = creatExcelSheet(sheetname + sheetCounter++, wb);
                counter = 0;
            }
            row = hSSFSheet.createRow((short) counter);
            hSSFSheet.autoSizeColumn((short) 1);
            Object[] importData = (Object[]) data.get(i);
            for (int j = 0; j < importData.length; j++) {
                Object object = importData[j];
                HSSFCell cell = row.createCell((short) j);
                cell.setCellValue(object != null ? object.toString() : "");

            }
        }
        return wb;
    }
}

Sunday, 17 February 2013

Android Development Book

Data Communication & Networking Ebooks PDF Download




Data Communication & Networking
Behrouz Forouzan


(Most Recommended)




--------------------------------------------------------

Understanding Data Communications

Gilbert Held

--------------------------------------------------------

Data and Computer Communications 

William Stallings

Download Now

--------------------------------------------------------

Wednesday, 6 February 2013

Writing to a PDF file from JAVA

Use iText JAR for this

package ....

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;

import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class GeneratePDF {

    public static void main(String[] args) {
        try {
            OutputStream file = new FileOutputStream(new File("D:\\Test.pdf"));

            Document document = new Document();
            PdfWriter.getInstance(document, file);

            document.open();
            document.add(new Paragraph("Hello World, iText"));
            document.add(new Paragraph(new Date().toString()));

            document.close();
            file.close();

        } catch (Exception e) {

            e.printStackTrace();
        }
    }
}

This link is important:

http://tutorials.jenkov.com/java-itext/table.html#width

Monday, 14 January 2013

Why Public Static Void Main ? - Java Code

We are so much habitual now to begin our code with public static void main(). Because we were taught to do so. Being taught that this is the entry point for our java program to execute, however are we aware what does it actually means??
Plenty of other questions which came into my mind:
1. Why public static void main()
2. Whats the meaning of each one of the individual keyword?
3. What if I don't use it?
4. Is there any other way to start the program execution?
5. How does JVM knows, "i need to start from here..."?
6. Can we shuffle the keywords?
  and so on....

Well answering to each one of the above, lets begin.

We structure our complete code into classes and objects(instance of those classes), that's what java says to do. Now we write a program say xyz, create a public class for it XYZ and define a public static void main(String args[]) method in the class.

The first thing to know is public static void main () aka psvm() is a method signature with an Array of String type as an argument. The method signature is
[access modifier] [return type] method_name (Argument_List)

Secondly, its a must to use this signature because JVM looks for this signature to begin execution. If it is not able to find the above signature an exception is thrown. Note this is the only way to begin the program execution and nothing else would work.

public is an access modifier, which tells compiler that any class or object can access this method.
or simply, Access is granted to anybody. Making the main method public is necessary so that anyone (object) can access the method.

static.. Please remember in java we need an object reference to call any class method. I mean instantiating the class. Now do we have any object before beginning the program. No we don't. So how we are going to begin with. Static keyword before a field or method describes that no instance or object reference is required to access that field/mehtod().

void.. Simply means no return by the method
Note : there's a difference between null returned and void.

main.. As we all know, method name defined by java.
main is different from Main as java is case sensitive. Further main() is pre-defined mehtod where as you can create your own method Main() / Main(x) or anything.