1 // Define message
2 Message message = new MimeMessage(session);
3 message.setFrom(new InternetAddress(from));
4 message.addRecipient(Message.RecipientType.TO,
5 new InternetAddress(to));
6 message.setSubject("Hello JavaMail Attachment");
7
8 // Create the message part
9 BodyPart messageBodyPart = new MimeBodyPart();
10
11 // Fill the message
12 messageBodyPart.setText("Pardon Ideas");
13
14 Multipart multipart = new MimeMultipart();
15 multipart.addBodyPart(messageBodyPart);
16
17 // Part two is attachment
18 messageBodyPart = new MimeBodyPart();
19 DataSource source = new FileDataSource(filename);
20 messageBodyPart.setDataHandler(new DataHandler(source));
21 messageBodyPart.setFileName(filename);
22 multipart.addBodyPart(messageBodyPart);
23
24 // Put parts in message
25 message.setContent(multipart);
26
27 // Send the message
28 Transport.send(message);
1Workbook xlsFile = new HSSFWorkbook(); // create a workbook
2CreationHelper helper = xlsFile.getCreationHelper();
3Sheet sheet1 = xlsFile.createSheet("Sheet #1"); // add a sheet to your workbook
4
5while(rs.next())
6{
7 Row row = sheet1.createRow((short)0); // create a new row in your sheet
8 for(int i = 0; i < 12; i++)
9 {
10 row.createCell(i).setCellValue(
11 helper.createRichTextString(exceldata)); // add cells to the row
12 }
13}
14
15// Write the output to a temporary excel file
16FileOutputStream fos = new FileOutputStream("temp.xls");
17xlsFile.write(fos);
18fos.close();
19
20// Switch to using a `FileDataSource` (instead of ByteArrayDataSource)
21DataSource fds = new FileDataSource("temp.xls");
1ByteArrayOutputStream bos = new ByteArrayOutputStream();
2xlsFile.write(bos); // write excel data to a byte array
3fos.close();
4
5// Now use your ByteArrayDataSource as
6DataSource fds = new ByteArrayDataSource(bos.toByteArray(), "application/vnd.ms-excel");