如何将Date值设置为单元格

问题描述 投票:1回答:3

以下是我的代码:

String monthEndDate = "31-Dec-17";
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy",java.util.Locale.ENGLISH);
XSSFCell updateDateCell = sheet.getRow(rownumber).getCell(15);
XSSFCellStyle cellStyle = (XSSFCellStyle)updateDateCell.getCellStyle();
CreationHelper createHelper = wb.getCreationHelper();
cellStyle.setDataFormat(
createHelper.createDataFormat().getFormat("dd-MMM-yy"));
Date updateDate = sdf.parse(monthEndDate);
updateDateCell.setCellValue(updateDate);
updateDateCell.setCellStyle(cellStyle);

它设置数值43100.0

java excel apache-poi xssf
3个回答
2
投票

我怀疑你的问题是你通过CellStyle得到Cell.getCellStyle,然后你覆盖了CellStyle

CellStyles在Excel级别定义的Workbook。这意味着,并非每个单元格都有自己的单元格样式,但单元格共享工作簿级别定义的单元格样式。

因此,如果您通过CellStyle获取Cell.getCellStyle,然后多次覆盖CellStyle,那么只有最后一次覆盖才会激活。所以我怀疑,你的完整代码覆盖了相同的单元格样式,从另一个单元格中获取,在用日期数字格式覆盖它之后使用另一种数字格式。

简单的结论可能是真正给每个细胞它自己的细胞样式。但这也是错误的,因为工作簿中的单元格样式数量有限制。所以我们需要

  1. 拥有所需的自己的细胞样式。
  2. 尽可能多地共享单元格样式。

为了达到这个目的,CellUtil可用于apache poi。如果工作簿中尚未定义相同的单元格样式,并且仅在工作簿中已定义相同的单元格样式时使用该单元格样式,则此方法仅提供创建新单元格样式的方法。

例:

import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

import org.apache.poi.ss.util.CellUtil;

import java.text.SimpleDateFormat;
import java.util.Date;

import java.util.Map;
import java.util.HashMap;

public class ExcelSetDateValue {

 public static void main(String[] args) throws Exception {
  XSSFWorkbook wb = (XSSFWorkbook)WorkbookFactory.create(new FileInputStream("ExcelTest.xlsx"));

  //possiby we need data formats
  DataFormat dataFormat = wb.createDataFormat();

  //get sheet and set row number
  XSSFSheet sheet = wb.getSheetAt(0);
  int rownumber = 3;

  //get the date
  String monthEndDate = "31-Dec-17";
  SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yy", java.util.Locale.ENGLISH);
  Date updateDate = sdf.parse(monthEndDate);

  //set date as cell value
  XSSFCell updateDateCell = sheet.getRow(rownumber).getCell(15);
  updateDateCell.setCellValue(updateDate);

  //use CellUtil to set the CellStyleProperties
  Map<String, Object> properties = new HashMap<String, Object>();
  properties.put(CellUtil.DATA_FORMAT, dataFormat.getFormat("dd-MMM-yy"));
  CellUtil.setCellStyleProperties(updateDateCell, properties);

  wb.write(new FileOutputStream("ExcelTestNew.xlsx"));
  wb.close();
 }  
}

0
投票

在代码末尾添加updateDateCell = Format(updateDateCell, "dd-MMM-yyyy")

你应该得到31-Dec-2017


0
投票

这是我已经用于格式化日期的示例,您可以重用它的一部分(我标记了相关的代码行)。如果有任何问题让我知道,它已经过测试并且运行正常。

//更新请参阅Axel Richter的答案https://stackoverflow.com/a/47920182/1053496以获得正确答案。在我的例子中,我将日期存储为String而不是Date对象,这不是推荐的方式

import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.xssf.usermodel.*;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;



public class WriteExcelBasic {
    public static void main(String[] args) throws IOException {

        String excelFileName = "/Users/home/Test3.xls";
        FileOutputStream fos = new FileOutputStream(excelFileName);


        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFCellStyle style = wb.createCellStyle();

        XSSFSheet sheet = wb.createSheet("sheet");
        XSSFFont urlFont = wb.createFont();
        style.setFont(urlFont);
        String monthEndDate = "31-Dec-17";
        DataFormat df = wb.createDataFormat(); //these 3 lines  are enough
         short dateFormat = df.getFormat("dd-MMM-yy"); // 2nd 
        style.setDataFormat(dateFormat); // 3rd


        for (int r = 0; r < 1; r++) {
            XSSFRow row = sheet.createRow(r);
            row.setHeight((short) -1);
            for (int c = 0; c < 3; c++) {
                XSSFCell cell = row.createCell(c);
                String ss = "31-Dec-17";
                cell.setCellValue(ss);
                    style.setWrapText(true);
                cell.setCellStyle(style);
            }
        }

        try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
            wb.write(baos);
            byte[] myByteArray = baos.toByteArray();
            fos.write(myByteArray);
            fos.flush();
        }
        finally {
            wb.close();
            fos.close();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.