0

When I create excel sheet through java ,the column which has number datatype in the oracle table, get converted to text format in excel.I want it to remain in the number format.Below is my code snippet for excel creation.

    public boolean prepareExcelFilefromQuery(String chanType,
                    Collection<List> queryDataRowWise, HttpServletResponse response)

            {
                List<String> queryDataColWise;
                HSSFRow row = null;
                HSSFCell cell = null;
                Integer rowCounter = 0;
                Integer colCounter;
                boolean success = false;
                try {

                    Iterator<List> rowIterator = queryDataRowWise.iterator();
                    HSSFWorkbook workbook = new HSSFWorkbook();
                    HSSFSheet sheet = workbook.createSheet(chanType + " Report");
                    System.out.println("First row/..." + sheet.getFirstRowNum());
                    while (rowIterator.hasNext()) {
                        colCounter = 0;

                        queryDataColWise = (List) rowIterator.next();
                        Iterator colIterator = queryDataColWise.iterator();

                        row = sheet.createRow(rowCounter++);
                        while (colIterator.hasNext()) {
                            cell = row.createCell(colCounter++);
                            Object o = colIterator.next();

                            if (o instanceof java.lang.String) {
                                String s = (String) o;
                                cell.setCellValue(s);
                            } else if (o instanceof java.lang.Double) {
                                Double d = (Double) o;
                                cell.setCellValue(d);
                            } else if (o instanceof java.lang.Integer) {
                                Integer i = (Integer) o;
                                 cell.setCellType(cell.CELL_TYPE_NUMERIC);
                                cell.setCellValue(i);
                            } else if (o instanceof java.util.Date) {
                                Date date = (Date) o;

                                SimpleDateFormat FORMATTER;

                                FORMATTER = new SimpleDateFormat("MM/dd/yyyy");
                                String date11 = FORMATTER.format(date);
                                HSSFCellStyle cellStyle = workbook.createCellStyle();
                                cellStyle.setDataFormat(HSSFDataFormat
                                        .getBuiltinFormat("m/d/yy"));
                                cell.setCellStyle(cellStyle);
                                cell.setCellValue(FORMATTER.parse(date11));
                            }
                        }
                    }

                    workbook.write(response.getOutputStream());
                    success = true;

                } catch (Exception e) {
                    logger.debug(
                            "Exception caught in prepareExcelFilefromQuery class ", e);
                    System.out
                            .println("Exception caught in prepareExcelFilefromQuery class ");
                    e.printStackTrace();

                } 
                }
                return success;
            }
1
  • These are not Java standard library classes. If you point us to the javadoc or the library, you will have more chances to get an answer. Commented Oct 29, 2012 at 11:35

1 Answer 1

1

It just use format like 0. More Format

CellStyle numberStyle = wb.createCellStyle();
numberStyle.setDataFormat(format.getFormat("0"));

CellStyle dateStyle = wb.createCellStyle();
dateStyle.setDataFormat(format.getFormat("d-mmm-yy"));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.