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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import javax.swing.tree.DefaultMutableTreeNode;
import jxl.*;
import jxl.write.*;
import javax.swing.*;
public class XLS_Generator {
public String xlssourcepath = null;
public String xlssourcedestination=null;
public boolean success=false;
private int nbelements=0;
public Workbook sourcedoc=null;
public WritableWorkbook destdoc=null;
private WritableSheet worksheet=null;
public boolean proceedconfirmation=true;
private int i=0;
public XLS_Generator (String source, String destination, DefaultMutableTreeNode rootnode) throws WriteException, IOException {
this.xlssourcedestination=destination;
this.xlssourcepath=source;
try {
this.sourcedoc= Workbook.getWorkbook(new File(source));
this.destdoc = Workbook.createWorkbook(new File(destination),this.sourcedoc);
this.worksheet=this.destdoc.getSheet(0);
}
catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage(),"XLS report generator",JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
}
initialize(rootnode,0);
this.destdoc.write();
this.destdoc.write();
}
private boolean initialize(DefaultMutableTreeNode node, int i) throws IOException, WriteException { //this method initializes the xls report by writing the first line that corresponds to the titles of the columns in the document
if (proceedconfirmation) {
Integer level=this.i+1;
String levelmsg=level.toString();
switch (level) {
case 1 : levelmsg="first"; break;
case 2 : levelmsg="second"; break;
case 3 : levelmsg="third"; break;
default : levelmsg+="th";
}
int response = JOptionPane.showConfirmDialog(null, "Please select a name for your column ","XLS Generator",JOptionPane.OK_CANCEL_OPTION);
if (response==JOptionPane.OK_OPTION) {
if (node.getChildAt(0).isLeaf()==false) {
String generalcategory = JOptionPane.showInputDialog(null,"Name for the column");
WritableCell cat_cell = this.worksheet.getWritableCell(this.i,0);
Label cat_lbl = (Label) cat_cell;
cat_lbl.setString(generalcategory);
this.i++; //pour passer a la colonne suivante
Label percent_lbl = new Label(this.i,0,"%");
this.worksheet.addCell(percent_lbl);
i++; // to go to the next column
this.destdoc.write();
DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getChildAt(0);
if(child.getChildAt(0).isLeaf()==false) {
initialize (child,this.i);
}
}
}
if (response==JOptionPane.CANCEL_OPTION) {
JOptionPane.showMessageDialog(null, "The xls won't be generated");
this.proceedconfirmation=false;
}
}
return proceedconfirmation;
}
} |
Partager