Tampilkan postingan dengan label poi. Tampilkan semua postingan
Tampilkan postingan dengan label poi. Tampilkan semua postingan

Senin, 10 Desember 2007

jakarta poi

jakarta poi adalah lib java yang digunakan untuk manipulasi data ke Microsoft's OLE 2 Compound Document format menggunakan java murni ,alias tanpa embel2 dll ,atau component lain bener2 pure java ... keterangan lebih lanjut silahkan baca : http://poi.apache.org

untuk menggunakanya pertama-tama download libnya .. di http://www.apache.org/dyn/closer.cgi/poi/

sedangakan kodingnya sbb:(Dalam hal ini memanipulasi data ke format excel) Java to Excel


package org.poi.sampel.action;

import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.Action;
import com.bedaplus.sis.ejb.sessionbean.SisMasterSessionHome;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import javax.ejb.CreateException;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javax.naming.NamingException;
import org.apache.poi.hssf.util.Region;
import java.rmi.RemoteException;
import org.apache.poi.hssf.util.HSSFColor;
import javax.servlet.ServletOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import java.util.ArrayList;
import javax.naming.InitialContext;
import javax.naming.Context;
import java.util.List;

import javax.rmi.PortableRemoteObject;
import java.io.FileOutputStream;

//lib untuk memakai poi
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;

public class excelCtkMurid
extends Action {
public ActionForward execute(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest servletRequest,
HttpServletResponse servletResponse) {
HSSFWorkbook wb = new HSSFWorkbook();
FileOutputStream fileOut = null;
FileInputStream fileIn = null;
SisMasterSession ejbSession = null;


System.out.println("HSSF => Create Workbook");
try {
HSSFSheet sheet = wb.createSheet("new Data Murid");
//Border
HSSFCellStyle style = wb.createCellStyle();
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setLeftBorderColor(HSSFColor.BLACK.index);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setRightBorderColor(HSSFColor.BLACK.index);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setTopBorderColor(HSSFColor.BLACK.index);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

HSSFCellStyle style2 = wb.createCellStyle();
style2.setBorderBottom(HSSFCellStyle.BORDER_NONE);
style2.setBottomBorderColor(HSSFColor.BLACK.index);
style2.setBorderLeft(HSSFCellStyle.BORDER_NONE);
style2.setLeftBorderColor(HSSFColor.BLACK.index);
style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
style2.setRightBorderColor(HSSFColor.BLACK.index);
style2.setBorderTop(HSSFCellStyle.BORDER_NONE);
style2.setTopBorderColor(HSSFColor.BLACK.index);

HSSFCellStyle styleH = wb.createCellStyle();
styleH.setBorderBottom(HSSFCellStyle.BORDER_DOUBLE);
styleH.setBottomBorderColor(HSSFColor.BLACK.index);
styleH.setBorderLeft(HSSFCellStyle.BORDER_THIN);
styleH.setLeftBorderColor(HSSFColor.BLACK.index);
styleH.setBorderRight(HSSFCellStyle.BORDER_THIN);
styleH.setRightBorderColor(HSSFColor.BLACK.index);
styleH.setBorderTop(HSSFCellStyle.BORDER_DOUBLE);
styleH.setTopBorderColor(HSSFColor.BLACK.index);
styleH.setWrapText(true);
styleH.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
styleH.setAlignment(HSSFCellStyle.ALIGN_CENTER);

HSSFCellStyle styleR = wb.createCellStyle();
styleR.setBorderBottom(HSSFCellStyle.BORDER_DOTTED);
styleR.setBottomBorderColor(HSSFColor.BLACK.index);
styleR.setBorderLeft(HSSFCellStyle.BORDER_THIN);
styleR.setLeftBorderColor(HSSFColor.BLACK.index);
styleR.setBorderRight(HSSFCellStyle.BORDER_THIN);
styleR.setRightBorderColor(HSSFColor.BLACK.index);
styleR.setBorderTop(HSSFCellStyle.BORDER_DOTTED);
styleR.setTopBorderColor(HSSFColor.BLACK.index);



HSSFCellStyle style3 = wb.createCellStyle();
style3.setAlignment(HSSFCellStyle.ALIGN_LEFT);
style3.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

HSSFCellStyle style4 = wb.createCellStyle();
style4.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style4.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 2));
sheet.addMergedRegion(new Region(1, (short) 0, 1, (short) 2));
sheet.addMergedRegion(new Region(2, (short) 0, 2, (short) 2));
sheet.addMergedRegion(new Region(0, (short) 4, 0, (short) 7));
sheet.addMergedRegion(new Region(1, (short) 4, 1, (short) 7));
sheet.addMergedRegion(new Region(2, (short) 4, 2, (short) 7));

drawCell( (short) 0, (short) 0, sheet, "Nama Sekolah ", false, style3);
drawCell( (short) 0, (short) 3, sheet, ": SMP Islam Al-Azhar 12 Rawamangun", false, style3);
drawCell( (short) 0, (short) 4, sheet, "DAFTAR NAMA MURID", false, style4);
drawCell( (short) 0, (short) 8, sheet, "Format", false, style4);
drawCell( (short) 0, (short) 9, sheet, ": 8355", false, style4);
drawCell( (short) 1, (short) 0, sheet, "Alamat", false, style3);
drawCell( (short) 1, (short) 3, sheet, ": Jalan Raya Sekali", false, style3);
drawCell( (short) 1, (short) 4, sheet, " ", false, style4);
drawCell( (short) 1, (short) 8, sheet, "Kelas ", false, style4);
drawCell( (short) 2, (short) 0, sheet, "Telepon", false, style3);
drawCell( (short) 2, (short) 3, sheet, ": 47868234", false, style3);
drawCell( (short) 2, (short) 4, sheet, "TAHUN PELAJARAN 2007/2008", false, style4);

sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 2));
sheet.addMergedRegion(new Region(1, (short) 0, 1, (short) 2));
sheet.addMergedRegion(new Region(2, (short) 0, 2, (short) 2));
sheet.addMergedRegion(new Region(0, (short) 4, 0, (short) 7));
sheet.addMergedRegion(new Region(1, (short) 4, 1, (short) 7));
sheet.addMergedRegion(new Region(2, (short) 4, 2, (short) 7));


drawCell( (short) 4, (short) 0, sheet, "No.", true, styleH);
drawCell( (short) 4, (short) 1, sheet, "Kelas", true, styleH);
drawCell( (short) 4, (short) 2, sheet, " Nomer Induk ", true, styleH);
drawCell( (short) 4, (short) 3, sheet, " Nama Murid ", true,
styleH);
drawCell( (short) 4, (short) 4, sheet, "L/P", true,
styleH);
drawCell( (short) 4, (short) 5, sheet, " Tempat Tanggal Lahir ", true,
styleH);
drawCell( (short) 4, (short) 6, sheet, "Agama", true, styleH);
drawCell( (short) 4, (short) 7, sheet, " Nama Orang Tua ", true, styleH);
drawCell( (short) 4, (short) 8, sheet, " Alamat ", true, styleH);
drawCell( (short) 4, (short) 9, sheet, " No. dan Tahun STTB ", true,
styleH);





}
fileOut = new FileOutputStream("workbook.xls");
//servletResponse.setHeader();
wb.write(fileOut);
fileOut.close();
}

catch (FileNotFoundException ex) {
System.out.print("No File Found");
}
catch (IOException ex) {
/** @todo Handle this exception */
}

finally {
}

return actionMapping.findForward("success");
//return null;

}
private void drawCell(short x, short y, HSSFSheet sheet, String value,
boolean autosize, HSSFCellStyle style) {
HSSFRow row = sheet.createRow( (short) x);
HSSFCell cell = row.createCell( (short) y);
cell.setCellValue(new HSSFRichTextString(value));
cell.setCellStyle(style);
if (autosize) {
sheet.autoSizeColumn( (short) y);
}

}

private void drawCell(short x, short y, HSSFSheet sheet, String value,
boolean autosize) {
HSSFRow row = sheet.createRow( (short) x);
HSSFCell cell = row.createCell( (short) y);
cell.setCellValue(new HSSFRichTextString(value));
if (autosize) {
sheet.autoSizeColumn( (short) y);
}
}
}


demkian dan terima kasih ,
mohon maklum , masih newbie banget ...(kalo ada yang salah dengan metode ini,atau ada cara yang lebih bagus )
saya cuman pengen berbagi dan belajar sedikit menulis ..