CSV文件分隔

时间:2013-05-31 15:40:37

标签: c# csv delimiter

我读入了一个字符串,然后输出到一个内部有逗号的CSV文件。字符串是

USA,B& W 1-25

我想要的只是将该字符串留在一个特定的单元格中,而不是分成两个不同的单元格。这可能是一个简单的答案,但我很难完成这个。任何帮助将非常感激。如果需要有人想看看它,这是我的输出代码...

    public void printAll()
    {
        output2.WriteLine("All companies in order of sequence number, THIS IS OUTPUT 2!");
        output2.WriteLine("___________________________________________________________________________________________");
        int i = listHead2;

        //Loops until the end of the list, printing out info
        while (i != -1)
        {
            output2.WriteLine("{0}" + ", {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13}, {14}, {15}, {16}, {17}, {18}, {19}, {20}, {21}, {22}, {23}, {24}, {25}, {26}, {27}, {28}, {29}, {30}, {31}, {32}, {33}, {34}, {35}",
                leaseName2[i], fieldName2[i],reservoir2[i], operator2[i], county2[i], state2[i],  majo2[i], resvCatgory2[i], disRate2[i], netOil2Int2[i], netGas2Int2[i], workingInt2[i], grossWells2[i]
                ,ultOil2[i], ultGas2[i], grossOil2[i], grossNGL2[i], grossGas2[i], netOil2[i], netGas2[i], netNGL2[i], revToInt2[i], operExpense2[i], totInvest2[i], revOil2[i], revGas2[i], operatingProfit2[i],
            revNGL2[i], discNetIncome2[i], seqNum2[i], wellID2[i], incASN2[i], lifeYears2[i],  ownQual2[i], prodTax2[i], AdValorem2Tax2[i]);
            i = pointers2[i];
        }
    }

感谢。

3 个答案:

答案 0 :(得分:3)

您可以尝试用双引号括起字段:“USA,B& W 1-25”这应该会将字符串视为一列。

答案 1 :(得分:3)

您可以创建一个引用字符串的小方法:

public string QuoteString(string input)
{
    return "\"" + input.Replace("\"", "\"\"") + "\"";
}

然后您可以使用它:

QuoteString(leaseName2[i]), QuoteString(fieldName2[i]),...

祝你的代码好运。

答案 2 :(得分:0)

CSV文件不一定需要以逗号分隔。如果有帮助,你可以使用分号进行分离!