简体   繁体   English

C#防止excel自动更改我的日期

[英]C# Preventing excel from automtically changing my numbers to date

I am currently trying to convert XLS files into CSV files using C# without using any excel library. 我目前正在尝试使用C#将XLS文件转换为CSV文件,而不使用任何excel库。 I have managed to convert it from XLS to CSV but I am currently facing a problem where the row which contains numbers is being automatically converted into a datetime format as the first cell above is a datetime. 我已经设法将其从XLS转换为CSV,但是我目前面临一个问题,其中包含数字的行会自动转换为datetime格式,因为上面的第一个单元格是datetime。 I cannot change the format of the original file. 我无法更改原始文件的格式。

Here is my code use for conversion of xls to csv: 这是我的代码,用于将xls转换为csv:

protected static void convertExcelToCsv(string inputFile, string outputFile)
    {

        int worksheetNumber = 1;

        var cnnStr = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;IMEX=1;HDR=NO\"", inputFile);
        var cnn = new OleDbConnection(cnnStr);

        var dt = new DataTable();

        cnn.Open();
        var schemaTable = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
        string worksheet = schemaTable.Rows[worksheetNumber - 1]["table_name"].ToString().Replace("'", "");
        string sql = String.Format("SELECT * FROM [{0}]", worksheet);
        var da = new OleDbDataAdapter(sql, cnn);



        da.Fill(dt);

        cnn.Close();

        using (var wtr = new StreamWriter(outputFile))
        {
            foreach (DataRow row in dt.Rows)
            {


                bool firstLine = true;
                foreach (DataColumn col in dt.Columns)
                {
                    if (!firstLine) { wtr.Write(","); } else { firstLine = false; }
                    var data = row[col.ColumnName].ToString().Replace(",", "");
                    wtr.Write(data);
                }
                wtr.WriteLine();
            }
        }


    }

The problem 问题

In IMEX mode, the driver looks at the first few rows (8 by default) to determine the format of the column. 在IMEX模式下,驱动程序查看前几行(默认为8行)以确定列的格式。 If the column is "intermixed," it defaults to text. 如果列是“混合的”,则默认为文本。 It sounds like the non-date data don't appear until lower down in the file. 听起来非日期数据要等到文件中的较低位置才出现。

Solution #1 解决方案1

You can increase the number of rows that it looks ahead by modifying the registry: 您可以通过修改注册表来增加它向前看的行数:

HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Jet\\4.0\\Engines\\Excel\\TypeGuessRows

Solution #2 解决方案#2

If you wind up with a DateTime and you wish to convert it to the integer value, you can use 如果最后使用DateTime并将其转换为整数值,则可以使用

readonly DateTime baseDate = new DateTime(1899,12,31);  //This is constant

var d = /* DateTime from Excel */
int originalValue = d.Subtract(baseDate).TotalDays;

Solution #3 解决方案#3

Ditch the driver. 抛弃驾驶员。 Use Excel interop instead. 请改用Excel互操作 Then you can read the underlying cell values, the formatted values, or whatever you want. 然后,您可以读取基础单元格值,格式化值或所需的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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