简体   繁体   English

如何写一个以-开头的字符串到csv文件中?

[英]how to write a string which starts with - into csv file?

I am trying to write data to CSV file. 我正在尝试将数据写入CSV文件。

The string value which starts with - is getting converted to #NAME? -开头的字符串值将转换为#NAME? automatically when i open csv file after writing. 写入后自动打开csv文件时自动生成。 eg If i write test it displays correctly but when i write -test the value would be #NAME? 例如,如果我写test它将正确显示,但是当我写-test ,值将是#NAME? when i open csv file. 当我打开csv文件时。 It is not a code issue but csv file automatically changes the value which starts with - to error(#NAME?) . 这不是代码问题,但是csv文件会自动将以-开头的值更改为error(#NAME?) How can i correct this programmatically. 我如何以编程方式更正此问题。 below is the code, 下面是代码,

public class FileWriterTest {
    public static void main(String[] args) {
        BufferedWriter bufferedWriter = null;
        File file = new File("test.csv");
        try {
            bufferedWriter = new BufferedWriter(new FileWriter(file));
            List<String> records = getRecords();
            for (String record : records) {
                bufferedWriter.write(record);
                bufferedWriter.newLine();
            }
            bufferedWriter.flush();
            System.out.println("Completed writing data to a file.");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedWriter != null)
                bufferedWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static List<String> getRecords() {
        List<String> al = new ArrayList<String>();
        String s1 = "test";
        String s2 = "-test";
        al.add(s1);
        al.add(s2);
        return al;
    }
}

Could you please assist? 你能帮忙吗?

In the formal CSV standard , you can do this by using quotes ( " ) around the field value. They're a text delimiter (as opposed to other kinds of values, like numeric ones). 正式的CSV标准中 ,您可以通过在字段值周围使用引号( " )来实现此目的。它们是文本定界符(与其他类型的值(如数字值)相反)。

It sounds like you're using Excel. 听起来您正在使用Excel。 You may need to enable " as a text delimiter/indicator. 您可能需要启用"作为文本定界符/指示符。

Update: If you double-click the .csv to open it in Excel, even this doesn't work. 更新:如果双击.csv以在Excel中打开它,即使这样也不起作用。 You have to open a workbook and then import the CSV data into it. 您必须打开一个工作簿,然后将CSV数据导入其中。 (Pathetic, really...) (真可悲,...)

It's a problem with excel. excel有问题。 When you open a CSV file in excel it tries to determine cell type automatically which usually fails. 当您在excel中打开CSV文件时,它会尝试自动确定通常会失败的单元格类型。 The CSV file is alright the editor is not ;) CSV文件没问题,编辑器不是;)

You can either right click on the field, select Format Cell and there make it a text file (and you might need to remove the automatically inserted '=' sign). 您可以右键单击该字段,选择“设置单元格格式”,然后在其中创建一个文本文件(并且可能需要删除自动插入的“ =”符号)。 Or you can open the CSV file by going into Data - From Text/CSV and in the wizard select the proper column types. 或者,您可以进入数据-从文本/ CSV中打开CSV文件,然后在向导中选择适当的列类型。

I got a relatively old version of Excel (2007), and the following works perfectly: 我有一个相对较旧的Excel版本(2007),以下代码可以正常运行:

Put the text between double quotes and preceed it with an equal sign. 将文本放在双引号之间,并以等号开头。
Ie, -test becomes ="-test" . 即, -test变为="-test"

You file will therefore look like this: 因此,您的文件将如下所示:

test1,test2,test3
test4,="-test5",test6

UPDATE UPDATE
Works in Excel-2010 as well. 同样适用于Excel-2010。

As Veselin Davidov mentioned, this will break the csv standard but I don't know whether that's a problem. 正如Veselin Davidov所提到的那样,这将破坏csv标准,但我不知道这是否是一个问题。

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

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