package Excel_create_style;
import org.apache.poi.*;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.PatternFormatting;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class Test2 {
public static void main(String[] args)
{
//Workbook workbook = new XSSFWorkbook();
Workbook workbook = new HSSFWorkbook();
Sheet sheet1 = workbook.createSheet("sheet1");
Sheet sheet2 = workbook.createSheet("sheet 2");
HSSFRow row;
Row header = (HSSFRow) sheet1.createRow(0);
sheet1.addMergedRegion(new CellRangeAddress(0,0,1,7)); //--Merge
header.createCell(1).setCellValue("Output Structure"); //
//Heder style
Font font_h = workbook.createFont();
font_h.setBold(true);
font_h.setFontHeightInPoints((short)12);
CellStyle Styleheader = workbook.createCellStyle();
Styleheader.setAlignment(HorizontalAlignment.CENTER);
Styleheader.setFont(font_h);
Styleheader.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
Styleheader.setFillPattern(FillPatternType.SOLID_FOREGROUND);
header.getCell(1).setCellStyle(Styleheader);
//************************************
//************************************
//--- Row 2 me ye headers aaega
row = (HSSFRow) sheet1.createRow(2);
row.setHeight((short) 500);
row.createCell(1).setCellValue("Segment");
row.createCell(2).setCellValue("Region");
row.createCell(3).setCellValue("Borrower Name");
row.createCell(4).setCellValue("RM Name");
row.createCell(5).setCellValue("Date Full Information received");
row.createCell(6).setCellValue("Date Letter Issued");
row.createCell(7).setCellValue("Date RM Letter Requested");
//---
//----Font Formatting
for(int i =1;i<=7;i++) {
CellStyle stylerowHeading = workbook.createCellStyle();
Font font = workbook.createFont();
font.setBold(true);
font.setFontHeightInPoints((short)10);
stylerowHeading.setFont(font);
stylerowHeading.setAlignment(HorizontalAlignment.CENTER);
stylerowHeading.setVerticalAlignment(VerticalAlignment.CENTER);
stylerowHeading.setFillForegroundColor(IndexedColors.TURQUOISE.getIndex());
stylerowHeading.setFillPattern(FillPatternType.SOLID_FOREGROUND);
stylerowHeading.setBorderLeft(BorderStyle.MEDIUM);
stylerowHeading.setBorderRight(BorderStyle.MEDIUM);
stylerowHeading.setBorderTop(BorderStyle.MEDIUM);
stylerowHeading.setBorderBottom(BorderStyle.MEDIUM);
row.getCell(i).setCellStyle(stylerowHeading);
}
//------Auto Fit
for(int i =1;i<=7;i++)
{
sheet1.autoSizeColumn(i);
}
//*****************
//CellStyle dynamic_rows = workbook.createCellStyle();
//dynamic_rows.setBorderLeft(BorderStyle.THIN);
//dynamic_rows.setBorderRight(BorderStyle.THIN);
//dynamic_rows.setBorderBottom(BorderStyle.THIN);
//******************
String[] rowVal = new String[7];
insertrowVal(rowVal);
Map < String, String[]> m = new LinkedHashMap < String, String[] >();
//m.put( "1", rowVal);
m.put( "1", rowVal);
m.put( "2", rowVal);
m.put( "3", rowVal);
m.put( "4", rowVal);
m.put( "5", rowVal);
//Iterate over data and write to sheet
Set <String> keyid = m.keySet();
int rowid = 3;
for (String key : keyid) {
row = (HSSFRow) sheet1.createRow(rowid++);
String [] str = m.get(key);
int cellid = 1;
for (String obj : str)
{
//row.getCell(cellid).setCellStyle(dynamic_rows);
Cell cell = row.createCell(cellid++);
cell.setCellValue((String)obj);
}
}
try
{
FileOutputStream output = new FileOutputStream("D:\\work\\newgen2.xls");
workbook.write(output);
output.close();
System.out.println("File created ....");
}
catch(Exception e)
{
e.printStackTrace();
}
}
private static void insertrowVal(String[] rowVal)
{
rowVal[0]="apple";
rowVal[1]="Orange";
rowVal[2]="Banana";
rowVal[3]="Mango";
rowVal[4]="Papaya";
rowVal[5]="Lichy";
rowVal[6]="Grapes";
}
}
----------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------
JARS :- poi-bin-4.1.0-20190412.tar