/Users/johnr/Desktop/IA_14_-_Stage_P_Upload_all_2021-04-08/IBCompSciInternalAssessment Lara April 6th/src/ibcompsciinternalassessment/ExportData.java
  1 /*
  2  * To change this license header, choose License Headers in Project Properties.
  3  * To change this template file, choose Tools | Templates
  4  * and open the template in the editor.
  5  */
  6 package ibcompsciinternalassessment;
  7 
  8 /**
  9  *
 10  * @author 21258
 11  */
 12 import java.io.FileOutputStream;
 13 import java.io.IOException;
 14 import java.util.ArrayList;
 15 import java.util.logging.Level;
 16 import java.util.logging.Logger;
 17 import org.apache.poi.ss.usermodel.Cell;
 18 import org.apache.poi.ss.usermodel.Row;
 19 import org.apache.poi.xssf.usermodel.XSSFSheet;
 20 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 21 
 22 public class ExportData {       // FULLY FUNCTIONING NO ERRORS
 23     // CONSTANTS
 24     /*----------------------------------------------------------*/
 25     private static int counter = 1;
 26     static final String fileName = "ContributionForms";
 27     static final String fileType = ".xlsx";
 28     /*----------------------------------------------------------*/
 29 
 30     public void exportToExcel(ArrayList<Contribution> contributions) throws IOException{
 31         // creating workbook
 32         XSSFWorkbook workbook = new XSSFWorkbook();
 33         // creating sheet with name "Report" in workbook
 34         XSSFSheet sheet = workbook.createSheet("Report");
 35         // this method creates header for our table
 36         createHeader(sheet, workbook);
 37 
 38         int rowCount = 0;
 39         for (Contribution contrib : contributions) {
 40             // creating row 
 41             Row row = sheet.createRow(++rowCount);
 42 
 43             // adding first cell to the row
 44             Cell nameCell = row.createCell(0);
 45             nameCell.setCellValue(contrib.getDonorName());
 46 
 47             // adding estimated amount cell to the row
 48             Cell amountCell = row.createCell(1);
 49             amountCell.setCellValue(contrib.getAmountEstimated());
 50 
 51             //adding due diligence cell to the row
 52             Cell dDCell = row.createCell(2);
 53             dDCell.setCellValue(contrib.getDueDiligence());
 54             
 55             //adding due diligence comments cell to the row
 56             Cell dDCommentsCell = row.createCell(3);
 57             dDCell.setCellValue(contrib.getdDComments());
 58 
 59             //adding budget year cell to the row
 60             Cell bYCell = row.createCell(4);
 61             bYCell.setCellValue(contrib.getBudgetYear());
 62 
 63             //adding bY comment cell to the row
 64             Cell bYCommentsCell = row.createCell(5);
 65             bYCommentsCell.setCellValue(contrib.getbYComments());
 66 
 67             //adding retro true false cell to the row
 68             Cell retroAcceptedCell = row.createCell(6);
 69             retroAcceptedCell.setCellValue(contrib.isIsRetroactivityAccepted());
 70 
 71             //adding retro comments cell to the row
 72             Cell retroCommentsCell = row.createCell(7);
 73             retroCommentsCell.setCellValue(contrib.getRetroComments());
 74 
 75             //adding geographical interest cell to the row
 76             Cell geoInterestCell = row.createCell(8);
 77             geoInterestCell.setCellValue(contrib.getGeoInterestName());
 78 
 79             //adding third cell to the row
 80             Cell geoCommentsCell = row.createCell(9);
 81             geoCommentsCell.setCellValue(contrib.getGeoComments());
 82 
 83             //adding third cell to the row
 84             Cell thematicCell = row.createCell(10);
 85             thematicCell.setCellValue(contrib.getThematicInterest());
 86 
 87             //adding third cell to the row
 88             Cell repReqCell = row.createCell(11);
 89             repReqCell.setCellValue(contrib.getReportingRequirements());
 90 
 91             //adding third cell to the row
 92             Cell visReqCell = row.createCell(12);
 93             visReqCell.setCellValue(contrib.getVisibilityRequirements());
 94 
 95             //adding third cell to the row
 96             Cell otherCommentsCell = row.createCell(13);
 97             otherCommentsCell.setCellValue(contrib.getOtherComments());
 98 
 99 
100         }
101         try (FileOutputStream outputStream = new FileOutputStream(fileName + counter+ fileType)) {
102             workbook.write(outputStream);
103             counter++;
104         } catch (IOException ex) {
105             Logger.getLogger(ExportData.class.getName()).log(Level.SEVERE, null, ex);
106         }
107 
108         finally {
109             // don't forget to close workbook to prevent memory leaks
110             workbook.close();
111         }
112     }
113 
114     private static void createHeader(XSSFSheet sheet, XSSFWorkbook workbook) {
115         Row headerRow = sheet.createRow(0);
116         headerRow.createCell(0).setCellValue("Donor Name");
117         headerRow.createCell(1).setCellValue("Amount Estimated");
118         headerRow.createCell(2).setCellValue("Due Diligence");
119         headerRow.createCell(3).setCellValue("DD Comments");
120         headerRow.createCell(4).setCellValue("Budget Year");
121         headerRow.createCell(5).setCellValue("Budget Comments");
122         headerRow.createCell(6).setCellValue("Retroactivity Accepted");
123         headerRow.createCell(7).setCellValue("Retroactivity Comments");
124         headerRow.createCell(8).setCellValue("Geographical Interest");
125         headerRow.createCell(9).setCellValue("Geographical Comments");
126         headerRow.createCell(10).setCellValue("Thematic Interest");
127         headerRow.createCell(11).setCellValue("Reporting Requirements");
128         headerRow.createCell(12).setCellValue("Visibility Requirements");
129         headerRow.createCell(13).setCellValue("Other Comments");
130     }
131 
132 }
133