JXL datetime no need the part time - excel

Hello i wrote this code to add a Date in excel but when the cell is added he show also the time. I want eliminated the time part. Thank you in advance if someone can help ..
Tabla[tabReg][tabCol]); is String Array
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date convertedDate = dateFormat.parse(Tabla[tabReg][tabCol]);
DateFormat df = new DateFormat("yyyy/MM/dd");
WritableCellFormat wdf = new WritableCellFormat(df);
cf = new WritableCellFormat(df);
cell = new jxl.write.DateTime(exCol,exReg, convertedDate);
cell.setCellFormat(wdf);
sheet2.addCell(cell);

The solution was to put the format when you create de cell.
cell = new jxl.write.DateTime(exCol,exReg, convertedDate,wdf);

Related

EPPlus Date Formatting in Excel

I need to format the date in Excel while writing in the Excel sheet.
It should display this format "MM/dd" but when click on this then formula bar show the complete date "MM/dd/yyyy".
For reference I have attached the screenshot as well. Please check and suggest how I can achieve this in EPPlus Code.
I have tried the following code but no luck:
namedWorksheet.Cells["E4"].Style.Numberformat.Format = "M/dd/yyyy";
namedWorksheet.Cells["E4"].Value = DateTime.Now.ToString("MM/dd/yyyy").Substring(0,4);
Here is the solution for this issue.
Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-NL")
{
DateTimeFormat = { YearMonthPattern = "MM/dd" }
};
namedWorksheet.Cells["G4"].Style.Numberformat.Format = DateTimeFormatInfo.CurrentInfo.YearMonthPattern;
var dateAndTime = DateTime.Now;
var date = dateAndTime.Date;
namedWorksheet.Cells["G4"].Value = date;

Am unable to read the date field from excel file selenium reads the date as Col=9=43070 Col=10=43070 Col=11=42931 Col=12=43296

Selenium is incorrectly reading the date as 43095 when I enter 26-12-2017. How to get Selenium to read the correct date?
for (int i=0;i<=TcRow;i++)
{
for (int j=0;j<TcCol;j++)
{
Cell Cell=TcSheet.getRow(i).getCell(j);
}
}
Am I reading the format incorrectly?
TcSheet.getRow(i).getCell(j).setCellType(Cell.CELL_TYPE_STRING);
What changes do I need to do here to make sure they read both the string and the date field?
data[i][j]=TcSheet.getRow(i).getCell(j).getStringCellValue();
}
I also faced the same issue during reading the excel file where I'm fetching date is formatted in dd/mm/yyyy format and selenium fetching wrong value.
For that, I have used DataFormatter. It will returns Excel cell value with format e.g. Date format 15-04-208 in excellent then it will returns date with same format. Look below code that I used in my Framework. Hope it will also work for you.
FileInputStream fis = new FileInputStream("path\\to\\file.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(worksheet);
DataFormatter formatter = new DataFormatter();
Cell cell = sheet.getRow(rowNum).getCell(cellNum);
String cellValue = formatter.formatCellValue(cell);
System.out.println(cellValue);
return cellValue;
Let me know if you have any query.

In C#, Excel cell's custom date format not working with EPPlus

I'm having trouble with Excel's custom format using EPPLUS. Here's my code:
var destFile = new FileInfo(#"C:\temp\test1.xlsx");
var fileName = "test1";
using (ExcelPackage pck = new ExcelPackage(destFile))
{
pck.Workbook.Worksheets.Add(fileName); // Create the worksheet in package
pck.Workbook.Worksheets[fileName].Cells["A2"].Value = DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss");
pck.Workbook.Worksheets[fileName].Cells["A2"].Style.Numberformat.Format = "d-mmm-yy";
pck.Save();
}
I'm getting the following:
The custom format is showed right, but the value in the cell doesn't display the format needed. Here's what I'm trying to get:
Note: I need the full date value DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") for other files, but the custom format is all I need for this file.
What do I need to change to get this to work?
Thanks to #PaullAbbott, here's the correct answer:
pck.Workbook.Worksheets[fileName].Cells["A2"].Value = DateTime.Now;
This displays the results that I needed.

DateTime is rounded up to the next day using ExcelLibrary

The datetime I'm writing to Excel always get rounded up to the next day:
workSheet.Cells[0, 0] = new Cell(DateTime.Now, new CellFormat(CellFormatType.DateTime, #"HH:mm:ss"));
In the output Excel file the cell gets this value: 29/09/2013 00:00:00
The DateTime.Now from this example is 28/09/2013 19:42:23
I ended up passing the cell value as a string instead of as a DateTime:
workSheet.Cells[0, 0] = new Cell(DateTime.Now.ToString(#"HH:mm:ss:ff"),
new CellFormat(CellFormatType.DateTime, #"HH:mm:ss"));
If you are using the ExcelLibrary Project Source Code, you can fix this by:
Go to SharedResource Class in this location: [Project Source Code folder]\Office\BinaryFileFormat folder
Change the EncodeDateTime function as below:
public double EncodeDateTime(DateTime value)
{
double days = (value - BaseDate).Days;
//if (days > 365) days++;
return days;
}
Pass the DataTime object to the Cell with the prefered format:
worksheet.Cells[iIndex, j] = new Cell(((DateTime)cellValue), new CellFormat(CellFormatType.DateTime, #"dd/MM/yyyy"));
You need to convert the date format from OLE Automation to the .net format by using DateTime.FromOADate.
If oCell.Format.FormatType = CellFormatType.Date OrElse oCell.Format.FormatType = CellFormatType.DateTime Then
Dim d As Double = Double.Parse(oCell.Value)
Debug.print(DateTime.FromOADate(d))
End If

Unable to fetch the exact date value from excel cell

I am fetching the value present in the excel cell which is a date like 20-3-2004 using the following code:
string logResult = null;
string ResultFilePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + ResultFile;
Microsoft.Office.Interop.Excel.Application myapp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook wb = myapp.Workbooks.Open(ResultFilePath);
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.get_Item(1);
var cell = (Microsoft.Office.Interop.Excel.Range)sheet.Cells[row, col];
logResult = cell.Value.ToString();
but in the logresult I am always getting date in format 20/3/2004 . please suggest how get the exact format of the date which is written in the cell.
A date value has no format. It is the code that you use to display it that present that value is some form on video.
In your case, it is the ToString() call that trasform whatever has been read on the cell in a string. If it is a date then it trasform it according to the current international settings of your machine.
You could force the ToString() method to use a particular format applying a Format Mask parameter like
logResult = cell.Value.ToString("d-M-yyyy");
See the topic on MSDN about DateTime.ToString()

Resources