使用POI Excel中设置时间 [英] Setting Time in Excel using POI

查看:675
本文介绍了使用POI Excel中设置时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个使用POI API在Java中的Excel工作表。在Excel中的工作表我想有独处的时间单元。通过设置此我们可以包括在该particuluar列的求和的细胞,因为我们在列数做。为此,我们需要在细胞格式化为时间>> 13时三十分55秒。 (内部格式是'H:MM:SS; @')。而我们需要删除的日期部分从细胞。

当我读了电池使用POI单元格的值,它返回为太阳12月31日01:00:00北京时间1899年(当我设置值1点),单元格的格式指数为166单元格的格式字符串为H:MM:SS; @'。

设置它是从Excel和单元格值1800-12 - 31和时间值读取格式和风格后,新的Excel单元格显示为######(错误)和细胞值被设置好的为-1。下面是code我都习惯了。我错过了什么?是否可以为我所需要的设定值。

  InputStream为=新的BufferedInputStream(新的FileInputStream(<文件名>中));
    XSSFWorkbook WB =新XSSFWorkbook(是);
    is.close();    XSSFSheet片= wb.getSheetAt(0);
    XSSFRow行= sheet.getRow(2);    XSSFCell细胞= row.getCell(18);
    的System.out.println(ExcelFileReader主cell.getDateCellValue():'+ cell.getDateCellValue()+');
    的System.out.println(ExcelFileReader主cell.getCellStyle()getDataFormat():'+ cell.getCellStyle()getDataFormat()+');
    的System.out.println(ExcelFileReader主cell.getCellStyle()getDataFormat():'+ cell.getCellStyle()getDataFormatString()+');    XSSFRow ROW1 = sheet.createRow(21);
    XSSFCell CELL1 = row1.createCell(2);    cell1.setCellStyle(cell.getCellStyle());
    cell1.setCellValue(cell.getDateCellValue());    日历虚设= Calendar.getInstance();
    dummy.setLenient(假);
    dummy.set(Calendar.YEAR,1899年);
    dummy.set(的Calendar.MONTH,Calendar.DECEMBER);
    dummy.set(Calendar.DATE,31);    dummy.set(Calendar.HOUR,00);
    dummy.set(Calendar.MINUTE,00);
    dummy.set(Calendar.SECOND,00);
    dummy.set(Calendar.MILLISECOND,00);    日历立方厘米= Calendar.getInstance();
    XSSFRow ROW2 = sheet.createRow(25);
    XSSFCell CELL2 = row2.createCell(2);    dummy.set(Calendar.HOUR,cc.get(Calendar.HOUR));
    dummy.set(Calendar.MINUTE,cc.get(Calendar.MINUTE));
    dummy.set(Calendar.SECOND,cc.get(Calendar.SECOND));
    dummy.set(Calendar.MILLISECOND,cc.get(Calendar.MILLISECOND));    的System.out.println(ExcelFileReader主要哑:'+ dummy.getTime()+');
    cell2.setCellValue(dummy.getTime());    CellStyle风格= wb.createCellStyle();
    DATAFORMAT DF = wb.createDataFormat();
    style.setDataFormat(df.getFormat([H]:MM:SS; @));
    cell2.setCellStyle(样式);    FOS的FileOutputStream =新的FileOutputStream(新文件(<新的Excel文件>));
    wb.write(FOS);
    fos.close();
    的System.out.println(ExcelFileReader DONE);

以下是该程序的输出

  ExcelFileReader主要cell.getDateCellValue():'太阳12月31日零点15分○○秒IST 1899
    ExcelFileReader主cell.getCellStyle()getDataFormat():'166'
    ExcelFileReader主要cell.getCellStyle()getDataFormat():H:MM:SS; @'。
    ExcelFileReader主要哑:太阳12月31日11时32分24秒IST 1899年
    ExcelFileReader DONE


解决方案
在Excel

日期和时间通常存储为浮点数,完全和分数天自1900年或1904年,您可以通过键入0.5到Excel看到这细胞和格式作为一个时间 - 它会显示为12:00:00,或者设置为1.5的值,它会在一千九百零四分之一千九百显示为一个日期作为一个日期,作为12:00:00时间或36:00:00如果时间允许> 24小时(这是一个不同的格式code)

什么你需要做的是火起来的Excel中,您希望有显示的时间值类型的副本,并制定格式字符串,使得它显示为你想让它。然后,请记格式code,并把那个给POI作为单元格的数据格式。现在,使用类似DateUtil在POI建立你的Date对象,并将其设置为单元格的值。有了正确的格式字符串中,Excel将只显示时间的一部分,而不是日期

I am trying to create an Excel Work sheet using POI api in Java. In that Excel Work Sheet I want to have a cell with TIME alone. By setting this we can include the cell in summation of that particuluar column as we do in number columns. For this we need to format the cell as Time >> 13:30:55. (The internal format is 'h:mm:ss;@' ). And we need to remove the date part from the cell.

When I read the cell the cell value using the POI, it is returning as 'Sun Dec 31 01:00:00 IST 1899' ( When i set the value as 1:00 ), the cell format index is 166 and the cell format string is 'h:mm:ss;@'.

After setting the formats and style which were read from the excel and the cell value as 1800-December-31 and with the time value, the new excel shows cell as '######' (error) and cell value is setted as '-1'. Below is the code I have used. Did I miss anything ? Is it possible to set the value as I required.

    InputStream is = new BufferedInputStream(new FileInputStream("<FileName>"));
    XSSFWorkbook wb = new XSSFWorkbook(is);
    is.close();

    XSSFSheet sheet = wb.getSheetAt(0);
    XSSFRow row = sheet.getRow(2);

    XSSFCell cell = row.getCell(18);
    System.out.println("ExcelFileReader main cell.getDateCellValue() : '" + cell.getDateCellValue() + "'");
    System.out.println("ExcelFileReader main cell.getCellStyle().getDataFormat() : '" + cell.getCellStyle().getDataFormat() + "'");
    System.out.println("ExcelFileReader main cell.getCellStyle().getDataFormat() : '" + cell.getCellStyle().getDataFormatString() + "'");

    XSSFRow row1 = sheet.createRow(21);
    XSSFCell cell1 = row1.createCell(2);

    cell1.setCellStyle(cell.getCellStyle());
    cell1.setCellValue(cell.getDateCellValue());

    Calendar dummy = Calendar.getInstance();
    dummy.setLenient(false);
    dummy.set(Calendar.YEAR, 1899);
    dummy.set(Calendar.MONTH, Calendar.DECEMBER);
    dummy.set(Calendar.DATE, 31);

    dummy.set(Calendar.HOUR, 00);
    dummy.set(Calendar.MINUTE, 00);
    dummy.set(Calendar.SECOND, 00);
    dummy.set(Calendar.MILLISECOND, 00);

    Calendar cc = Calendar.getInstance();
    XSSFRow row2 = sheet.createRow(25);
    XSSFCell cell2 = row2.createCell(2);

    dummy.set(Calendar.HOUR, cc.get(Calendar.HOUR));
    dummy.set(Calendar.MINUTE, cc.get(Calendar.MINUTE));
    dummy.set(Calendar.SECOND, cc.get(Calendar.SECOND));
    dummy.set(Calendar.MILLISECOND, cc.get(Calendar.MILLISECOND));

    System.out.println("ExcelFileReader main dummy : '" + dummy.getTime() + "'");
    cell2.setCellValue(dummy.getTime());

    CellStyle style = wb.createCellStyle();
    DataFormat df = wb.createDataFormat();
    style.setDataFormat(df.getFormat("[h]:mm:ss;@"));
    cell2.setCellStyle(style);

    FileOutputStream fos = new FileOutputStream(new File("<New Excel file>"));
    wb.write(fos);
    fos.close();
    System.out.println("ExcelFileReader DONE");

The following is the output of the program.

    ExcelFileReader main cell.getDateCellValue() : 'Sun Dec 31 00:15:00 IST 1899'
    ExcelFileReader main cell.getCellStyle().getDataFormat() : '166'
    ExcelFileReader main cell.getCellStyle().getDataFormat() : 'h:mm:ss;@'
    ExcelFileReader main dummy : 'Sun Dec 31 11:32:24 IST 1899'
    ExcelFileReader DONE

解决方案

Dates and Times in Excel are normally stored as floating point numbers, as full and fractional days since 1900 or 1904. You can see this by typing 0.5 into an excel cell and formatting as a time - it'll show as 12:00:00, or set a value of 1.5 and it'll show as a date in 1900/1904 as a date, 12:00:00 as a time, or 36:00:00 if a time that allows >24 hours (it's a different format code)

What you'll want to do is fire up a copy of Excel, type in the time value you want to have displayed, and work out the format string that makes it show up as you want it to. Then, make a note of that format code, and give that to POI as the data format for the cell. Now, use something like DateUtil in POI to build up your Date object, and set that to the cell as the value. With the right format string in, Excel will show only the time part and not the date

这篇关于使用POI Excel中设置时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆