Hi I am trying to learn jexcel api if I am running it through a main class its actually working and generating the excel file however if I put in maven plugin I cannot find the generated excel anymore I am not getting error it keeps saying excel generated but there is none. If possible I wanted to ask for suggestion how to like store the file in variable or temporary directory then call it as attachment to email? Is it possible? Because I dont really need to store it I just need to call the generated file to attach in my email
try{
//*** for Database Connected ***//
Connection connect = null;
Statement s = null;
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost/mydatabase" +
"?user=root&password=root");
s = connect.createStatement();
String sql = "SELECT * FROM customer ORDER BY CustomerID ASC";
ResultSet rec = s.executeQuery(sql);
//*** for Excel Report ***//
String fileName = "C:\\java\\myExcel.xls";
WritableWorkbook workbook = Workbook.createWorkbook(new File(fileName));
//*** Create Font ***//
WritableFont fontBlue = new WritableFont(WritableFont.TIMES, 10);
fontBlue.setColour(Colour.BLUE);
WritableFont fontRed = new WritableFont(WritableFont.TIMES, 10);
fontRed.setColour(Colour.RED);
//*** Sheet 1 ***//
WritableSheet ws1 = workbook.createSheet("mySheet1", 0);
//*** Header ***//
WritableCellFormat cellFormat1 = new WritableCellFormat(fontRed);
//cellFormat2.setBackground(Colour.ORANGE);
cellFormat1.setAlignment(Alignment.CENTRE);
cellFormat1.setVerticalAlignment(VerticalAlignment.CENTRE);
cellFormat1.setBorder(Border.ALL, BorderLineStyle.THIN);
//*** Data ***//
WritableCellFormat cellFormat2 = new WritableCellFormat(fontBlue);
// cellFormat2.setWrap(true);
cellFormat2.setAlignment(jxl.format.Alignment.CENTRE);
cellFormat2.setVerticalAlignment(VerticalAlignment.CENTRE);
cellFormat2.setWrap(true);
cellFormat2.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.HAIR,
jxl.format.Colour.BLACK);
ws1.mergeCells(0, 0, 5, 0);
Label lable = new Label(0, 0,"Customer Report", cellFormat1);
ws1.addCell(lable);
//*** Header ***//
ws1.setColumnView(0, 10); // Column CustomerID
ws1.addCell(new Label(0,1,"CustomerID",cellFormat1));
ws1.setColumnView(1, 15); // Column Name
ws1.addCell(new Label(1,1,"Name",cellFormat1));
ws1.setColumnView(2, 25); // Column Email
ws1.addCell(new Label(2,1,"Email",cellFormat1));
ws1.setColumnView(3, 12); // Column CountryCode
ws1.addCell(new Label(3,1,"CountryCode",cellFormat1));
ws1.setColumnView(4, 10); // Column Budget
ws1.addCell(new Label(4,1,"Budget",cellFormat1));
ws1.setColumnView(5, 10); // Column Used
ws1.addCell(new Label(5,1,"Used",cellFormat1));
int iRows = 2;
while((rec!=null) && (rec.next()))
{
ws1.addCell(new Label(0,iRows,rec.getString("CustomerID"),cellFormat2));
ws1.addCell(new Label(1,iRows,rec.getString("Name"),cellFormat2));
ws1.addCell(new Label(2,iRows,rec.getString("Email"),cellFormat2));
ws1.addCell(new Label(3,iRows,rec.getString("CountryCode"),cellFormat2));
ws1.addCell(new Label(4,iRows,rec.getString("Budget"),cellFormat2));
ws1.addCell(new Label(5,iRows,rec.getString("Used"),cellFormat2));
++iRows;
}
workbook.write();
workbook.close();
System.out.println("Excel file created.");
// Close
try {
if(connect != null){
s.close();
connect.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}