简体   繁体   中英

preserve seconds when saving excel to csv/txt

When I am saving an exported Excel file as a CSV or text file, the seconds are lost in DateTime data . For example, if 7/10/2019 2:01:39 PM is shown in Excel, and I save it as a CSV, when I open the CSV, I am seeing 7/10/2019 2:01:00 PM .

I am using Office.Interop.Excel to save an opened Excel file to CSV. And then opening the CSV file.

This behavior is same if I save Excel file as a txt and then open txt file.

How can I preserve seconds when I save an excel file as csv or txt?

Here is what I have so far:

excel = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application") as Excel.Application;

//for csv save
excel.ActiveWorkbook.SaveAs(toFile, Excel.XlFileFormat.xlCSVWindows); 

//for txt save
excel.ActiveWorkbook.SaveAs(toFile, FileFormat: Excel.XlFileFormat.xlUnicodeText,
    AccessMode: Excel.XlSaveAsAccessMode.xlNoChange,
    ConflictResolution: Excel.XlSaveConflictResolution.xlLocalSessionChanges);

I was able to reproduce the issue by following the steps outlined below. My proposed solution is at the end of the answer, but I am adding the steps I used to reproduce the issue for completeness. The examples I used are as follows:

7/10/2019 2:01:39 PM

7/10/2019 1:12:11 PM

7/10/2019 12:44:58 PM

  1. Create new Excel document and add three example dates as shown below. This is using the default date format from pasting the examples provided in the comments above.

示例Excel数据

  1. Run the following code to save as a CSV and text file

_Application excel = (_Application)Marshal.GetActiveObject("Excel.Application");
excel.ActiveWorkbook.SaveAs("test.csv", XlFileFormat.xlCSVWindows);
excel.ActiveWorkbook.SaveAs("test.txt", XlFileFormat.xlUnicodeText, XlSaveAsAccessMode.xlNoChange, XlSaveConflictResolution.xlLocalSessionChanges);
  1. Output showing the result the OP was getting

输出结果


The solution I found was to update the formatting of the cells before exporting. For instance, if all the dates are in the first row, the following will change the formatting to display the date as desired.

Range range = excel.Cells[1,1];
range.EntireColumn.NumberFormat = "MM/DD/YYYY hh:mm:ss";

The full solution . Note the call to SaveAs will save in the default folder which for me was my documents folder, not the executing directory.

_Application excel = (_Application)Marshal.GetActiveObject("Excel.Application");

Range range = excel.Cells[1,1];
range.EntireColumn.NumberFormat = "MM/DD/YYYY hh:mm:ss";

excel.ActiveWorkbook.SaveAs("test.csv", XlFileFormat.xlCSVWindows);
excel.ActiveWorkbook.SaveAs("test.txt", XlFileFormat.xlUnicodeText, XlSaveAsAccessMode.xlNoChange, XlSaveConflictResolution.xlLocalSessionChanges);

I had to format column before I save as csv. I just needed to add this

worksheet.Columns["B"].NumberFormat = "mm/dd/yyyy hh:mm:ss";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM