Monday, May 28, 2012

How to create Dynamic table using AWS?


  String tableName = "my-favorite-movies-table";

            // Create a table with a primary key named 'name', which holds a string
            CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName)
                .withKeySchema(new KeySchema(new KeySchemaElement().withAttributeName("name").withAttributeType("S")))
                .withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(10L).withWriteCapacityUnits(10L));
            TableDescription createdTableDescription = dynamoDB.createTable(createTableRequest).getTableDescription();
            System.out.println("Created Table: " + createdTableDescription);







Wednesday, May 16, 2012

How to create xls file from java code...

Step 1:- You need one jar file which is jxl.jar.

Step 2:-

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;


import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;



public class CreateExcelFile{

    /**
     * @param args
     * @throws IOException
     * @throws WriteException,Exception
     */
    public static void main(String[] args) throws Exception {
        try{
        WorkbookSettings ws = new WorkbookSettings();
        ws.setLocale(new Locale("en",
                "EN"));
        String fileLocation = createDirectory("c:\\", "test");
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Calendar fromCalender = new GregorianCalendar();
     
        fromCalender.add(Calendar.DATE, -7);

        String toDate = formatter.format(new Date());
        String fromDate = formatter.format(fromCalender.getTime());
       
        StringBuilder fileName = new StringBuilder();
        fileName.append("IRC Downloads ").append(fromDate).append(" to ").append(toDate).append(".xls");
       
        WritableWorkbook workbook = Workbook.createWorkbook(new File(fileLocation +"/" + fileName.toString()), ws);
        WritableSheet sheet = workbook.createSheet(
                "Sheet 1", 0);
       
        writeDataSheetinfo(writeDataSheetColoumnHeaders(sheet), "test");
        workbook.write();
        workbook.close();
        }catch(Exception ex){
            ex.printStackTrace();
        }

    }
    public static String createDirectory(String archiveLocation, String catalog) {
       
        String fileLocation = null;
        Date today = Calendar.getInstance().getTime();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String formatDate = formatter.format(today);
        fileLocation = archiveLocation + catalog + "\\" + formatDate;
        try {
       
            File file = new File(fileLocation);
            file.mkdirs();
        } catch (Exception e) {
       
       
        }

       
        return fileLocation;
    }

    public static WritableSheet writeDataSheetColoumnHeaders(WritableSheet sheet) {
        final WritableFont wf = new WritableFont(WritableFont.ARIAL, 10,
                WritableFont.BOLD);
        final String header[] = { "Customer Name", "Username", "Customer SMS",
                "Email Address", "US", "EMA", "CA", "Total",
                "Across 3 Source Group", "IP Address", "Location" };

        final WritableCellFormat wcf1 = new WritableCellFormat(wf);
        int size = header.length;
        for (int i = 0; i < size; i++) {
            final String value = header[i];
            final Label label = new Label(i, 1, value, wcf1);
            try {
                sheet.addCell(label);
            } catch (WriteException we) {
               
            } catch (Exception e) {
               
            }
        }
        return sheet;
    }
   
    public static void writeDataSheetinfo(WritableSheet sheet, String catalog) {
       
       

        final WritableFont wf1 = new WritableFont(WritableFont.ARIAL, 10,WritableFont.NO_BOLD);
        final WritableCellFormat wcf2 = new WritableCellFormat(wf1);
        int size = 1;
            try {
                for (int i = 0; i < size; i++) {
       
                    String customerName = "Rahul";
                    final Label lbl1 = new Label(0, i + 2, customerName, wcf2);
                    sheet.addCell(lbl1);
   
                    String userName = "Rahul1851";
                    final Label lbl2 = new Label(1, i + 2, userName, wcf2);
                    sheet.addCell(lbl2);
   
                    Integer smsId = 1234;
                    final Label lbl3 = new Label(2, i + 2, String.valueOf(smsId), wcf2);
                    sheet.addCell(lbl3);
                   
                    String emailAddress = "rahul1851@gmail.com";
                    sheet.addCell(new Label(3, i + 2, emailAddress, wcf2));
   
                    Integer us = 1236547;
                    final Label lbl5 = new Label(4, i + 2, String.valueOf(us), wcf2);
                    sheet.addCell(lbl5);
   
                   
                    Integer total = 78954624;
                    final Label lbl8 = new Label(7, i + 2, String.valueOf(total), wcf2);
                    sheet.addCell(lbl8);
                   
                    String across3sg = "45646546";
                    final Label lbl9 = new Label(8, i + 2, across3sg, wcf2);
                    sheet.addCell(lbl9);
                   
                    String ipAddress = "10.97.42.102";
                    final Label lbl10 = new Label(9, i + 2, ipAddress, wcf2);
                    sheet.addCell(lbl10);
                   
                    String ipLocation = "India";
                    final Label lbl11 = new Label(10, i + 2, ipLocation, wcf2);
                    sheet.addCell(lbl11);
                }
            } catch (WriteException we) {
               
            }
             catch (Exception e) {
               
            }
       
       
    }
   
}