0

当我通过java创建excel表时,numberoracle表中具有数据类型的列,text在excel中转​​换为格式。我希望它保持格式。number下面是我创建excel的代码片段。

    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;
            }
4

1 回答 1

1

它只是使用像0这样的格式。更多格式

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

CellStyle dateStyle = wb.createCellStyle();
dateStyle.setDataFormat(format.getFormat("d-mmm-yy"));
于 2012-10-29T11:54:44.140 回答