如何使用Selenium Web Driver重命名现有的Excel文件为当前日期和时间?

huangapple 未分类评论44阅读模式
英文:

How to rename the Existing Excel file with Current date and time using Selenium web Driver?

问题

Filepath我正在使用,但它没有获取当前日期和时间

public static String FilePath = "C:\\Users\\skumari1\\eclipse-workspace\\CoreLinkAutomation\\src\\main\\java\\com\\corelink\\testData\\PCPAutomation.xlsx" + PrimaryCommonSetDataUTil.Validatedate();

使用以下代码获取当前日期和时间

public static String Validatedate() {
    DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
    Date date = new Date();
    String date1 = dateFormat.format(date);
    return date1;
}

使用以下代码将数据写入Excel文件,但我想要重命名Filepath

public static void write_test_result(String result, String sheet_name, String filePath, int row_num, int col_num) throws EncryptedDocumentException, FileNotFoundException, IOException {
    Workbook wb = WorkbookFactory.create(new FileInputStream(filePath));
    Sheet sh = wb.getSheet(sheet_name);
    
    sh.getRow(row_num).createCell(col_num).setCellValue(result);
    
    wb.write(new FileOutputStream(filePath));
}

我希望在每次项目执行之后,将文件路径重命名为Filepath + 当前日期和时间。

英文:

Filepath i am using but its not taking the current date and time

public static String FilePath="C:\\Users\\skumari1\\eclipse-workspace\\CoreLinkAutomation\\src\\main\\java\\com\\corelink\\testData\\PCPAutomation.xlsx"+ PrimaryCommonSetDataUTil.Validatedate();

For current date and time using this code

public static String Validatedate() {

	DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
	Date date = new Date();
	String date1= dateFormat.format(date);
	// System.out.println("Current date and time is " +date1);
	return date1;
}

For writing data into Excel file using this code but i want to rename the Filepath

public static void write_test_result(String result, String sheet_name, String filePath,int row_num,int col_num) throws EncryptedDocumentException, FileNotFoundException, IOException
{
	Workbook wb = WorkbookFactory.create(new FileInputStream(filePath));
	Sheet sh = wb.getSheet(sheet_name);
	
	sh.getRow(row_num).createCell(col_num).setCellValue(result);
	
	wb.write(new FileOutputStream(filePath));
}

I want after every execution of project the filepath should be renamed as Filepath+Current date and time.

答案1

得分: 0

你使用的日期语法包含了 / 和 : 这在文件名中是无效的。使用一个文件名有效的格式。例如:

DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy_HH_mm_ss");
英文:

The syntax of date you are using contains / and : which is invalid for a file name. Use a format that is valid as a file name. For example:

DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy_HH_mm_ss");

huangapple
  • 本文由 发表于 2020年7月24日 01:54:26
  • 转载请务必保留本文链接:https://java.coder-hub.com/63060323.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定