Women in Technology

Hear us Roar



Article:
  Opening Microsoft File Formats to Java
Subject:   reading and writing excel and database entries and writing it into new excelsheet
Date:   2007-08-31 03:15:41
From:   Rupali.
Response to: reading and writing excel and database entries and writing it into new excelsheet

Hi Bhavna,
I need to look into the kind of code or entries you have to fetch form data base. Well till then, i can give you the general code for the same. It may be like,,
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Report");

FileOutputStream fileOutputStream = new FileOutputStream(exportFile);
Iterator rowIterator = tabularRows.iterator();
int rowCount = 0;
while (rowIterator.hasNext()) {
HSSFRow row = sheet.createRow(rowCount++);
String rowToExport = (String) rowIterator.next();
List<String> columnsToExportForRow = tokenizeString(rowToExport,
your string from database);
int cellCount = 0;
if (columnsToExportForRow != null && columnsToExportForRow.size() > 0) {
Iterator columnIterator = columnsToExportForRow.iterator();
// System.out.println("\n");
while (columnIterator.hasNext()) {
HSSFCell cell = row.createCell((short) cellCount++);
cell.setCellType(1); //store numbers as text
cell.setCellValue((String) columnIterator.next());
if (rowCount == 1) {
/**
* this is the header row. so decorate it
*/
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short) 12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFCellStyle style = workbook.createCellStyle();
style.setFillBackgroundColor(HSSFColor.BLUE_GREY.index);
style.setFont(font);
cell.setCellStyle(style);
}
}// end of while (columnIterator.hasNext())
}// end of if(columnsToExportForRow!=null && ...
}// end of while(listIterator.hasNext())
try {
workbook.write(fileOutputStream);
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException e) {
responseString = "The report could not be generated due to an internal error.";
}
}// end of if(tabularRows!=null && tabularRows.size()>0)
} catch (FileNotFoundException e) {
responseString = "The report could not be generated due to an internal error.";
}
return responseString;
}



Try this code out and let me know if anything more you need to know on it.
Thanks.
Rupali