Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 以双重格式获取日期43318.4847916667,而不是06-08-2018 11:38:06_Java_Excel_Apache Poi_Hssfworkbook - Fatal编程技术网

Java 以双重格式获取日期43318.4847916667,而不是06-08-2018 11:38:06

Java 以双重格式获取日期43318.4847916667,而不是06-08-2018 11:38:06,java,excel,apache-poi,hssfworkbook,Java,Excel,Apache Poi,Hssfworkbook,我正在编写一个java代码,以使用HSSFWorkbook(.xls)生成excel文件。我需要以这种格式在一列中写入日期06-08-2018 11:38:06,但它生成的格式是43318.4847916667。 下面是我使用的代码片段。 请帮助我如何才能成功地写作 Workbook workbook = new HSSFWorkbook(); Sheet excelSheet = workbook.createSheet(); Row dataRow = excelSheet.createRo

我正在编写一个java代码,以使用HSSFWorkbook(.xls)生成excel文件。我需要以这种格式在一列中写入日期06-08-2018 11:38:06,但它生成的格式是43318.4847916667。
下面是我使用的代码片段。 请帮助我如何才能成功地写作

Workbook workbook = new HSSFWorkbook();
Sheet excelSheet = workbook.createSheet();
Row dataRow = excelSheet.createRow(1);;
Cell dataCell = dataRow.createCell(1);;
CellStyle cStyle = workbook.createCellStyle();
CreationHelper createHelper = workbook.getCreationHelper();         
cStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss"));
dataCell.setCellValue(new Date());
dataCell.setCellStyle(cStyle);

无法重现该问题

下面的完整示例生成一个
Excel
工作簿,该工作簿在单元格
B2
中有一个工作表和一个正确格式的日期

代码:

结果:


从你的评论来看,你的问题并没有显示全部。行有一个循环,每行应创建一个日期。这只适用于第42排

这个问题是众所周知的<代码>Excel对每个工作簿的单元格样式计数有限制。请参阅:

因此,如果在循环中一次又一次地创建新单元样式,则有时会达到该限制。但是,您不必总是在循环中创建新的单元样式。单元格样式存储在工作簿级别。只要在循环外创建任何所需的单元样式即可。然后仅将先前创建的单元样式应用于循环中的单元

以下内容适用于我,并创建了1000个正确格式的日期:

import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

class CreateExcelDate {

 public static void main(String[] args) throws Exception {

  try (Workbook workbook = new HSSFWorkbook(); 
       FileOutputStream fileout = new FileOutputStream("./Excel.xls") ) {

   CellStyle cStyle = workbook.createCellStyle();
   CreationHelper createHelper = workbook.getCreationHelper();         
   cStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss"));

   Sheet excelSheet = workbook.createSheet();

   for (int r = 1; r < 1000; r++) {
    Row dataRow = excelSheet.createRow(r);;
    Cell dataCell = dataRow.createCell(1);;
    dataCell.setCellValue(new java.util.GregorianCalendar(2019, 9, r));
    dataCell.setCellStyle(cStyle);
   }

   excelSheet.setColumnWidth(1, 25 * 256);

   workbook.write(fileout);
  }

 }
}
import java.io.FileOutputStream;
导入org.apache.poi.ss.usermodel.*;
导入org.apache.poi.hssf.usermodel.HSSFWorkbook;
类CreateExcelDate{
公共静态void main(字符串[]args)引发异常{
尝试(工作簿=新的HSSFWorkbook();
FileOutputStream fileout=新的FileOutputStream(“./Excel.xls”)){
CellStyle cStyle=workbook.createCellStyle();
CreationHelper createHelper=workbook.getCreationHelper();
setDataFormat(createHelper.createDataFormat().getFormat(“yyyy-MM-dd-HH:MM:ss”);
Sheet excelSheet=工作簿.createSheet();
对于(int r=1;r<1000;r++){
Row dataRow=excelSheet.createRow(r);;
Cell dataCell=dataRow.createCell(1);;
setCellValue(新java.util.Gregorianalendar(2019,9,r));
setCellStyle(cStyle);
}
excelSheet.setColumnWidth(1,25*256);
工作簿。写入(归档);
}
}
}
如果您想要“06-08-2018 11:38:06”,那么您的日期格式应该是“dd-MM-yyy-HH:MM:ss”。否则,您将丢失需要将工作簿写入的
FileOutputStream

private static final String FILE_NAME = "./out/MyFirstExcel.xls";

public static void main(String ... args) {
    Workbook workbook = new HSSFWorkbook();
    Sheet excelSheet = workbook.createSheet();
    Row dataRow = excelSheet.createRow(1);
    Cell dataCell = dataRow.createCell(1);
    CellStyle cStyle = workbook.createCellStyle();
    CreationHelper createHelper = workbook.getCreationHelper();
    cStyle.setDataFormat(createHelper.createDataFormat().getFormat("dd-MM-YYYY HH:mm:ss"));
    dataCell.setCellValue(new Date());
    dataCell.setCellStyle(cStyle);

    try {
        File file = new File(FILE_NAME);
        if(!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream outputStream = new FileOutputStream(file);
        workbook.write(outputStream);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

按照以下代码在DD-MM-YYYY HH:MM:ss中打印日期

String fileName = "myExcel.xls";
WritableWorkbook writableWorkbook = null;
response.setContentType("application/vnd.ms-excel");
writableWorkbook = Workbook.createWorkbook(response.getOutputStream());
WritableSheet excelOutputsheet = writableWorkbook.createSheet("Sheet1", 0);

DateFormat customDateFormatWithTime = new DateFormat("dd-MM-yyyy HH:mm:ss");
WritableCellFormat dateFormatWithTime = new 
WritableCellFormat(customDateFormatWithTime);

int row = 0;
int col = 0;
excelOutputsheet.setColumnView(col, 20);
Label lable1 = new Label(col, row, "Date and Time", cellFormat);
excelOutputsheet.addCell(lable1);

row = row + 1;
col = col + 1;
DateTime myDate = new jxl.write.DateTime(col, row, "Value", dateFormatWithTime);
excelOutputsheet.addCell(myDate);

根据我的经验,Excel会自行确定是否输入了日期或日期时间,并自行调整格式。您是否尝试过格式化日期并将格式化的
字符串写入单元格?您定义的数据格式显然不起作用。可能在写入单元格之前设置单元格样式,我认为值得一试。您只是缺少了
FileOutputStream
(请参见下面的答案),但其他情况下,日期会在您请求时在excel中生成(使用提供的
dateformat
)。您使用的是非microsoft excel(如OpenOffice、LibreOffice等?)@deHaar,谢谢您的回复,但如果我将其记为字符串,excel将无法识别其为日期格式,因此我们无法对其进行日期操作。好奇:如果您想要像
06-08-2018
这样的格式,并且是去年,那么,你为什么要给出一个像“yyyy-MM-dd”这样的模式,而且是第一年呢?@Andreas观察得很好。我猜这是一个复制粘贴错误嗨,阿克塞尔·里克特,谢谢你偷看你的代码片段。是的,我同意这只适用于一行。但我将其循环50行,直到第41行才产生问题,从第42行开始,它将日期存储为数字值,如前所述(43318.4847916667)。请给我任何建议,我会通知你。@Anam Qureshi:见我的补充。@Anam,你的问题得到回答了吗?如果是,请勾选勾号,将此标记为已解决。德克萨斯州。
String fileName = "myExcel.xls";
WritableWorkbook writableWorkbook = null;
response.setContentType("application/vnd.ms-excel");
writableWorkbook = Workbook.createWorkbook(response.getOutputStream());
WritableSheet excelOutputsheet = writableWorkbook.createSheet("Sheet1", 0);

DateFormat customDateFormatWithTime = new DateFormat("dd-MM-yyyy HH:mm:ss");
WritableCellFormat dateFormatWithTime = new 
WritableCellFormat(customDateFormatWithTime);

int row = 0;
int col = 0;
excelOutputsheet.setColumnView(col, 20);
Label lable1 = new Label(col, row, "Date and Time", cellFormat);
excelOutputsheet.addCell(lable1);

row = row + 1;
col = col + 1;
DateTime myDate = new jxl.write.DateTime(col, row, "Value", dateFormatWithTime);
excelOutputsheet.addCell(myDate);