Я ввожу DateTime.Now.ToUniversalTime().ToLongTimeString()
в ячейку листа Excel и закрываю ее после сохранения, но при повторном открытии того же листа Excel и попытке извлечь значение ячейки это дает мне некоторое двойное значение.
Мой код C #:
В конструкторе: -
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet, xlWorkSheet2, xlWorkSheet3;
object misValue = System.Reflection.Missing.Value;
Excel.Range range;
xlWorkBook = xlApp.Workbooks.Open(
"Book1.xls",
0,
true,
misValue,
"",
"",
true,
Excel.XlPlatform.xlWindows,
"\t",
false,
false,
0,
true,
1,
0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
((Excel.Range)xlWorkSheet.Cells[1, 5]).EntireColumn.NumberFormat = "H:mm:ss";
xlWorkSheet.Cells[(rowcnt + 1), 5] = DateTime.Now.ToUniversalTime().ToLongTimeString();
xlWorkBook.SaveAs("Book2.xls",Excel.XlFileFormat.xlOpenXMLWorkbook);
//Close the Excel Workbook after saving a copy of it.
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
Marshal.ReleaseComObject(xlWorkSheet);
Marshal.ReleaseComObject(xlWorkBook);
Marshal.ReleaseComObject(xlApp);
xlWorkBook = null;
//Make the Excel Application null.
xlApp = null;
и затем вызов метода ниже, чтобы получить значение ячейки.
private void AddXMLDetail()
{
try
{
xlApp = new Excel.ApplicationClass();
#region Open the Excel File
//Assigning the saved changes workbook present in
//"My Document" to add the xml detail in excel sheet.
xlWorkBook =
xlApp.Workbooks.Open(
"Book2.xls",
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing,
Type.Missing);
#endregion Open the Excel File.
//Assigning Sheet 1.
xlWorkSheet =
(Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
string compareTime =
((Excel.Range)range.Cells[1, 5]).Value2.ToString();
Console.WriteLine("The saved cell value is : " + compareTime);
#region Close and Release the object.
//Close the Excel Workbook after saving a copy of it.
xlWorkBook.Close(true, misValue, misValue);
Marshal.ReleaseComObject(xlWorkSheet);
Marshal.ReleaseComObject(xlWorkBook);
xlWorkBook = null;
//Quit The Excel Application after success completion of work.
xlApp.Quit();
//Release the Excel Application for reuse.
Marshal.ReleaseComObject(xlApp);
//Make the Excel Application null.
xlApp = null;
#endregion Close and Release the object.
}
catch (Exception ex)
{
Console.WriteLine("exception was : " + ex);
}
}
Может кто-нибудь сказать мне, где я делаю неправильно? Любой ответ будет оценен.