2011-04-26 148 views
68

我一直在使用Apache POI一段时间以编程方式读取现有的Excel 2003文件。现在我有了一个新的需求,即在内存中创建完整的.xls文件(仍然使用Apache POI),然后在最后将它们写入文件。以我的方式站在唯一的问题是处理与日期单元格。POI - 如何将单元格值设置为日期并应用默认的Excel日期格式?

考虑下面的代码:

Date myDate = new Date(); 
HSSFCell myCell; 
// code that assigns a cell from an HSSFSheet to 'myCell' would go here... 
myCell.setCellValue(myDate); 

当我写包含此单元格的工作簿到一个文件,并用Excel打开它,细胞显示为数字。是的,我确实意识到Excel会将它的'日期'存储为1900年1月1日以来的天数,这就是单元格中的数字。

问题:我可以在POI中使用哪些API调用来告诉它我希望将默认日期格式应用于我的日期单元格?

理想情况下,我希望电子表格单元格以与Excel分配的默认日期格式显示,如果用户在Excel中手动打开电子表格并键入Excel识别为日期的单元格值。

回答

112

http://poi.apache.org/spreadsheet/quick-guide.html#CreateDateCells

CellStyle cellStyle = wb.createCellStyle(); 
CreationHelper createHelper = wb.getCreationHelper(); 
cellStyle.setDataFormat(
    createHelper.createDataFormat().getFormat("m/d/yy h:mm")); 
cell = row.createCell(1); 
cell.setCellValue(new Date()); 
cell.setCellStyle(cellStyle); 
+15

感谢忍者,这个作品为了我。对其他需要这样做的人的评论。有一个名为“BuiltinFormats”的POI类,列出了Excel知道的所有标准格式(不仅仅是日期格式)。我坚持使用其中的一个作为我上面代码片段中显示的'getFormat()'方法的参数。 – 2011-04-27 12:08:19

+0

重要的部分在链接的评论中:我们将第二个单元格定为日期(和时间)。从工作簿中创建新的单元格样式非常重要,否则您最终可能会修改内置样式并且不仅影响此单元格而且会影响其他单元格。 – CGK 2013-09-24 13:27:48

+0

谢谢,@ninja。你知道为什么需要'getCreationHelper()'吗?我现在正在使用Mule生成一个Excel输出文件,实际上我可以在不使用创建助手的情况下使用'createDataFormat()'并在我的测试中生成一个Excel文件。没有使用它有缺点吗?谢谢! – Rashiki 2017-03-28 15:23:39

11

这个例子是与.xlsx文件类型的工作。此示例来自用于创建.xslx电子表格的.jsp页面。

import org.apache.poi.xssf.usermodel.*; //import needed 

XSSFWorkbook wb = new XSSFWorkbook(); // Create workbook 
XSSFSheet sheet = wb.createSheet();  // Create spreadsheet in workbook 
XSSFRow row = sheet.createRow(rowIndex); // Create the row in the spreadsheet 


//1. Create the date cell style 
XSSFCreationHelper createHelper = wb.getCreationHelper(); 
XSSFCellStyle cellStyle   = wb.createCellStyle(); 
cellStyle.setDataFormat(
createHelper.createDataFormat().getFormat("MMMM dd, yyyy")); 

//2. Apply the Date cell style to a cell 

//This example sets the first cell in the row using the date cell style 
cell = row.createCell(0); 
cell.setCellValue(new Date()); 
cell.setCellStyle(cellStyle); 
0

此代码示例可用于更改日期格式。在这里我想从yyyy-MM-dd改为dd-MM-yyyy。这里pos是列的位置。

import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.CellStyle; 
import org.apache.poi.ss.usermodel.CreationHelper; 
import org.apache.poi.ss.usermodel.Row; 
import org.apache.poi.xssf.usermodel.XSSFCellStyle; 
import org.apache.poi.xssf.usermodel.XSSFColor; 
import org.apache.poi.xssf.usermodel.XSSFFont; 
import org.apache.poi.xssf.usermodel.XSSFSheet; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 

class Test{ 
public static void main(String[] args) 
{ 
String input="D:\\somefolder\\somefile.xlsx"; 
String output="D:\\somefolder\\someoutfile.xlsx" 
FileInputStream file = new FileInputStream(new File(input)); 
XSSFWorkbook workbook = new XSSFWorkbook(file); 
XSSFSheet sheet = workbook.getSheetAt(0); 
Iterator<Row> iterator = sheet.iterator(); 
Cell cell = null; 
Row row=null; 
row=iterator.next(); 
int pos=5; // 5th column is date. 
while(iterator.hasNext()) 
{ 
    row=iterator.next(); 

    cell=row.getCell(pos-1); 
    //CellStyle cellStyle = wb.createCellStyle(); 
    XSSFCellStyle cellStyle = (XSSFCellStyle)cell.getCellStyle(); 
    CreationHelper createHelper = wb.getCreationHelper(); 
    cellStyle.setDataFormat(
     createHelper.createDataFormat().getFormat("dd-MM-yyyy")); 
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
    Date d=null; 
    try { 
     d= sdf.parse(cell.getStringCellValue()); 
    } catch (ParseException e) { 
     // TODO Auto-generated catch block 
     d=null; 
     e.printStackTrace(); 
     continue; 
    } 
    cell.setCellValue(d); 
    cell.setCellStyle(cellStyle); 
    } 

file.close(); 
FileOutputStream outFile =new FileOutputStream(new File(output)); 
workbook.write(outFile); 
workbook.close(); 
outFile.close(); 
}} 
6

要设置为默认的Excel类型日期(默认为OS级别的区域设置/ - >即XLSX会寻找不同的,当由德国或英国人开/并用星号标记,如果你在Excel的单元格的格式选择它选择器)你应该:

CellStyle cellStyle = xssfWorkbook.createCellStyle(); 
    cellStyle.setDataFormat((short)14); 
    cell.setCellStyle(cellStyle); 

我用xlsx做了它,它工作正常。

+1

最佳答案,无法理解为什么downvoted。 – fiffy 2017-10-02 08:09:49

1

我在这里写我的答案,因为它可能对其他读者有所帮助,其他读者可能与此处的提问者略有不同。

我准备一个.xlsx模板;所有将填充日期的单元格都已格式化为日期单元格(使用Excel)。

我使用Apache POI打开.xlsx模板,然后将日期写入单元格,并且工作正常。

在下面的示例中,单元格A1已经在格式为[$-409]mmm yyyy的Excel中格式化,并且Java代码仅用于填充单元格。

FileInputStream inputStream = new FileInputStream(new File("Path to .xlsx template")); 
Workbook wb = new XSSFWorkbook(inputStream); 
Date date1=new Date(); 
Sheet xlsMainTable = (Sheet) wb.getSheetAt(0); 
Row myRow= CellUtil.getRow(0, xlsMainTable); 
CellUtil.getCell(myRow, 0).setCellValue(date1); 

当Excel打开时,日期格式正确。

0

要知道Excel使用的格式字符串,而不必猜测它:创建一个Excel文件,在单元格A1中写入一个日期并根据需要进行格式化。然后运行下面几行:

FileInputStream fileIn = new FileInputStream("test.xlsx"); 
Workbook workbook = WorkbookFactory.create(fileIn); 
CellStyle cellStyle = workbook.getSheetAt(0).getRow(0).getCell(0).getCellStyle(); 
String styleString = cellStyle.getDataFormatString(); 
System.out.println(styleString); 

然后将生成的字符串复制粘贴,删除反斜杠(例如d/m/yy\ h\.mm;@变得d/m/yy h.mm;@)和http://poi.apache.org/spreadsheet/quick-guide.html#CreateDateCells代码中使用它:

CellStyle cellStyle = wb.createCellStyle(); 
CreationHelper createHelper = wb.getCreationHelper(); 
cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("d/m/yy h.mm;@")); 
cell = row.createCell(1); 
cell.setCellValue(new Date()); 
cell.setCellStyle(cellStyle);