使用HSSFSheet将列格式类型设置为日期或时间

时间:2017-06-07 08:35:27

标签: java excel apache-poi

我想将列 Mobile_Time 设置为时间/日期时间类型列。 我试过了

protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        DAO d0 = new DAO();
        Map<String, String> AllUsermap = new HashMap<String, String>();
        AllUsermap = d0.colleagueMap(Integer.parseInt(request.getParameter("lid")), request.getParameter("ltype"));
        String date = request.getParameter("date");

        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition",
                "attachment; filename=All Employees Tracking Report [Date]:" + date + ".xls");
        Workbook workbook = new HSSFWorkbook();

        Font font = workbook.createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        CellStyle HeaderStyle = workbook.createCellStyle();
        HeaderStyle.setFont(font);

        for (String userK : AllUsermap.keySet()) {
            String userinfo[] = userK.split("-");
            String userinfoval[] = AllUsermap.get(userK).split("-");

            if (userinfo.length == 2 && userinfoval.length == 3) {
                Sheet sheet = workbook.createSheet(userinfoval[0]);

                // header
                Row row = sheet.createRow(0);
                Cell serial = row.createCell(0);
                serial.setCellValue("#");
                serial.setCellStyle(HeaderStyle);

                 serial = row.createCell(1);
                 serial.setCellValue("LOCATION");
                 serial.setCellStyle(HeaderStyle);

                 serial = row.createCell(2);
                 serial.setCellValue("DISTANCE");
                 serial.setCellStyle(HeaderStyle);

                 serial = row.createCell(3);
                serial.setCellValue("BATTERY");
                serial.setCellStyle(HeaderStyle);

                 serial = row.createCell(4);
                serial.setCellValue("SPEED");
                serial.setCellStyle(HeaderStyle);

                 serial = row.createCell(5);
                serial.setCellValue("GPS_ACCURACY");
                serial.setCellStyle(HeaderStyle);

                 serial = row.createCell(6);
                serial.setCellValue("STATUS");
                serial.setCellStyle(HeaderStyle);

                 serial = row.createCell(7);
                 serial.setCellType(1);
                serial.setCellValue("MOBILE_TIME");
                serial.setCellStyle(HeaderStyle);

                 serial = row.createCell(8);
                serial.setCellValue("SPEED");
                serial.setCellStyle(HeaderStyle);

                // End Header

                List<DailyReport> adr = new ArrayList<DailyReport>();
                adr = d0.seletspecificdailyreport(Integer.parseInt(userinfo[0]), date, userinfo[1]);
                if (adr.size() > 0) {
                    List<DailyReportDetails> adlrd = new ArrayList<DailyReportDetails>();
                    adlrd = d0.seletdailyreportdetails(adr.get(0).getIdDailyReport());
                    int i = 1;
                    for (DailyReportDetails drd : adlrd) {
                        row = sheet.createRow(i);
                        Cell cell = row.createCell(0);
                        cell.setCellValue(i);

                        cell = row.createCell(1);
                        cell.setCellValue(drd.getLocation());

                        cell = row.createCell(2);
                        cell.setCellValue(drd.getDistance());

                        cell = row.createCell(3);
                        cell.setCellValue(drd.getBattery());

                        cell = row.createCell(4);
                        cell.setCellValue(drd.getSpeed());

                        cell = row.createCell(5);
                        cell.setCellValue(drd.getAccuracy());

                        cell = row.createCell(6);
                        cell.setCellValue(drd.getOffline() == 1 ? "ONLINE" : "OFFLINE");

                        cell = row.createCell(7);
                        cell.setCellValue(drd.getMobiletime());

                        cell = row.createCell(8);
                        cell.setCellValue(userinfoval[2]);
                        i++;
                    }
                }
            }
        }

        workbook.write(response.getOutputStream()); // Write workbook to
                                                    // response.
        workbook.close();

    }

上部servlet正确编写xcel但是移动时间不是xcel中的时间或日期格式

我也尝试为mobile_time列串行设置行。

serial = row.createCell(7);
                    serial.setCellType(HSSFCell.LAST_COLUMN_NUMBER);
                    serial.setCellValue("MOBILE_TIME");
                    serial.setCellStyle(HeaderStyle);

并尝试了生成行的下一行

cell = row.createCell(7);

                    try {
                        cell.setCellValue(new SimpleDateFormat("HH:mm:ss").parse(drd.getMobiletime()));
                    } catch (ParseException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

但仍然是mobile_time列是字符格式。什么是正确的解决方案?

1 个答案:

答案 0 :(得分:1)

您可以这样做:

set_fact