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

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


当前回答

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

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

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

其他回答

警告:Excel '07(至少)有一个(另一个)错误:如果字段的内容中有逗号,它不会正确解析="field, contents",而是将逗号后的所有内容放入下面的字段中,而不管引号是什么。

我发现唯一有效的解决方法是当字段内容包含逗号时消除=。

这可能意味着有些字段不可能在Excel中完全“正确”地表示,但到目前为止,我相信没有人会感到太惊讶。

嗨,我也有同样的问题,

我写这个vbscipt来创建另一个CSV文件。新的CSV文件将在每个字段的字体中有一个空格,因此excel将把它理解为文本。

所以你用下面的代码创建了一个。vbs文件(例如Modify_CSV.vbs),保存并关闭它。将原始文件拖放到vbscript文件中。它将在相同的位置创建一个文件名为“SPACE_ADDED”的新文件。

Set objArgs = WScript.Arguments

Set objFso = createobject("scripting.filesystemobject")

dim objTextFile
dim arrStr ' an array to hold the text content
dim sLine  ' holding text to write to new file

'Looping through all dropped file
For t = 0 to objArgs.Count - 1
    ' Input Path
    inPath = objFso.GetFile(wscript.arguments.item(t))

    ' OutPut Path
    outPath = replace(inPath, objFso.GetFileName(inPath), left(objFso.GetFileName(inPath), InStrRev(objFso.GetFileName(inPath),".") - 1) & "_SPACE_ADDED.csv")

    ' Read the file
    set objTextFile = objFso.OpenTextFile(inPath)


    'Now Creating the file can overwrite exiting file
    set aNewFile = objFso.CreateTextFile(outPath, True) 
    aNewFile.Close  

    'Open the file to appending data
    set aNewFile = objFso.OpenTextFile(outPath, 8) '2=Open for writing 8 for appending

    ' Reading data and writing it to new file
    Do while NOT objTextFile.AtEndOfStream
        arrStr = split(objTextFile.ReadLine,",")

        sLine = ""  'Clear previous data

        For i=lbound(arrStr) to ubound(arrStr)
            sLine = sLine + " " + arrStr(i) + ","
        Next

        'Writing data to new file
        aNewFile.WriteLine left(sLine, len(sLine)-1) 'Get rid of that extra comma from the loop


    Loop

    'Closing new file
    aNewFile.Close  

Next ' This is for next file

set aNewFile=nothing
set objFso = nothing
set objArgs = nothing

(EXCEL 2016及以后版本,实际上我没有尝试过旧版本)

打开新空白页 转到“数据”标签 点击“来自文本/CSV”,选择CSV文件 检查预览您的数据是否正确。 在сase中,当某些列被转换为日期时,单击“编辑”,然后通过单击列头部的日历选择类型文本 点击“关闭并加载”

我知道这是个老话题了。对于像我这样的人来说,通过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和底部的分隔符。

CSV -用逗号分隔的值。只需通过文本编辑器创建/编辑,而不是xls/xlsx/exel。 在编辑时,您可以设置所需的格式的日期,它必须保持完整。 这是假设同样的文件将被以编程方式处理。