- การสร้างตัว excel object ของ XSSFWorkbook
- การสร้าง sheet XSSFSheet ใน XSSFWorkbook
- การสร้างแถว XSSFROW
- การสร้าง XSSFCell ต่างๆในแถวนั้นๆและใส่ข้อมูลให้ Cell ครับ
ตัวอย่างโคด
package com.java.poi.excel;
import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class HelloExcel {
public static void main(String[] args) {
try {
// สร้าง object ของ excel
XSSFWorkbook wb = new XSSFWorkbook();
// สร้าง sheet
XSSFSheet sheet = wb.createSheet("Hello Sheet");
// สร้างแถวแรก การนับแถวเริ่มจาก 0,1,2....
XSSFRow row = sheet.createRow((short)0);
// สร้าง cell แรกแล้วใส่ค่าลงไป การนับcellเริ่มจาก 0,1,2....
XSSFCell cell = row.createCell(0);
cell.setCellValue("Hello Excel.");
// path ของไฟล์
FileOutputStream out = new FileOutputStream("C:\\poi\\HelloExcel.xlsx");
wb.write(out);
wb.close();
out.close();
System.out.println("Excel created successfully");
} catch (Exception e) {
e.printStackTrace();
}
}
}
ไฟล์ที่ถูกสร้าง
ผลลัพธ์ที่ได้



0 comments:
Post a Comment