java java到excel日期格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11059919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:37:57  来源:igfitidea点击:

java to excel date formatting

javaexceldatetimejxl

提问by samligo

The problem is when I'm running my application and have a grid (with strings and date columns) and save it as an excel file.

问题是当我运行我的应用程序并有一个网格(带有字符串和日期列)并将其保存为 excel 文件时。

When I save it for the first time everything is correctly formatted, but when I try to save the same exact grid again a second time, the date formatting is gone (it's just a float value that when i right click and format to a dateTime object works). When I restart my app it will work again for the first time, then lose formatting again

当我第一次保存它时,一切都正确格式化,但是当我再次尝试再次保存相同的精确网格时,日期格式消失了(它只是一个浮点值,当我右键单击并格式化为 dateTime 对象时作品)。当我重新启动我的应用程序时,它将再次运行,然后再次丢失格式

the code looks like this:

代码如下所示:

Calendar calendar = Calendar.getInstance();

calendar.setTime((Date)data);
            Date gmtDate = new Date(((Date) data).getTime() + (calendar.get(Calendar.ZONE_OFFSET) + calendar.get(Calendar.DST_OFFSET)));

writableCell = new jxl.write.DateTime(sheetColumn, sheetRow, gmtDate, jxl.write.DateTime.GMT);

cellFormat = new jxl.write.WritableCellFormat (new jxl.write.DateFormat("m/d/yyyy h:mm");

writableCell.setCellFormat(cellFormat);

sheet.addCell(writableCell);

I kept break-pointing and everything is as it should be (it always knew it was a dateTimetype before going in to the sheet), so I don't think it's from the code.

我一直在断点,一切都是应该的(dateTime在进入工作表之前它总是知道它是一种类型),所以我认为它不是来自代码。

Has anyone else run into this issue?

有没有其他人遇到过这个问题?

回答by duffymo

Try this:

试试这个:

cellFormat = new jxl.write.WritableCellFormat (new jxl.write.DateFormat("MM/dd/yyyy hh:mm");

回答by Mr. Polywhirl

Try to define a static WritableCellFormatwhich takes care of the date formatting.

尝试定义一个WritableCellFormat负责日期格式的静态。

// Required classes.
import java.util.TimeZone;
import jxl.write.DateFormat;
import jxl.write.DateTime;
import jxl.write.WritableCellFormat;

// Defined once.
public static final WritableCellFormat DATE_CELL_FRMT;
static {
    DateFormat df = new DateFormat("MM/dd/yyyy hh:mm");
    df.getDateFormat().setTimeZone(TimeZone.getTimeZone("GMT"))
    DATE_CELL_FRMT = new WritableCellFormat(df);
}

// Usage
writableCell = new DateTime(sheetColumn, sheetRow, gmtDate, DATE_CELL_FRMT);

回答by DmitrijZ

It seems that your question is similar to "jExcelApi - cell date format reusing doesn't work". Probably, answer for that question help you with your problem.

您的问题似乎类似于“jExcelApi - 单元格日期格式重用不起作用”。可能,该问题的回答可以帮助您解决问题。

Retelling of answer: According to FAQ(question: "I'm getting the error message 'too many different cell formats'") formats cannot be reused in different sheets because they are not designed to be reused this way

复述答案:根据常见问题解答(问题:“我收到错误消息'太多不同的单元格格式'”)格式不能在不同的工作表中重用,因为它们不是设计为以这种方式重用的

In your case, code may be like this:

在你的情况下,代码可能是这样的:

WritableCellFormat format = new jxl.write.WritableCellFormat(new jxl.write.DateFormat("m/d/yyyy h:mm"));
for(java.util.Date date : someDateList){
  WritableCell cell = new jxl.write.DateTime(someColumn, someRow, date, format);
  sheet.addCell(cell);
}