Created
April 24, 2021 08:49
-
-
Save Vinodh-thimmisetty/70ab29b7a34d9c28056727b5832b5f30 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.poi.common.usermodel.HyperlinkType; | |
import org.apache.poi.common.usermodel.fonts.FontFamily; | |
import org.apache.poi.ss.usermodel.BorderExtent; | |
import org.apache.poi.ss.usermodel.BorderStyle; | |
import org.apache.poi.ss.usermodel.FillPatternType; | |
import org.apache.poi.ss.usermodel.Font; | |
import org.apache.poi.ss.usermodel.Footer; | |
import org.apache.poi.ss.usermodel.Header; | |
import org.apache.poi.ss.usermodel.HorizontalAlignment; | |
import org.apache.poi.ss.usermodel.IndexedColors; | |
import org.apache.poi.ss.usermodel.Workbook; | |
import org.apache.poi.ss.util.CellRangeAddress; | |
import org.apache.poi.ss.util.PropertyTemplate; | |
import org.apache.poi.util.IOUtils; | |
import org.apache.poi.xssf.usermodel.XSSFCell; | |
import org.apache.poi.xssf.usermodel.XSSFCellStyle; | |
import org.apache.poi.xssf.usermodel.XSSFClientAnchor; | |
import org.apache.poi.xssf.usermodel.XSSFCreationHelper; | |
import org.apache.poi.xssf.usermodel.XSSFDrawing; | |
import org.apache.poi.xssf.usermodel.XSSFFont; | |
import org.apache.poi.xssf.usermodel.XSSFHyperlink; | |
import org.apache.poi.xssf.usermodel.XSSFPicture; | |
import org.apache.poi.xssf.usermodel.XSSFRow; | |
import org.apache.poi.xssf.usermodel.XSSFSheet; | |
import org.apache.poi.xssf.usermodel.XSSFWorkbook; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.OutputStream; | |
import java.time.LocalDate; | |
public class ApachePOIExcelWriter { | |
public static void main(String[] args) { | |
try (final XSSFWorkbook wb = new XSSFWorkbook(); | |
final OutputStream outputStream = new FileOutputStream(".......file.xlsx")) { | |
final XSSFCreationHelper helper = wb.getCreationHelper(); | |
final XSSFSheet sheet = wb.createSheet("Sheet"); | |
// available while printing the Excel | |
final Header header = sheet.getHeader(); | |
header.setLeft("Header Left"); | |
header.setCenter("Header Center"); | |
header.setRight("Header Right"); | |
final Footer footer = sheet.getFooter(); | |
footer.setLeft("Footer Left"); | |
footer.setCenter("Footer Center"); | |
footer.setRight("Footer Right"); | |
// custom column size | |
sheet.setColumnWidth(2, 10 * 256); | |
sheet.setColumnWidth(4, 20 * 256); | |
final XSSFRow introRow = sheet.createRow(0); | |
final XSSFCell todayDate = introRow.createCell(2); | |
final XSSFCellStyle dateStyle = wb.createCellStyle(); | |
dateStyle.setDataFormat(helper.createDataFormat().getFormat("yyyy-mm-dd")); | |
dateStyle.setAlignment(HorizontalAlignment.LEFT); | |
dateStyle.setFillForegroundColor(IndexedColors.AQUA.getIndex()); | |
dateStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); | |
todayDate.setCellStyle(dateStyle); | |
todayDate.setCellValue(LocalDate.now()); | |
final XSSFFont font = wb.createFont(); | |
font.setBold(true); | |
font.setColor(IndexedColors.RED.getIndex()); | |
font.setItalic(true); | |
font.setFontName(FontFamily.FF_SWISS.name()); | |
final XSSFCellStyle titleStyle = wb.createCellStyle(); | |
titleStyle.setFont(font); | |
titleStyle.setAlignment(HorizontalAlignment.CENTER); | |
final XSSFCell title = introRow.createCell(4); | |
title.setCellValue("Page Title"); | |
title.setCellStyle(titleStyle); | |
final XSSFRow detailsRow = sheet.createRow(1); | |
final XSSFCell contactNo = detailsRow.createCell(2); | |
contactNo.setCellValue("Contact Info : XXX - XXX - XXXX"); | |
final XSSFCell companyName = detailsRow.createCell(10); | |
companyName.setCellValue("Main Address"); | |
companyName.setCellStyle(dateStyle); | |
// Insert IMAGE | |
final FileInputStream fis = new FileInputStream("...............vinodh_photo.jpg"); | |
final byte[] bytes = IOUtils.toByteArray(fis); | |
final int pictureIndex = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG); | |
final XSSFDrawing drawingPatriarch = sheet.createDrawingPatriarch(); | |
final XSSFClientAnchor clientAnchor = helper.createClientAnchor(); | |
clientAnchor.setCol1(10); | |
clientAnchor.setRow1(1); | |
final XSSFPicture picture = drawingPatriarch.createPicture(clientAnchor, pictureIndex); | |
picture.resize(); | |
// merge multiple rows*cells | |
sheet.addMergedRegion(new CellRangeAddress(1, 10, 10, 10)); | |
final XSSFRow additionalDetails = sheet.createRow(2); | |
final XSSFCell email = additionalDetails.createCell(2); | |
email.setCellValue("Email : vinodh5052@gmail.com"); | |
// Include Hyperlinks | |
final XSSFRow webDetails = sheet.createRow(3); | |
final XSSFCell website = webDetails.createCell(2); | |
final XSSFCellStyle hLinks = wb.createCellStyle(); | |
final XSSFHyperlink hyperlink = helper.createHyperlink(HyperlinkType.URL); | |
hyperlink.setAddress("https://www.github.com"); | |
final XSSFFont hFont = wb.createFont(); | |
hFont.setUnderline(Font.U_SINGLE); | |
hFont.setColor(IndexedColors.BLUE.getIndex()); | |
hLinks.setFont(hFont); | |
website.setCellValue("Github Home Page"); | |
website.setHyperlink(hyperlink); | |
website.setCellStyle(hLinks); | |
// Borders | |
PropertyTemplate pt = new PropertyTemplate(); | |
pt.drawBorders(new CellRangeAddress(1, 3, 2, 2), | |
BorderStyle.MEDIUM, | |
IndexedColors.GREEN.getIndex(), | |
BorderExtent.OUTSIDE); | |
pt.drawBorders(new CellRangeAddress(1, 10, 10, 10), | |
BorderStyle.MEDIUM, | |
IndexedColors.RED.getIndex(), | |
BorderExtent.OUTSIDE); | |
pt.applyBorders(sheet); | |
// Group Panel - which is available in Left side of excel | |
sheet.groupRow(1, 4); | |
wb.write(outputStream); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment