[英]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 . 当我将导出的Excel文件另存为CSV或文本文件时,秒数在
DateTime
数据中会丢失。 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
. 例如,如果在Excel中显示
7/10/2019 2:01:39 PM
,并且将其另存为CSV,则当打开CSV时,我看到的是7/10/2019 2:01:00 PM
。
I am using Office.Interop.Excel
to save an opened Excel file to CSV. 我正在使用
Office.Interop.Excel
将打开的Excel文件保存为CSV。 And then opening the CSV file. 然后打开CSV文件。
This behavior is same if I save Excel file as a txt and then open txt file. 如果我将Excel文件另存为txt,然后打开txt文件,此行为是相同的。
How can I preserve seconds when I save an excel file as csv or txt? 将Excel文件另存为csv或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下午2:01:39
7/10/2019 1:12:11 PM
2019/7/10下午1:12:11
7/10/2019 12:44:58 PM
7/10/2019下午12:44:58
_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);
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. 请注意,对
SaveAs
的调用将保存在默认文件夹中,该文件夹对我来说是我的文档文件夹,而不是执行目录。
_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. 在另存为csv之前,我必须格式化列。 I just needed to add this
我只需要添加这个
worksheet.Columns["B"].NumberFormat = "mm/dd/yyyy hh:mm:ss";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.