Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.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/2/image-processing/2.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
Excel POI-XSSF:用户定义的数据格式_Excel_Apache Poi_Xssf - Fatal编程技术网

Excel POI-XSSF:用户定义的数据格式

Excel POI-XSSF:用户定义的数据格式,excel,apache-poi,xssf,Excel,Apache Poi,Xssf,我正在读一个excel,它似乎有用户定义的数据格式。 例如,它有一种数据格式:“yyyy”E,它只以yyy格式显示日期,后跟字母E 现在,此格式不是内置的可用格式的一部分 因此,当我尝试将其设置为CellStyle的DataFormat时,它似乎不起作用 首先,我阅读了一本工作簿,然后在此工作簿中创建了DataFormat XSSFWorkbook wb = new XSSFWorkbook(excelToRead); XSSFDataFormat df = wb.createDataForma

我正在读一个excel,它似乎有
用户定义的数据格式
。 例如,它有一种数据格式:
“yyyy”E
,它只以
yyy
格式显示日期,后跟字母E

现在,此格式不是内置的可用格式的一部分

因此,当我尝试将其设置为
CellStyle
DataFormat
时,它似乎不起作用

首先,我阅读了一本工作簿,然后在此工作簿中创建了
DataFormat

XSSFWorkbook wb = new XSSFWorkbook(excelToRead);
XSSFDataFormat df = wb.createDataFormat();
df
对象现在也具有
用户定义的数据格式
(通过打印到200检查)。 现在,我尝试在同一工作簿中创建一个新单元格,使用如下新样式:

XSSFCell cellTemp = row.createCell(0);
XSSFCellStyle styleTemp = wb.createCellStyle();
styleTemp.setDataFormat(df.getFormat(cell.getCellStyle().getDataFormatString()));
cellTemp.setCellStyle(styleTemp);
cellTemp.setCellValue(cell.getStringCellValue());
System.out.print(formatter.formatCellValue(cellTemp)+" ");
但是,格式化程序没有给我正确的字符串值,而是给了我日期的基本整数值(如果格式无法识别,我猜这是默认值)

在XSSF中使用用户定义格式的正确方法是什么


更新:我似乎能够使用其他用户定义的格式,如
yyyy
0.0%
,但不能使用
yyyy“E”或
yyyy“A”
,我不太清楚您到底想实现什么。但是如果
DataFormatter
格式不正确,那么也可以使用从 .

例如:

import java.io.*;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.format.*;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.awt.Desktop;
import java.util.Date;

class CustomDateFormatTest {

 public static void main(String[] args) {
  try {

   Workbook wb = new XSSFWorkbook();
   Sheet sheet = wb.createSheet("Sheet1");
   Row row = sheet.createRow(0);
   Cell cell = row.createCell(0);

   cell.setCellValue(new Date());

   DataFormat format = wb.createDataFormat();
   CellStyle cellstyle = wb.createCellStyle();
   cellstyle.setDataFormat(format.getFormat("yyyy\"E\""));

   cell.setCellStyle(cellstyle);

   OutputStream out = new FileOutputStream("CustomDateFormatTest.xlsx");
   wb.write(out);
   wb.close();

   System.out.println("Done");
   File outputfile = new File("CustomDateFormatTest.xlsx");
   Desktop.getDesktop().open(outputfile);

   InputStream inp = new FileInputStream("CustomDateFormatTest.xlsx");
   wb = WorkbookFactory.create(inp);

   sheet = wb.getSheetAt(0);
   row = sheet.getRow(0);
   cell = row.getCell(0);

   //This will not format properly
   DataFormatter dataformatter = new DataFormatter();
   System.out.println(dataformatter.formatCellValue(cell));

   //This will format properly
   String formatstring = cell.getCellStyle().getDataFormatString();
   System.out.println(formatstring);
   CellDateFormatter celldateformatter = new CellDateFormatter(formatstring);
   //For using CellDateFormatter we need to know that the cell value is a Date.
   System.out.println(celldateformatter.format(cell.getDateCellValue()));


  } catch (InvalidFormatException ifex) {
  } catch (FileNotFoundException fnfex) {
  } catch (IOException ioex) {
  }
 }
}

这确实奏效了!谢谢。。。你知道为什么其他的用户格式在工作,而这个(yyyy“E”)格式不工作吗?