有没有人碰巧知道,如果有一个令牌,我可以添加到我的csv的某个字段,这样Excel就不会试图将它转换为日期?

我试图从我的应用程序中编写一个.csv文件,其中一个值碰巧看起来足够像一个日期,Excel会自动将它从文本转换为日期。我曾尝试将所有文本字段(包括看起来像日期的文本字段)放在双引号内,但没有效果。


当前回答

最简单的解决方案 我今天才想出来的。

用Word打开 用连字符替换所有连字符 保存并关闭 在Excel中打开

一旦你完成编辑,你可以再次在Word中打开它,再次用连字符替换破折号。

其他回答

None of the solutions offered here is a good solution. It may work for individual cases, but only if you're in control of the final display. Take my example: my work produces list of products they sell to retail. This is in CSV format and contain part-codes, some of them start with zero's, set by manufacturers (not under our control). Take away the leading zeroes and you may actually match another product. Retail customers want the list in CSV format because of back-end processing programs, that are also out of our control and different per customer, so we cannot change the format of the CSV files. No prefixed'=', nor added tabs. The data in the raw CSV files is correct; it's when customers open those files in Excel the problems start. And many customers are not really computer savvy. They can just about open and save an email attachment. We are thinking of providing the data in two slightly different formats: one as Excel Friendly (using the options suggested above by adding a TAB, the other one as the 'master'. But this may be wishful thinking as some customers will not understand why we need to do this. Meanwhile we continue to keep explaining why they sometimes see 'wrong' data in their spreadsheets. Until Microsoft makes a proper change I see no proper resolution to this, as long as one has no control over how end-users use the files.

(excel2007及以上版本)

如何强制excel不“检测”日期格式而不编辑源文件

:

将文件重命名为.txt 如果你不能这样做,而不是直接在excel中打开CSV文件,创建一个新的工作簿,然后转到 数据>从文本中获取外部数据>并选择您的CSV。

无论哪种方式,你都会看到导入选项,只需选择每个包含日期的列,并告诉excel格式为“文本”而不是“一般”。

我知道这是个老话题了。对于像我这样的人来说,通过PowerShell COM对象使用Office 2013仍然有这个问题,可以使用opentext方法。问题是这个方法有很多参数,有时是相互排斥的。要解决这个问题,您可以使用本文介绍的invoke-namedparameter方法。 一个例子是

$ex = New-Object -com "Excel.Application"
$ex.visible = $true
$csv = "path\to\your\csv.csv"
Invoke-NamedParameter ($ex.workbooks) "opentext" @{"filename"=$csv; "Semicolon"= $true}

不幸的是,我刚刚发现当单元格包含换行符时,该方法会以某种方式破坏CSV解析。这是CSV支持的,但微软的实现似乎有bug。 此外,它不知何故没有检测到德语特有的字符。给它正确的文化并没有改变这种行为。所有文件(CSV和脚本)都使用utf8编码保存。 首先,我编写了以下代码,逐个单元插入CSV。

$ex = New-Object -com "Excel.Application"
$ex.visible = $true;
$csv = "path\to\your\csv.csv";
$ex.workbooks.add();
$ex.activeWorkbook.activeSheet.Cells.NumberFormat = "@";
$data = import-csv $csv -encoding utf8 -delimiter ";"; 
$row = 1; 
$data | %{ $obj = $_; $col = 1; $_.psobject.properties.Name |%{if($row -eq1){$ex.ActiveWorkbook.activeSheet.Cells.item($row,$col).Value2= $_ };$ex.ActiveWorkbook.activeSheet.Cells.item($row+1,$col).Value2 =$obj.$_; $col++ }; $row++;}

但这太慢了,所以我找了个替代品。显然,Excel允许您使用矩阵设置单元格范围的值。所以我在这篇博客中使用算法将CSV转换为一个多数组。

function csvToExcel($csv,$delimiter){
     $a = New-Object -com "Excel.Application"
     $a.visible = $true
     
    $a.workbooks.add()
     $a.activeWorkbook.activeSheet.Cells.NumberFormat = "@"
     $data = import-csv -delimiter $delimiter $csv; 
     $array = ($data |ConvertTo-MultiArray).Value
     $starta = [int][char]'a' - 1
     if ($array.GetLength(1) -gt 26) {
         $col = [char]([int][math]::Floor($array.GetLength(1)/26) + $starta) + [char](($array.GetLength(1)%26) + $Starta)
     } else {
         $col = [char]($array.GetLength(1) + $starta)
     }
     $range = $a.activeWorkbook.activeSheet.Range("a1:"+$col+""+$array.GetLength(0))
     $range.value2 = $array;
     $range.Columns.AutoFit();
     $range.Rows.AutoFit();
     $range.Cells.HorizontalAlignment = -4131
     $range.Cells.VerticalAlignment = -4160
}

 function ConvertTo-MultiArray {
     param(
         [Parameter(Mandatory=$true, Position=1, ValueFromPipeline=$true)]
         [PSObject[]]$InputObject
     )
     BEGIN {
         $objects = @()
         [ref]$array = [ref]$null
     }
     Process {
         $objects += $InputObject
     }
     END {
         $properties = $objects[0].psobject.properties |%{$_.name}
         $array.Value = New-Object 'object[,]' ($objects.Count+1),$properties.count
         # i = row and j = column
         $j = 0
         $properties |%{
             $array.Value[0,$j] = $_.tostring()
             $j++
         }
         $i = 1
         $objects |% {
             $item = $_
             $j = 0
             $properties | % {
                 if ($item.($_) -eq $null) {
                     $array.value[$i,$j] = ""
                 }
                 else {
                     $array.value[$i,$j] = $item.($_).tostring()
                 }
                 $j++
             }
             $i++
         }
         $array
     } 
} 
csvToExcel "storage_stats.csv" ";"

你可以按原样使用上面的代码;它应该将任何CSV转换为Excel。只需将路径更改为CSV和底部的分隔符。

如果有人还在寻找答案,下面这句话对我来说非常合适

我输入=("my_value")。 即=("04SEP2009")显示为04SEP2009,而不是09/04/2009

这同样适用于大于15位的整数。他们不再修剪了。

下面是我们在生成csv文件时使用的简单方法,它确实会稍微改变值,所以并不适用于所有应用程序:

在csv中的所有值前加一个空格

excel将从诸如“1”、“2.3”和“-2.9e4”等数字中剥离该空格,但将保留诸如“01/10/1993”这样的日期和诸如“TRUE”这样的布尔值,从而阻止它们转换为excel的内部数据类型。

它也停止双引号被zapped在读进去,所以一个简单的方法,使文本在csv中保持不变的excel即使是一些文本,如“3.1415”是用双引号包围它,并在整个字符串前加上一个空格,即(使用单引号来显示你会键入什么)“3.1415”。然后在excel中,你总是有原始的字符串,除了它被双引号包围并以空格开头,所以你需要在任何公式中考虑这些。