DateTime is rounded up to the next day using ExcelLibrary - excel

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

Related

Flutter/Dart: Excel Date to Date Object

I am parsing an excel document in excel using the excel: ^1.1.5 package. In my sheet, i have a Date column and this Date is being received in my Flutter code in the following format
"44663"
rather than:
"2022/04/12"
How do I parse this to a format such as YY-MM-DD.
I have tried DateTime.parse(), but it throws an error that my date format is invalid.
I found the answer :
const gsDateBase = 2209161600 / 86400;
const gsDateFactor = 86400000;
final date = double.tryParse("44663");
if (date == null) return null;
final millis = (date - gsDateBase) * gsDateFactor;
print(DateTime.fromMillisecondsSinceEpoch(millis.toInt(), isUtc: true));

Getting the 1st date of the current and previous month in U-SQL

How to get in U-SQL:
the 1st date of the current month
the 1st date of the previous month?
If I was using SQL I would write the following query (any idea how to write it in U-SQL?):
WHERE MyDate BETWEEN DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0) AND DATEADD(MONTH, DATEDIFF(MONTH, 0, getdate()), 0)
You can use C# expressions for that:
DECLARE #startDayThisMonth DateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
DECLARE #startDayPreviousMonth DateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month - 1, 1);
#data =
SELECT
x
FROM
y
WHERE
MyDate BETWEEN #startDate AND #endDate;
Examples can be found at this site.

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.

Apache POI to excel - Zero Date

Please make sure you understand my problem before replying, it is not as simple as it looks. Please don't just do a google search and post the link to the results; I already looked.
I have a VB.Net application that we are replacing with a Java application. The purpose of the application is to write an excel sheet (.xls). The file is then sent over to a second party and they process the data in it. I am using the APACHE POI to write the file.
The final product is being rejected by the second party because two time fields are "not valid". After scratching my head for a while, I noticed that Java produced file and VB.Net produced file are handling 0 date values differently. Let's say the time is suppose to be 3:30 PM in military time, the data appears as 15:30 on both files. The problem is the date portion of the field:
VB.Net generated: 1/0/1900 15:30
Java generated: 1/1/1970 15:30
I can't seem to find a way to have the apache POI mimic the way excel handles 0 dates. The following are some of the things I tried.
I set my date/time variable in the java application as 1/0/1900 15:30. This gives me an error in the application.
I set my variable as a string and pass it to the worksheet and then set the format of the cell. I don't get an error, but the data stays as 'general' until I double click on the cell and press Enter. This process is suppose to be automated so this is not an option.
I set the formula of the cell to =TIMEVALUE("15:30"), but this was not accepted by the 2nd party.
Has anyone else ran into this problem? Can anyone think of a way around this? Having the second party change the way they read the file is not an option.
What you need to know is that Excel stores datetime values as floating point double values. There 0 = 00:00:00 and 1 = 24:00:00 = 01/01/1900 00:00:00. Also 0.5 = 12:00:00 and 1.5 = 36:00:00 = 01/01/1900 12:00:00. So in other words, Excels datetime values are starting with 0 and 1 is one day and is 01/01/1900. Also 1/24 is one hour, 1/24/60 is one minute and 1/24/60/60 is one second.
The problem using a Java Date is that the months in Calendar constructors are 0 based. So month 0 is January and new GregorianCalendar(1900, 0, 1, 15, 30, 0) will be 01/01/1900 15:30:00. And there is not a day 0, so new GregorianCalendar(1900, 0, 0, 15, 30, 0) will be 12/31/1899 15:30:00 and this will be -1 for Excel.
Because the problems with Excels date behavior are known, apache poi provides DateUtil.
Using this we can do:
import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.util.Calendar;
import java.util.GregorianCalendar;
class XSSFNullDateTest {
public static void main(String[] args) {
try {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("Sheet1");
CreationHelper creationHelper = wb.getCreationHelper();
CellStyle cellStyleTime = wb.createCellStyle();
cellStyleTime.setDataFormat(creationHelper.createDataFormat().getFormat("hh:mm:ss"));
//using a Calendar:
Calendar calendar = new GregorianCalendar(1900, 0, 1, 15, 30, 0);
System.out.println(calendar.getTime()); //01/01/1900 15:30:00
double doubleTime = DateUtil.getExcelDate(calendar, false);
System.out.println(doubleTime); //1.6458333333333335
Cell cell = sheet.createRow(0).createCell(0);
cell.setCellValue(doubleTime-1); //subtract 1 so we have day 0
cell = sheet.getRow(0).createCell(1);
cell.setCellValue(doubleTime-1); //subtract 1 so we have day 0
cell.setCellStyle(cellStyleTime);
//using a string:
doubleTime = DateUtil.convertTime("15:30:00");
System.out.println(doubleTime); //0.6458333333333334 = day 0 already
cell = sheet.createRow(1).createCell(0);
cell.setCellValue(doubleTime);
cell = sheet.getRow(1).createCell(1);
cell.setCellValue(doubleTime);
cell.setCellStyle(cellStyleTime);
OutputStream out = new FileOutputStream("XSSFNullDateTest.xlsx");
wb.write(out);
wb.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

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