จากโคดตัวอย่างเราจะสร้างไฟล์ .xlsx ดังนั้น ผู้เขียนจึงใช้ Package XSSF แต่หากใครจะสร้างไฟล์ .xls เฉยๆให้เรียกใช้ Package HSSF นะครับ
ตัวอย่างโคด
package com.java.poi.excel;
import java.io.FileOutputStream;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class FontStyleExcel {
public static void main(String[] args) {
try {
// สร้าง object ของ excel
XSSFWorkbook wb = new XSSFWorkbook();
// สร้าง sheet
XSSFSheet spreadsheet = wb.createSheet("Font Excel");
// สร้างแถวแรก การนับแถวเริ่มจาก 0,1,2....
XSSFRow rowColor = spreadsheet.createRow(1);
XSSFFont fontColor = wb.createFont();
XSSFCellStyle styleColor = wb.createCellStyle();
// ใส่สี
fontColor.setColor(HSSFColor.DARK_RED.index);
styleColor.setFont(fontColor);
XSSFCell cell = rowColor.createCell(1);
cell.setCellValue("Font Color");
cell.setCellStyle(styleColor);
// กำหนดขนาด text
XSSFRow rowSize = spreadsheet.createRow(2);
XSSFFont fontSize = wb.createFont();
XSSFCellStyle styleSize = wb.createCellStyle();
fontSize.setFontHeightInPoints((short) 20);
styleSize = wb.createCellStyle();
styleSize.setFont(fontSize);
cell = rowSize.createCell(1);
cell.setCellValue("Font Size");
cell.setCellStyle(styleSize);
// ตัวหนา
XSSFRow rowBold = spreadsheet.createRow(3);
XSSFFont fontBold = wb.createFont();
XSSFCellStyle styleBold = wb.createCellStyle();
fontBold.setBold(true);
styleBold = wb.createCellStyle();
styleBold.setFont(fontBold);
cell = rowBold.createCell(1);
cell.setCellValue("Font Bold");
cell.setCellStyle(styleBold);
// ตัวเอียง
XSSFRow rowItalic = spreadsheet.createRow(4);
XSSFFont fontItalic = wb.createFont();
XSSFCellStyle styleItalic = wb.createCellStyle();
fontItalic.setItalic(true);
styleItalic = wb.createCellStyle();
styleItalic.setFont(fontItalic);
cell = rowItalic.createCell(1);
cell.setCellValue("Font Italic");
cell.setCellStyle(styleItalic);
// ตัวหนาและอียง
XSSFRow rowBoldItalic = spreadsheet.createRow(5);
XSSFFont fontBoldItalic = wb.createFont();
XSSFCellStyle styleBoldItalic = wb.createCellStyle();
fontBoldItalic.setBold(true);
fontBoldItalic.setItalic(true);
styleBoldItalic = wb.createCellStyle();
styleBoldItalic.setFont(fontBoldItalic);
cell = rowBoldItalic.createCell(1);
cell.setCellValue("Font Bold And Italic");
cell.setCellStyle(styleBoldItalic);
// ตัวตัด
XSSFRow rowStrikeout = spreadsheet.createRow(6);
XSSFFont fontStrikeout = wb.createFont();
XSSFCellStyle styleStrikeout = wb.createCellStyle();
fontStrikeout.setStrikeout(true);
styleStrikeout = wb.createCellStyle();
styleStrikeout.setFont(fontStrikeout);
cell = rowStrikeout.createCell(1);
cell.setCellValue("Font Strikeout");
cell.setCellStyle(styleStrikeout);
// ขีดเส้นใต้หนึ่งเส้น
XSSFRow rowUnderline = spreadsheet.createRow(7);
XSSFFont fontUnderline = wb.createFont();
XSSFCellStyle styleUnderline = wb.createCellStyle();
fontUnderline.setUnderline(XSSFFont.U_SINGLE);
styleUnderline = wb.createCellStyle();
styleUnderline.setFont(fontUnderline);
cell = rowUnderline.createCell(1);
cell.setCellValue("Font Single Underline");
cell.setCellStyle(styleUnderline);
// ขีดเส้นใต้สองเส้น
XSSFRow rowUnderline2 = spreadsheet.createRow(8);
XSSFFont fontUnderline2 = wb.createFont();
XSSFCellStyle styleUnderline2 = wb.createCellStyle();
fontUnderline2.setUnderline(XSSFFont.U_DOUBLE);
styleUnderline2 = wb.createCellStyle();
styleUnderline2.setFont(fontUnderline2);
cell = rowUnderline2.createCell(1);
cell.setCellValue("Font Double Underline");
cell.setCellStyle(styleUnderline2);
// กำหนด font Impact
XSSFRow rowImpact = spreadsheet.createRow(9);
XSSFFont fontImpact = wb.createFont();
XSSFCellStyle styleImpact = wb.createCellStyle();
fontImpact.setFontName("Impact");
styleImpact.setFont(fontImpact);
cell = rowImpact.createCell(1);
cell.setCellValue("Font Impact");
cell.setCellStyle(styleImpact);
// กำหนด font Arial
XSSFFont fontArial = wb.createFont();
XSSFCellStyle styleArial = wb.createCellStyle();
fontArial.setFontName("Arial");
styleArial.setFont(fontArial);
cell = rowImpact.createCell(2);
cell.setCellValue("Font Arial");
cell.setCellStyle(styleArial);
// กำหนด font Tahoma
XSSFFont fontTahoma = wb.createFont();
XSSFCellStyle styleTahoma = wb.createCellStyle();
fontTahoma.setFontName("Tahoma");
styleTahoma.setFont(fontTahoma);
cell = rowImpact.createCell(3);
cell.setCellValue("Font Tahoma");
cell.setCellStyle(styleTahoma);
// กำหนด font Kunstler Script
XSSFFont fontKunstlerScript = wb.createFont();
XSSFCellStyle styleKunstlerScript = wb.createCellStyle();
fontKunstlerScript.setFontName("Kunstler Script");
styleKunstlerScript.setFont(fontKunstlerScript);
cell = rowImpact.createCell(4);
cell.setCellValue("Font KunstlerScript");
cell.setCellStyle(styleKunstlerScript);
//หมุน 120 องศา
XSSFRow rowRotation = spreadsheet.createRow(10);
XSSFCellStyle styleRotation = wb.createCellStyle();
styleRotation.setRotation((short) 120);
cell = rowRotation.createCell(1);
cell.setCellValue("120D angle");
cell.setCellStyle(styleRotation);
// path ของไฟล์
FileOutputStream out = new FileOutputStream("C:\\poi\\FontStyleExcel.xlsx");
wb.write(out);
wb.close();
out.close();
System.out.println("Excel created successfully");
} catch (Exception e) {
e.printStackTrace();
}
}
}
ไฟล์ที่ถูกสร้าง
ผลลัพธ์ที่ได้


0 comments:
Post a Comment