2017-10-23 5 views
0
public class createChart { 

public static void main(String[] args) { 
    ArrayList<Integer> studentList = new ArrayList<>(); 
    ArrayList<Integer> gradeList = new ArrayList<>(); 
    ArrayList<String> header = new ArrayList<>(); 

    header.add("Attendance Sheet"); 

    for(int i = 1; i <= 20; i++){ 
     studentList.add(i); 
     if(i <= 20){ 
      gradeList.add((80+i)); 
     } 

    } 

    int bordernum = 2; 
    try { 
     FileOutputStream fileOut = new FileOutputStream("Attendance Sheet.xls"); 
     HSSFWorkbook workbook = new HSSFWorkbook(); 
     HSSFSheet worksheet = workbook.createSheet("Attendance sheet"); 



     // row 1 for Prinitng attendance sheet in center 
     HSSFRow row0 = worksheet.createRow((short) 0);//1 
     HSSFCell cellmid = row0.createCell((short) (gradeList.size()/2)-1);//2 
     cellmid.setCellValue(header.get(0));//3 
     HSSFCellStyle cellStylem = workbook.createCellStyle();//4 
     cellStylem.setFillForegroundColor(HSSFColor.GOLD.index);//5 
     cellmid.setCellStyle(cellStylem);//6 
     createBorders(workbook, cellmid, 1); 
     HSSFCell cellmid2 = row0.createCell((short) (gradeList.size()/2));//2 
     createBorders(workbook, cellmid2, 1); 



     // row 2 with all the dates in the correct place 
     HSSFRow row1 = worksheet.createRow((short) 1);//1 
     HSSFCell cell1; 
     for(int y = 0; y < gradeList.size(); y++){ 

      cell1 = row1.createCell((short) y+1);//2 
      cell1.setCellValue(gradeList.get(y));//3 
      createBorders(workbook, cell1, bordernum); 

     } 
     HSSFCellStyle cellStylei = workbook.createCellStyle();//4 
     cellStylei.setFillForegroundColor(GREEN.index);//5 



     // row 3 and on until the studentList.size() create the box. 
     int counter = 0; 
     for(int stu = 2; stu <= (studentList.size()+1); stu++){ 
      HSSFRow Row = worksheet.createRow((short) stu);//1 
      for(int gr = 0; gr <= gradeList.size(); gr++){ 
       if(gr == 0){ 
        HSSFCell cell = Row.createCell((short) 0);//2 
        cell.setCellValue(studentList.get(counter));//3 
        HSSFCellStyle cellStyle2 = workbook.createCellStyle();//4 
        cellStyle2.setFillForegroundColor(IndexedColors.YELLOW.getIndex()); 
        cellStyle2.setFillForegroundColor(HSSFColor.GOLD.index);//5 
        cell.setCellStyle(cellStyle2);//6 
        createBorders(workbook, cell, 2); 
       }else{ 
        HSSFCell Cell = Row.createCell((short) gr);//2 
        createBorders(workbook, Cell, 3); 
       } 


      } 
      counter++; 
     } 
     workbook.write(fileOut); 
     fileOut.flush(); 
     fileOut.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 
public static void createBorders(HSSFWorkbook workbook,HSSFCell cell, int x){ 
    if(x == 1){ 
     HSSFCellStyle style = workbook.createCellStyle(); 
     //style.setFillBackgroundColor(HSSFColor.HSSFColorPredefined.LIGHT_BLUE.getIndex()); 
     //style.setFillPattern(FillPatternType.SOLID_FOREGROUND); 
     style.setBorderBottom(BorderStyle.THICK); 
     style.setBottomBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex()); 
     style.setBorderLeft(BorderStyle.THICK); 
     style.setLeftBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex()); 
     style.setBorderRight(BorderStyle.THICK); 
     style.setRightBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex()); 
     style.setBorderTop(BorderStyle.THICK); 
     style.setTopBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex()); 
     cell.setCellStyle(style); 
    } 
    else if(x == 2){ 
     HSSFCellStyle style = workbook.createCellStyle(); 
     //style.setFillBackgroundColor(HSSFColor.HSSFColorPredefined.LIGHT_BLUE.getIndex()); 
     //style.setFillPattern(FillPatternType.SOLID_FOREGROUND); 
     style.setBorderBottom(BorderStyle.MEDIUM); 
     style.setBottomBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex()); 
     style.setBorderLeft(BorderStyle.MEDIUM); 
     style.setLeftBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex()); 
     style.setBorderRight(BorderStyle.MEDIUM); 
     style.setRightBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex()); 
     style.setBorderTop(BorderStyle.MEDIUM); 
     style.setTopBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex()); 
     cell.setCellStyle(style); 
    }else { 
     HSSFCellStyle style = workbook.createCellStyle(); 
     //style.setFillBackgroundColor(HSSFColor.HSSFColorPredefined.AQUA.getIndex()); 
     //style.setFillPattern(FillPatternType.SOLID_FOREGROUND); 
     style.setBorderBottom(BorderStyle.THIN); 
     style.setBottomBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex()); 
     style.setBorderLeft(BorderStyle.THIN); 
     style.setLeftBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex()); 
     style.setBorderRight(BorderStyle.THIN); 
     style.setRightBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex()); 
     style.setBorderTop(BorderStyle.THIN); 
     style.setTopBorderColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex()); 
     cell.setCellStyle(style); 
    } 

} 

Der Code markieren Werte von studentList (Zeile 3 bis studentList.size() + 2) schreibt in jede Zeile in Spalte 0 und von gradesList (Reihe 1 von Spalte schreibt 1 to grainsList.size() + 1) in eine Datei Anwesenheitsliste, wie eine Zelle in Excel mit APACHE POI HSSF

Was soll ich bearbeiten, damit ich alle studentList mit hellgrün, gradeList mit hellorange, Title (header) mit gelb und den Rest von markieren kann die leeren Boxen mit hellblau?

Image 1 is the product of the code above Image 2 I need my sheet to look like this

Ich brauche Hilfe mein Blatt von Bild 1 bis 2 Bild zu transformieren

+0

Nicht downvoting, sieht aber viel wie Hausaufgaben für mich aus. –

+0

Haben Sie auch schon etwas probiert? Irgendwelche eigenen Ideen? –

Antwort

0

Sie erstellen die Hintergrundfarbe in Zeile 5, sondern zwingenden es dann, wenn man die Grenzen schaffen. Sie müssen alle Stile gleichzeitig anwenden, um das Problem zu lösen.

Abgesehen davon, müssen Sie die FillPattern, zum Beispiel umfassen:

 style.setFillForegroundColor(HSSFColor.GOLD.index); 
     style.setFillPattern(CellStyle.SOLID_FOREGROUND); 

Sie ein Beispiel, in dem

Hoffnung Apache POI Quick Guide finden kann dies helfen kann. Danke.

+0

können Sie mir bitte sagen, was zu welcher Zeile bitte ich versuche style.setFillPattern (CellStyle.SOLID_FOREGROUND); // 6 aber nichts ändert –

+0

Auch die Apache Poi Quick Guide

+0

In Ihrer createBorders-Methode können Sie die Zeilen style.setFillForegroundColor (HSSFColor.GREEN.index); und style.setFillPattern (CellStyle.SOLID_FOREGROUND); wenn beispielsweise X == 2, um zu sehen, wie der Hintergrund zu diesen Zellen hinzugefügt wird. Dann können Sie es als ein Beispiel verwenden, um die Stile anzuwenden – ervidio

Verwandte Themen