Skip to Content
0
Former Member
May 29, 2007 at 02:56 AM

POI HSSF WDWebResource

16 Views

my requirement is not output any xml and xls file,

but byte to bind to the WDWebResource for FileDownload

below is my code that output physical file tm.xml and convert to physical file tm.xls

i dont want this file to store but just keep it in memory

previous tutorial is about Export to excel, but the column title is start with /MailList/EmailId not just EMAILID so i decide to use POI...

if i change this

if (type=="MailInbound"){

byte[] xmldocument=null;

xmldocument=xml_file.toString().getBytes("UTF-8");

InputStream in=null;

in.read(xmldocument);

generateInboundMailExcel(in);

}

how am i going to modify

public static void generateInboundMailExcel(InputStream xmlDocument) {

}

if (type=="MailInbound"){
FileOutputStream fout=new FileOutputStream("tm.xml");
OutputStreamWriter out=new OutputStreamWriter(fout,"UTF-8");
out.write(xml_file.toString());
out.flush();
out.close();
File xmlDocument = new File("tm.xml");	
generateInboundMailExcel(xmlDocument);
}


public static void generateInboundMailExcel(File xmlDocument) {
try {// Creating a Workbook
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet spreadSheet = wb.createSheet("spreadSheet");
spreadSheet.setColumnWidth((short) 0, (short) (256 * 25));
spreadSheet.setColumnWidth((short) 1, (short) (256 * 25));
// Parsing XML Document
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(xmlDocument);
NodeList nodeList = document.getElementsByTagName("TablesemailinElement");
//Create font style
HSSFFont font=wb.createFont();
font.setFontHeightInPoints((short)10);
font.setFontName("Verdana");
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFCellStyle style=wb.createCellStyle();
style.setFont(font);
// Creating Rows
HSSFRow row = spreadSheet.createRow(0);
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("DATE IN");
cell.setCellStyle(style);				  
cell = row.createCell((short) 1);
cell.setCellValue("EMAIL ID");
cell.setCellStyle(style);							   
cell = row.createCell((short) 2);
cell.setCellValue("SUBJECT");
cell.setCellStyle(style);
							  
for(int i=0;i<nodeList.getLength();i++){
HSSFRow rowno=spreadSheet.createRow(i+1);	
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
//************take note for string and date************** 
cell = rowno.createCell((short) 0);
cell.setCellValue(((Element) (nodeList.item(i)))
.getElementsByTagName("dateIn").item(0)
.getFirstChild().getNodeValue());
cell.setCellType(HSSFCell.CELL_TYPE_STRING); 
										
cell = rowno.createCell((short) 1);
cell.setCellValue(((Element) (nodeList.item(i)))
.getElementsByTagName("emailId").item(0)
.getFirstChild().getNodeValue());
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
										 
cell = rowno.createCell((short) 2);
cell.setCellValue(((Element) (nodeList.item(i)))
.getElementsByTagName("subject").item(0)
.getFirstChild().getNodeValue());
cell.setCellType(HSSFCell.CELL_TYPE_STRING);		 
}
	  	 
// Outputting to Excel spreadsheet
System.err.println("Outputing");
FileOutputStream output = new FileOutputStream(new File("tm.xls"));
wb.write(output);
output.flush();
output.close();
} catch (Exception e) {
System.out.println("Exception " + e.getMessage());	
}