ต่อไปเรามาดูประเภทของ Cell กันก่อนครับ ซึ่งประเภทของ Cell ใน Excel มีดังนี้
- Blank (ค่าว่าง) : XSSFCell.CELL_TYPE_BLANK
- Boolean (จริงเท็จ) : XSSFCell.TYPE_BOOLEAN
- Error : XSSFCell.CELL_TYPE_ERROR
- Numeric (ตัวเลข) : XSSFCell.CELL_TYPE_NUMERIC
- String (ตัวหนังสือ) : XSSFCell.CELL_TYPE_STRING
ตัวอย่างโคด
package com.java.poi.excel;
import java.io.FileOutputStream;
import java.util.Date;
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 TypeOfCellExcel {
public static void main(String[] args) {
try {
// สร้าง object ของ excel
XSSFWorkbook wb = new XSSFWorkbook();
// สร้าง sheet
XSSFSheet spreadsheet = wb.createSheet("Type Of Cell");
// สร้างแถวแรก การนับแถวเริ่มจาก 0,1,2....
XSSFRow row = spreadsheet.createRow((short) 0);
row.createCell(0).setCellValue("Type of Cell");
row.createCell(1).setCellValue("cell value");
row = spreadsheet.createRow((short) 1);
row.createCell(0).setCellValue("set cell type BLANK");
row.createCell(1);
row = spreadsheet.createRow((short) 2);
row.createCell(0).setCellValue("set cell type BOOLEAN");
row.createCell(1).setCellValue(true);
row = spreadsheet.createRow((short) 3);
row.createCell(0).setCellValue("set cell type ERROR");
row.createCell(1).setCellValue(XSSFCell.CELL_TYPE_ERROR );
row = spreadsheet.createRow((short) 4);
row.createCell(0).setCellValue("set cell type date");
row.createCell(1).setCellValue(new Date());
row = spreadsheet.createRow((short) 5);
row.createCell(0).setCellValue("set cell type numeric" );
row.createCell(1).setCellValue(20 );
row = spreadsheet.createRow((short) 6);
row.createCell(0).setCellValue("set cell type string");
row.createCell(1).setCellValue("A String");
// path ของไฟล์
FileOutputStream out = new FileOutputStream("C:\\poi\\TypeOfCellExcel.xlsx");
wb.write(out);
wb.close();
out.close();
System.out.println("Excel created successfully");
} catch (Exception e) {
e.printStackTrace();
}
}
}
ไฟล์ที่ถูกสร้าง
ผลลัพธ์ที่ได้
หลังจากรู้ประเภทของ Cell เราลองมาดูการจัด Style ต่างๆให้ Cell กันบ้างครับ
ตัวอย่างโคด
package com.java.poi.excel;
import java.io.FileOutputStream;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class CellStyleExcel {
public static void main(String[] args) {
try {
// สร้าง object ของ excel
XSSFWorkbook wb = new XSSFWorkbook();
// สร้าง sheet
XSSFSheet spreadsheet = wb.createSheet("Cell Style");
// สร้างแถวแรก การนับแถวเริ่มจาก 0,1,2....
XSSFRow row = spreadsheet.createRow((short) 1);
row.setHeight((short) 800);
XSSFCell cell = (XSSFCell) row.createCell((short) 1);
cell.setCellValue("test of merging");
//การรวม cell
spreadsheet.addMergedRegion(new CellRangeAddress(
1, //แถวแรก (0-based)
1, //แถวสุดท้าย (0-based)
1, //column แรก (0-based)
4 //column สุดท้าย (0-based)
));
// การจัดตำแหน่ง
row = spreadsheet.createRow(3);
cell = (XSSFCell) row.createCell(0);
row.setHeight((short) 800);
// วางตำแหน่งไว้ซ้ายบน
XSSFCellStyle style1 = wb.createCellStyle();
spreadsheet.setColumnWidth(0, 8000);
style1.setAlignment(XSSFCellStyle.ALIGN_LEFT);
style1.setVerticalAlignment(XSSFCellStyle.VERTICAL_TOP);
cell.setCellValue("Top Left");
cell.setCellStyle(style1);
row = spreadsheet.createRow(4);
cell = (XSSFCell) row.createCell(1);
row.setHeight((short) 800);
// วางตำแหน่งไว้ตรงกลาง
XSSFCellStyle style2 = wb.createCellStyle();
style2.setAlignment(XSSFCellStyle.ALIGN_CENTER);
style2.setVerticalAlignment(
XSSFCellStyle.VERTICAL_CENTER);
cell.setCellValue("Center Aligned");
cell.setCellStyle(style2);
row = spreadsheet.createRow(5);
cell = (XSSFCell) row.createCell(2);
row.setHeight((short) 800);
// วางตำแหน่งไว้ล่างขวา
XSSFCellStyle style3 = wb.createCellStyle();
style3.setAlignment(XSSFCellStyle.ALIGN_RIGHT);
style3.setVerticalAlignment(
XSSFCellStyle.VERTICAL_BOTTOM);
cell.setCellValue("Bottom Right");
cell.setCellStyle(style3);
row = spreadsheet.createRow(6);
cell = (XSSFCell) row.createCell(3);
// วางตำแหน่ง Justified
XSSFCellStyle style4 = wb.createCellStyle();
style4.setAlignment(XSSFCellStyle.ALIGN_JUSTIFY);
style4.setVerticalAlignment(
XSSFCellStyle.VERTICAL_JUSTIFY);
cell.setCellValue("Contents are Justified in Alignment");
cell.setCellStyle(style4);
//เส้นขอบ
row = spreadsheet.createRow((short) 8);
row.setHeight((short) 800);
cell = (XSSFCell) row.createCell((short) 1);
cell.setCellValue("BORDER");
XSSFCellStyle style5 = wb.createCellStyle();
style5.setBorderBottom(XSSFCellStyle.BORDER_THICK);
style5.setBottomBorderColor(
IndexedColors.BLUE.getIndex());
style5.setBorderLeft(XSSFCellStyle.BORDER_DOUBLE);
style5.setLeftBorderColor(
IndexedColors.GREEN.getIndex());
style5.setBorderRight(XSSFCellStyle.BORDER_HAIR);
style5.setRightBorderColor(
IndexedColors.RED.getIndex());
style5.setBorderTop(XSSFCellStyle.BIG_SPOTS);
style5.setTopBorderColor(
IndexedColors.CORAL.getIndex());
cell.setCellStyle(style5);
// การใส่สีพื้นหลัง
row = spreadsheet.createRow((short) 10 );
cell = (XSSFCell) row.createCell((short) 1);
XSSFCellStyle style6 = wb.createCellStyle();
style6.setFillBackgroundColor(
HSSFColor.LEMON_CHIFFON.index );
style6.setFillPattern(XSSFCellStyle.LESS_DOTS);
style6.setAlignment(XSSFCellStyle.ALIGN_FILL);
spreadsheet.setColumnWidth(1,8000);
cell.setCellValue("FILL BACKGROUNG/FILL PATTERN");
cell.setCellStyle(style6);
//ใส่สี Foreground
row = spreadsheet.createRow((short) 12);
cell = (XSSFCell) row.createCell((short) 1);
XSSFCellStyle style7=wb.createCellStyle();
style7.setFillForegroundColor(HSSFColor.BLUE.index);
style7.setFillPattern( XSSFCellStyle.LESS_DOTS);
style7.setAlignment(XSSFCellStyle.ALIGN_FILL);
cell.setCellValue("FILL FOREGROUND/FILL PATTERN");
cell.setCellStyle(style7);
// path ของไฟล์
FileOutputStream out = new FileOutputStream("C:\\poi\\CellStyleExcel.xlsx");
wb.write(out);
wb.close();
out.close();
System.out.println("Excel created successfully");
} catch (Exception e) {
e.printStackTrace();
}
}
}
ไฟล์ที่ถูกสร้าง
ผลลัพธ์ที่ได้




ขอบคุณสำหรับบทความดีดีครับ
ReplyDelete