Excel错误地将范围转换为date,如何避免它?

我有一个.tsv文件,其中一些字段的范围是1 - 4 。 我想阅读这些领域,因为他们是文字写入。 但是,文件打开excel自动将这些范围字段转换为date。 例如1 - 4转换为4-Jan 。 如果我尝试格式化单元格到另一种types,值已经改变,我只能得到一个无用的数字(39816)。 即使范围字段在双引号内,仍然会发生错误的转换。 如何避免这种行为?

我认为你最好在Excel中使用导入工具,但你可能不得不手动改变文件扩展名为CSV

导入时请确保为所有具有这些值的列select文本。

我的问题其实至less是一个重复:

1) 停止Excel自动将某些文本值转换为date

2) Excel:打开.csv文件时默认为TEXT而不是GENERAL

Excel的可能解决scheme是:1)使用特殊双引号(如"May 16, 2011" )将字段写为"=""May 16, 2011"""或2)使用外部数据向导导入csv / tsv文件然后手动select你想要阅读的列为TEXT而不是GENERAL(可以将字段转换为date)

至于我的使用情况,我只是使用Excel来删除一些列。 没有解决scheme是吸引我的,因为我不想用特殊的引号重写tsv文件,因为我有数百个列,我不想手动select每一个读为TEXT。

因此我写了一个scala脚本来按列名过滤tsv文件:

 package com.jmcejuela.ml import java.io.InputStream import java.io.Writer import scala.io.Codec import scala.io.Source import Table._ /** * Class to represent tables with a fixed size of columns. All rows have the same columns. */ class Table(val rows: Seq[Row]) { lazy val numDiffColumns = rows.foldLeft(Set[Int]())((set, row) => set + row.size) def toTSV(out: Writer) { if (rows.isEmpty) out.write(TableEmpty.toString) else { out.write(writeLineTSV(rows.head.map(_.name))) //header rows.foreach(r => out.write(writeLineTSV(r.map(_.value)))) out.close } } /** * Get a Table with only the given columns. */ def filterColumnsByName(columnNames: Set[String]): Table = { val existingNames = rows.head.map(_.name).toSet assert(columnNames.forall(n => existingNames.contains(n)), "You want to include column names that do not exist") new Table(rows.map { row => row.filter(col => columnNames.contains(col.name)) }) } } object TableEmpty extends Table(Seq.empty) { override def toString = "Table(Empty)" } object Table { def apply(rows: Row*) = new Table(rows) type Row = Array[Column] /** * Column representation. Note that each column has a name and a value. Since the class Table * is a sequence of rows which are a size-fixed array of columns, the name field is redundant * for Table. However, this column representation could be used in the future to support * schemata-less tables. */ case class Column(name: String, value: String) private def parseLineTSV(line: String) = line.split("\t") private def writeLineTSV(line: Seq[String]) = line.mkString("", "\t", "\n") /** * It is assumed that the first row gives the names to the columns */ def fromTSV(in: InputStream)(implicit encoding: Codec = Codec.UTF8): Table = { val linesIt = Source.fromInputStream(in).getLines if (linesIt.isEmpty) TableEmpty else { val columnNames = parseLineTSV(linesIt.next) val padding = { //add padding of empty columns-fields to lines that do not include last fields because they are empty def infinite[A](x: A): Stream[A] = x #:: infinite(x) infinite("") } val rows = linesIt.map { line => ((0 until columnNames.size).zip(parseLineTSV(line) ++: padding).map { case (index, field) => Column(columnNames(index), field) }).toArray }.toStream new Table(rows) } } } 

在Excel中写入01-04而不是1-4