Writing the data into java is having few easy steps.
Step 1- Create HSSFWorkbook class object.
HSSFWorkbook workbook = new HSSFWorkbook();
Step 2- Call workbook.createSheet() method by mentioning the sheet name.
HSSFSheet sheet = workbook.createSheet("Sample sheet");
Step 3- Create data in form of Object array and add this object array into Map object.
Map data = new LinkedHashMap();
data.put("1", new Object[]{"Year", "WinnerCountryName", "LosserCountryName","By"});
data.put("2", new Object[]{"1975", "WestInddies", "Australia","17 Run"});
.
.
Note : I have created LinkedHashMap object instead of HashMap because of preserving the insertion order.
Step 4- Iterate this Map object and create the row dynamically.
Row row = sheet.createRow(rownum++);
Step 5- Create a cell by Iterating this row object and set the cell value to it based on types.
row.createCell(cellnum++);
if (obj instanceof Date) {
cell.setCellValue((Date) obj);
} else if (obj instanceof Boolean) {
cell.setCellValue((Boolean) obj);
} else if (obj instanceof String) {
cell.setCellValue((String) obj);
} else if (obj instanceof Double) {
cell.setCellValue((Double) obj);
}
Step 6- Create FileOutputStream class object.
FileOutputStream out = new FileOutputStream(new File("D:\\Ashish\\Personal\\WebApplication1\\iccworldcuplistwrite.xls"));
Step 7- Write the data into file by calling workbook.write(out);
Example:
WriteIntoExcel.java
Output:
Step 1- Create HSSFWorkbook class object.
HSSFWorkbook workbook = new HSSFWorkbook();
Step 2- Call workbook.createSheet() method by mentioning the sheet name.
HSSFSheet sheet = workbook.createSheet("Sample sheet");
Step 3- Create data in form of Object array and add this object array into Map object.
Map
data.put("1", new Object[]{"Year", "WinnerCountryName", "LosserCountryName","By"});
data.put("2", new Object[]{"1975", "WestInddies", "Australia","17 Run"});
.
.
Note : I have created LinkedHashMap object instead of HashMap because of preserving the insertion order.
Step 4- Iterate this Map object and create the row dynamically.
Row row = sheet.createRow(rownum++);
Step 5- Create a cell by Iterating this row object and set the cell value to it based on types.
row.createCell(cellnum++);
if (obj instanceof Date) {
cell.setCellValue((Date) obj);
} else if (obj instanceof Boolean) {
cell.setCellValue((Boolean) obj);
} else if (obj instanceof String) {
cell.setCellValue((String) obj);
} else if (obj instanceof Double) {
cell.setCellValue((Double) obj);
}
Step 6- Create FileOutputStream class object.
FileOutputStream out = new FileOutputStream(new File("D:\\Ashish\\Personal\\WebApplication1\\iccworldcuplistwrite.xls"));
Step 7- Write the data into file by calling workbook.write(out);
Example:
WriteIntoExcel.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | package com.atozjavatutorials; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Date; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; public class WriteIntoExcel { public static void main(String[] args) { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet("Sample sheet"); Map<String, Object[]> data = new LinkedHashMap<String, Object[]>(); data.put("1", new Object[]{"Year", "WinnerCountryName", "LosserCountryName","By"}); data.put("2", new Object[]{"1975", "WestInddies", "Australia","17 Run"}); data.put("3", new Object[]{"1979", "WestInddies", "England","92 Run"}); data.put("4", new Object[]{"1983", "India", "WestIndies","43 Run"}); data.put("5", new Object[]{"1987", "Australia", "England","7 Run"}); data.put("6", new Object[]{"1992", "Pakistan", "England","22 Run"}); data.put("7", new Object[]{"1996", "Sri Lanka", "Australia","7 Wicket"}); data.put("8", new Object[]{"1999", "Australia", "Pakistan","8 Wicket"}); data.put("9", new Object[]{"2003", "Australia", "India","125 Run"}); data.put("10", new Object[]{"2007", "Australia", "Srilanka","53 Run"}); data.put("11", new Object[]{"2011", "India", "Srilanka","6 Wicket"}); data.put("12", new Object[]{"2015", "Australia", "New Zeland","7 Wicket"}); Set<String> keyset = data.keySet(); int rownum = 0; for (String key : keyset) { Row row = sheet.createRow(rownum++); Object[] objArr = data.get(key); int cellnum = 0; for (Object obj : objArr) { Cell cell = row.createCell(cellnum++); if (obj instanceof Date) { cell.setCellValue((Date) obj); } else if (obj instanceof Boolean) { cell.setCellValue((Boolean) obj); } else if (obj instanceof String) { cell.setCellValue((String) obj); } else if (obj instanceof Double) { cell.setCellValue((Double) obj); } } } try { FileOutputStream out = new FileOutputStream(new File("D:\\Ashish\\Personal\\WebApplication1\\iccworldcuplistwrite.xls")); workbook.write(out); out.close(); System.out.println("Excel written successfully.."); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } |
Output:
No comments :
Post a Comment