package com.jasamedika.medifirst2000.dao.test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.apache.commons.lang.time.DateUtils;
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.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.util.CellRangeAddress;

public class Testing {
	public static void main(String[] args) throws IOException {// Create
		// blank
		// workbook
		HSSFWorkbook workbook = new HSSFWorkbook();
		// Create a blank sheet
		HSSFSheet spreadsheet = workbook.createSheet(" Employee Info ");
		// Create row object
		HSSFRow row;
		// This data needs to be written (Object[])
		Map<String, Object[]> empinfo = new TreeMap<String, Object[]>();

		CellStyle style = workbook.createCellStyle();
		style.setFillForegroundColor(IndexedColors.AQUA.getIndex());
		style.setFillPattern(CellStyle.SOLID_FOREGROUND);
		Font font = workbook.createFont();
		font.setColor(IndexedColors.BLACK.getIndex());
		font.setBold(true);
		style.setFont(font);
		style.setWrapText(true);
		
		CellStyle styleIsi = workbook.createCellStyle();
		Font fontIsi = workbook.createFont();
		fontIsi.setColor(IndexedColors.BLACK.getIndex());
		fontIsi.setBold(false);
		fontIsi.setFontName("Arial");
		fontIsi.setFontHeightInPoints((short)8);
		styleIsi.setFont(fontIsi);
		styleIsi.setWrapText(true);

		empinfo.put("01", new Object[] { "", "LAMPIRAN SURAT PENAWARAN HARGA OBAT TAHUN 2016 ", "", "" });
		empinfo.put("02", new Object[] { "", "TAHUN 2016", "", "" });
		empinfo.put("03", new Object[] { "", "EMP ID", "EMP NAME", "DESIGNATION" });
		empinfo.put("04", new Object[] { "", "tp01", "Gopal", "Technical Manager" });
		empinfo.put("05", new Object[] { "", "tp02", "Manisha", "Proof Reader" });
		empinfo.put("06", new Object[] { "", "tp03", "Masthan", "Technical Writer" });
		empinfo.put("07", new Object[] { "", "tp04", "Satish", "Technical Writer" });
		empinfo.put("08", new Object[] { "", "tp05", "Krishna", "Technical Writer" });
		empinfo.put("09", new Object[] { "", "tp06", "Krishna", "Technical Writer" });
		empinfo.put("10", new Object[] { "", "tp07", "Krishna", "Technical Writer" });
		empinfo.put("11", new Object[] { "", "tp08", "Krishna", "Technical Writer" });
		empinfo.put("12", new Object[] { "", "tp09", "Krishna", "Technical Writer" });
		empinfo.put("13", new Object[] { "", "tp10", "Krishna", "Technical Writer" });
		empinfo.put("14", new Object[] { "", "tp11", "Krishna", "Technical Writer" });
		empinfo.put("15", new Object[] { "", "tp12", "Krishna", "Technical Writer" });
		empinfo.put("16", new Object[] { "", "tp13", "Krishna", "Technical Writer" });
		
		// Iterate over data and write to sheet
		Set<String> keyid = empinfo.keySet();
		int rowid = 0;
		for (String key : keyid) {
			row = spreadsheet.createRow(rowid++);
			Object[] objectArr = empinfo.get(key);
			int cellid = 0;
			for (Object obj : objectArr) {
			
				Cell cell = row.createCell(cellid++);
				if(key.equalsIgnoreCase("03") && cellid!=1){
					System.out.println(cellid);
					cell.setCellStyle(style);
					
				}else{
					cell.setCellStyle(styleIsi);
				}
				cell.setCellValue((String) obj);
			}
		}
		spreadsheet.setColumnWidth(0, 1000);
		spreadsheet.setColumnWidth(1, 7500);
		spreadsheet.setColumnWidth(2, 7500);
		spreadsheet.setColumnWidth(3, 7500);
		spreadsheet.addMergedRegion(new CellRangeAddress(0, 0, 1, 4));
		// Write the workbook in file system
		FileOutputStream out = new FileOutputStream(new File("d:/Writesheet.xls"));
		workbook.write(out);
		out.close();
		System.out.println("d:/Writesheet.xlsx written successfully");

	}
}
