การสร้าง excel ไฟล์ ด้วย JExcelApi (Java Programming)

JExcelApi เป็น java library ที่เอางานเกี่ยวกับ M$ Excel โดยสามารถที่จะ อ่าน, เขียน และทำการแก้ไข
M$ Excel ได้. โดย JExcelApi ได้รับการพัฒนามาจากนาย
Andy Khan's เมื่อ March 2002 ตอนนี้ได้สร้างมาถึง Version 2.6.3 (February 2007) ท่านสามารถอ่านรายละเอียดเพิ่มเติม และ Download JExcelApi ได้ที่ http://www.jexcelapi.org

ตัวอย่างการ อ่าน, เขียน และทำการแก้ไข M$ Excel
import java.io.*;
import jxl.*;
import java.util.*;
import jxl.Workbook;
import jxl.write.DateFormat;
import jxl.write.Number;
import jxl.write.*;


class create
{
public static void main(String[] args)
{
try
{
String filename = "Test.xls";
WorkbookSettings ws = new WorkbookSettings();
ws.setLocale(new Locale("en", "EN"));
WritableWorkbook workbook =
Workbook.createWorkbook(new File(filename), ws);
WritableSheet s = workbook.createSheet("Sheet1", 0);
WritableSheet s1 = workbook.createSheet("Sheet1", 0);
writeDataSheet(s);
writeImageSheet(s1);
workbook.write();
workbook.close();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (WriteException e)
{
e.printStackTrace();
}
}

private static void writeDataSheet(WritableSheet s)
throws WriteException
{

/* กำหนดรูปแบบของตัวอักษร */
WritableFont wf = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD); // ตัวอักษรชนิด ARIAL ขนาด 10 และมีขนาดเป็นตัวหนา
WritableCellFormat cf = new WritableCellFormat(wf);
cf.setWrap(true);

/* ทำการสร้าง Label และทำการเขียนชื่อของ Label */
Label l = new Label(0,0,"Date",cf);
s.addCell(l);
WritableCellFormat cf1 = new WritableCellFormat(DateFormats.FORMAT9);

DateTime dt = new DateTime(0,1,new Date(), cf1, DateTime.GMT);

s.adCell(dt);

l = new Label(2,0,"Float", cf);
s.addCell(l);
WritableCellFormat cf2 = new WritableCellFormat(NumberFormats.FLOAT);
Number n = new Number(2,1,3.1415926535,cf2);
s.addCell(n);

n = new Number(2,2,-3.1415926535, cf2);
s.addCell(n);

/* ทำการสร้าง Label และเขียนข้อมูลชนิด float number ที่มีจุดทศนิยมจำนวน 3 ตำแหน่ง */
l = new Label(3,0,"3dps",cf);
s.addCell(l);
NumberFormat dp3 = new NumberFormat("#.###");
WritableCellFormat dp3cell = new WritableCellFormat(dp3);
n = new Number(3,1,3.1415926535,dp3cell);
s.addCell(n);

/* ทำการเขียนข้อมูลลงไปใน cell จำนวน 2 cell */
l = new Label(4, 0, "Add 2 cells",cf);
s.addCell(l);
n = new Number(4,1,10);
s.addCell(n);
n = new Number(4,2,16);
s.addCell(n);
Formula f = new Formula(4,3, "E1+E2");
s.addCell(f);

/* ทำการเขียนข้อมูลลงไปใน cell โดยใช้สูตรการคำนวน */
l = new Label(5,0, "Multipy by 2",cf);
s.addCell(l);
n = new Number(5,1,10);
s.addCell(n);
f = new Formula(5,2, "F1 * 3");
s.addCell(f);

/* ทำการเขียนข้อมูลลงไปใน cell โดยใช้สูตรการคำนวน */
l = new Label(6,0, "Divide",cf);
s.addCell(l);
n = new Number(6,1, 12);
s.addCell(n);
f = new Formula(6,2, "F1/2.5");
s.addCell(f);
}

private static void writeImageSheet(WritableSheet s)
throws WriteException
{
/* ทำการ insert รูปภาพเข้าไปใน cell */
Label l = new Label(0, 0, "Image");
s.addCell(l);
WritableImage wi = new WritableImage(0, 3, 5, 7, new File("image.png"));
s.addImage(wi);

/* ทำการเขียน HYPERLINK ไปใน cell */
l = new Label(0,15, "HYPERLINK");
s.addCell(l);
Formula f = new Formula(1, 15,
"HYPERLINK(\"http://kingmot.blogspot.com/\", "+ "\"Kingmot Blog\")​");
s.addCell(f);

}
}

Comments