import
java.io.File;
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
GFG {
public
static
void
main(String[] args)
throws
Exception
{
XSSFWorkbook workbook =
new
XSSFWorkbook();
XSSFSheet spreadsheet
= workbook.createSheet(
"Sheet1"
);
XSSFRow row = spreadsheet.createRow((
short
)
1
);
row.setHeight((
short
)
800
);
XSSFCell cell = (XSSFCell)row.createCell((
short
)
1
);
cell.setCellValue(
"Merged cells"
);
spreadsheet.addMergedRegion(
new
CellRangeAddress(
1
,
1
,
1
,
4
));
row = spreadsheet.createRow(
5
);
cell = (XSSFCell)row.createCell(
0
);
row.setHeight((
short
)
800
);
XSSFCellStyle style1 = workbook.createCellStyle();
spreadsheet.setColumnWidth(
0
,
8000
);
style1.setAlignment(XSSFCellStyle.ALIGN_LEFT);
style1.setVerticalAlignment(
XSSFCellStyle.VERTICAL_TOP);
cell.setCellValue(
"Hi, I'm top left indent"
);
cell.setCellStyle(style1);
row = spreadsheet.createRow(
6
);
cell = (XSSFCell)row.createCell(
1
);
row.setHeight((
short
)
800
);
XSSFCellStyle style2 = workbook.createCellStyle();
style2.setAlignment(XSSFCellStyle.ALIGN_CENTER);
style2.setVerticalAlignment(
XSSFCellStyle.VERTICAL_CENTER);
cell.setCellValue(
"I'm Center Aligned indent"
);
cell.setCellStyle(style2);
row = spreadsheet.createRow(
7
);
cell = (XSSFCell)row.createCell(
2
);
row.setHeight((
short
)
800
);
XSSFCellStyle style3 = workbook.createCellStyle();
style3.setAlignment(XSSFCellStyle.ALIGN_RIGHT);
style3.setVerticalAlignment(
XSSFCellStyle.VERTICAL_BOTTOM);
cell.setCellValue(
"I'm Bottom Right indent"
);
cell.setCellStyle(style3);
row = spreadsheet.createRow(
8
);
cell = (XSSFCell)row.createCell(
3
);
XSSFCellStyle style4 = workbook.createCellStyle();
style4.setAlignment(XSSFCellStyle.ALIGN_JUSTIFY);
style4.setVerticalAlignment(
XSSFCellStyle.VERTICAL_JUSTIFY);
cell.setCellValue(
"I'm Justify indent nice to meet you"
);
cell.setCellStyle(style4);
row = spreadsheet.createRow((
short
)
10
);
row.setHeight((
short
)
800
);
cell = (XSSFCell)row.createCell((
short
)
1
);
cell.setCellValue(
"BORDER"
);
XSSFCellStyle style5 = workbook.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.Black.getIndex());
cell.setCellStyle(style5);
row = spreadsheet.createRow((
short
)
10
);
cell = (XSSFCell)row.createCell((
short
)
1
);
XSSFCellStyle style6 = workbook.createCellStyle();
style6.setFillBackgroundColor(HSSFColor.BLUE.index);
style6.setFillPattern(
XSSFCellStyle.FILL_HORIZONTAL_CROSS_HATCH);
style6.setAlignment(XSSFCellStyle.ALIGN_FILL);
spreadsheet.setColumnWidth(
1
,
8000
);
cell.setCellValue(
"FILL HORIZONTAL CROSS HATCH"
);
cell.setCellStyle(style6);
row = spreadsheet.createRow((
short
)
12
);
cell = (XSSFCell)row.createCell((
short
)
1
);
XSSFCellStyle style7 = workbook.createCellStyle();
style7.setFillForegroundColor(
HSSFColor.GREEN.index);
style7.setFillPattern(
XSSFCellStyle.THIN_VERTICAL_STRIPE);
style7.setAlignment(XSSFCellStyle.ALIGN_FILL);
cell.setCellValue(
"THIN VERTICAL STRIPE"
);
cell.setCellStyle(style7);
FileOutputStream out =
new
FileOutputStream(
new
File(
"C:/poiexcel/stlingcells.xlsx"
));
workbook.write(out);
out.close();
System.out.println(
"gfg.xlsx success"
);
}
}