public void TestBug56957CloseWorkbook() { FileInfo file = TempFile.CreateTempFile("TestBug56957_", ".xlsx"); //String dateExp = "Sun Nov 09 00:00:00 CET 2014"; DateTime dateExp = LocaleUtil.GetLocaleCalendar(2014, 11, 9); IWorkbook workbook = null; try { // as the file is written to, we make a copy before actually working on it FileHelper.CopyFile(HSSFTestDataSamples.GetSampleFile("56957.xlsx"), file); Assert.IsTrue(file.Exists); // read-only mode works! workbook = WorkbookFactory.Create(OPCPackage.Open(file, PackageAccess.READ)); DateTime dateAct = workbook.GetSheetAt(0).GetRow(0).GetCell(0, MissingCellPolicy.CREATE_NULL_AS_BLANK).DateCellValue; Assert.AreEqual(dateExp, dateAct); workbook.Close(); workbook = null; workbook = WorkbookFactory.Create(OPCPackage.Open(file, PackageAccess.READ)); dateAct = workbook.GetSheetAt(0).GetRow(0).GetCell(0, MissingCellPolicy.CREATE_NULL_AS_BLANK).DateCellValue; Assert.AreEqual(dateExp, dateAct); workbook.Close(); workbook = null; // now check read/write mode workbook = WorkbookFactory.Create(OPCPackage.Open(file, PackageAccess.READ_WRITE)); dateAct = workbook.GetSheetAt(0).GetRow(0).GetCell(0, MissingCellPolicy.CREATE_NULL_AS_BLANK).DateCellValue; Assert.AreEqual(dateExp, dateAct); workbook.Close(); workbook = null; workbook = WorkbookFactory.Create(OPCPackage.Open(file, PackageAccess.READ_WRITE)); dateAct = workbook.GetSheetAt(0).GetRow(0).GetCell(0, MissingCellPolicy.CREATE_NULL_AS_BLANK).DateCellValue; Assert.AreEqual(dateExp, dateAct); workbook.Close(); workbook = null; } finally { if (workbook != null) { workbook.Close(); } Assert.IsTrue(file.Exists); file.Delete(); file.Refresh(); Assert.IsTrue(!file.Exists); } }
public void GetJavaDate_ValidValue() { double dateValue = 0; TimeZone tz = LocaleUtil.GetUserTimeZone(); bool use1904windowing = false; bool roundSeconds = false; DateTime calendar = LocaleUtil.GetLocaleCalendar(1900, 1, 0); //Date date = calendar.GetTime(); DateTime date = calendar; Assert.AreEqual(date, DateUtil.GetJavaDate(dateValue)); Assert.AreEqual(date, DateUtil.GetJavaDate(dateValue, tz)); Assert.AreEqual(date, DateUtil.GetJavaDate(dateValue, use1904windowing)); Assert.AreEqual(date, DateUtil.GetJavaDate(dateValue, use1904windowing, tz)); Assert.AreEqual(date, DateUtil.GetJavaDate(dateValue, use1904windowing, tz, roundSeconds)); }
public void GetJavaCalendar_ValidValue() { double dateValue = 0; TimeZone tz = LocaleUtil.GetUserTimeZone(); bool use1904windowing = false; bool roundSeconds = false; DateTime expCal = LocaleUtil.GetLocaleCalendar(1900, 1, 0); DateTime[] actCal = { DateUtil.GetJavaCalendar(dateValue), DateUtil.GetJavaCalendar(dateValue, use1904windowing), DateUtil.GetJavaCalendar(dateValue, use1904windowing, tz), DateUtil.GetJavaCalendar(dateValue, use1904windowing, tz, roundSeconds) }; Assert.AreEqual(expCal, actCal[0]); Assert.AreEqual(expCal, actCal[1]); Assert.AreEqual(expCal, actCal[2]); Assert.AreEqual(expCal, actCal[3]); }
public static DateTime GetJavaCalendar(double date, bool use1904windowing, TimeZone timeZone, bool roundSeconds) { if (!IsValidExcelDate(date)) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Invalid Excel date double value: {0}", date)); } int wholeDays = (int)Math.Floor(date); int millisecondsInDay = (int)((date - wholeDays) * DAY_MILLISECONDS + 0.5); DateTime calendar; if (timeZone != null) { calendar = LocaleUtil.GetLocaleCalendar(timeZone); } else { calendar = LocaleUtil.GetLocaleCalendar(); // using default time-zone } calendar = SetCalendar(wholeDays, millisecondsInDay, use1904windowing, roundSeconds); return(calendar); }