簡體   English   中英

無法使用Java替換Excel文件中的日期

[英]unable to replace the date in the excel file using java

需求

我需要打開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();
        }
    }
}

日志記錄是找到答案的關鍵。

當我查看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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM