无法使用java替换excel文件中的日期

时间:2015-02-18 12:55:53

标签: java excel apache date apache-poi

要求

我需要打开excel文件。然后我需要检查日期(2014年12月31日)。如果文件中存在这个,那么我需要替换为11/28/2014。实际上excel文件包含日期。但是在我的代码中永远不会传递这个条件if (df.format(DateUtil.getJavaDate(cell.getNumericCellValue())).equals(df.format(asOfDate))) {

以下是代码:

package excelAsOfDate;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;

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.DateUtil;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelAsOfDate {
    public static void main(String[] args) {
        try {
            DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
            Date asOfDate = df.parse("12/31/2014");
            Date newAsOfDate = df.parse("11/28/2014");

            System.out.println("date as of" + df.format(asOfDate));

            File directory = new File("C://Users//kondeti.venkatarao//Documents//Regresion_sheets//test");
            File[] files = directory.listFiles();

            for (File file : files) {
                if (file.getName().toLowerCase().endsWith(".xlsx")) {
                    FileInputStream fis = new FileInputStream(file.getAbsoluteFile());
                    // Create Workbook instance holding reference to .xlsx file


                    XSSFWorkbook workbook = new XSSFWorkbook(fis);


                    int i = 1;
                    while (i < workbook.getNumberOfSheets()) {

                        // Get first/desired sheet from the workbook
                        XSSFSheet sheet = workbook.getSheetAt(i);

                        // Iterate through each rows one by one
                        Iterator<Row> rowIterator = sheet.iterator();
                        while (rowIterator.hasNext()) {
                            Row row = rowIterator.next();
                            // For each row, iterate through all the columns
                            Iterator<Cell> cellIterator = row.cellIterator();
                            while (cellIterator.hasNext()) {
                                Cell cell = cellIterator.next();
                                // Check the cell type and format accordingly
                                switch (cell.getCellType()) {

                                case Cell.CELL_TYPE_NUMERIC:
                                    if (DateUtil.isCellDateFormatted(cell)) {
                                        if (df.format(DateUtil.getJavaDate(cell.getNumericCellValue())).equals(df.format(asOfDate))) {
                                            // System.out.println(df.format(cell.getDateCellValue()));
                                            System.out.println(df.format(DateUtil.getJavaDate(cell.getNumericCellValue())));                                            

                                            CreationHelper createHelper = workbook.getCreationHelper();
                                            CellStyle cellStyle = workbook.createCellStyle();
                                            cellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
                                            cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
                                            cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("m/d/yy"));
                                            cell.setCellValue(newAsOfDate);
                                            cell.setCellStyle(cellStyle);
                                        }
                                    } 
                                    break;
                                }
                            }
                        }
                        i++;
                        fis.close();
                    }
                    //FileOutputStream fileOut = new FileOutputStream("C://Users//kondeti.venkatarao//Documents//Regresion_sheets//test//final//"+file.getName());
                    FileOutputStream fileOut = new FileOutputStream(file.getAbsoluteFile());
                    workbook.write(fileOut);
                    fileOut.close();
                    fis.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

记录是找到答案的关键。

当我查看Cell类时,我发现有一个名为:

的方法

getDateCellCalue()

当我们看看那个的设定者时:

  

void setCellValue(java.util.Date value)

     

将提供的日期转换为其等效的Excel数值,并将其设置为单元格。

所以你没有比较正确 我的快速建议是:

if (df.format(cell.getDateCellValue()).equals(df.format(asOfDate)))

如果这不起作用,请将df.format(cell.getDateCellValue()生成的内容记录为String

编辑:

您是否也注意到了这一点:

DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date asOfDate = df.parse("12/31/2014");

您的第二个日期是MM/dd/yyyy