CSV转换程序没有执行构建方法的内容

时间:2016-06-16 18:42:41

标签: c# csv

我已经构建了一个CSV转换程序,其中一个方法是BuildRecordAndWriteOutput(),我已经构建它以输出存储在outputBuffers中的新项目,但也可以跳过某些字段中的逗号。这个方法在我的其他一些程序中运行得非常好,但是对于这个程序,当它不应该在字段中的逗号分割时。

一个示例是名称字段可能包含Company Name, Example,但是在A列中,但当我在输出文件中重新打印出来时,它将是Company Name Example,它们将是移位超过1列,而不是在1个字段中。

我的困惑在于,尽管我的代码可能不是最优雅或最好的方法,但我必须忽略一些我调用方法或变量的方法。

我一直在扫描并将代码与其他程序进行比较,其中该方法完美且没有运气!任何人都可以看到我不能在这个吗?

我的源代码

class Program
{
    // File object variables
    static TextFieldParser input = new TextFieldParser("PPVendingPricing.csv");
    static StreamWriter output = new StreamWriter("convertedInventoryItemListTacMed.csv");

    // Input and output buffer variables
    static string[] inputBuffer;
    static string[] outputBuffer = new string[40];


    static void Main(string[] args)
    { 
        ReadInputAndBuildDataStructures();

        Console.WriteLine("done");
        input.Close();
        output.Close();
        Console.Read();
    }

     /*
     * This method reads the input file and fill data structures
     * that are used to organize the data before moving selected
     * fields to the output buffer.
     */ 
    public static void ReadInputAndBuildDataStructures()
    {
        input.SetDelimiters(",");

        input.ReadFields(); // Skip the header record.

        while (!input.EndOfData)
        {
            inputBuffer = input.ReadFields(); // Read a CSV record in to the inputBuffer. 
            {
                SetOutputBufferDefaultValues(); // Put default values in the output buffer
                MapInputFieldsToOutputFields(); // Move fields from the input buffer to the output buffer.
                BuildRecordAndWriteOutput(); // Build record from output buffer and write it.
            }
        }
    }

    /*
    * This method loads default values into the output
    * buffer (string array). Some of these values will be 
    * replaced before the output buffer is written to the file.
    */
    public static void SetOutputBufferDefaultValues()
    {
        // Initialize all fields to empty.
        for (int i = 0; i < outputBuffer.Length; i++)
        {
            outputBuffer[i] = "";
        }

        // Update selected fields with default values.
        outputBuffer[7] = "Inc";
        outputBuffer[10] = "TRUE";
        outputBuffer[11] = "FIFO";
        outputBuffer[15] = "TRUE";
        outputBuffer[17] = "Main";
        outputBuffer[19] = "TRUE";
        outputBuffer[21] = "Inc";
        outputBuffer[25] = "Main";
        outputBuffer[28] = "Periods of Supply";
        outputBuffer[32] = "1";
        outputBuffer[35] = "By Overall Item Qty";
        outputBuffer[36] = "TRUE";
        outputBuffer[37] = "TRUE";
    }

    /*
    * This method maps selected values from the input buffer
    * to the appropriate position in the output buffer.
    */
    public static void MapInputFieldsToOutputFields()
    {
        outputBuffer[0] = inputBuffer[26];
        outputBuffer[1] = inputBuffer[38];
        outputBuffer[2] = inputBuffer[3];
        outputBuffer[3] = inputBuffer[3];
        outputBuffer[4] = inputBuffer[40];
        outputBuffer[5] = inputBuffer[3];
        outputBuffer[6] = inputBuffer[27];
        outputBuffer[12] = inputBuffer[13];
        outputBuffer[13] = inputBuffer[39];
        outputBuffer[14] = inputBuffer[38] + " " +inputBuffer[40];
        //skipping outputBuffer[16] position 17 on spreadsheet
        outputBuffer[20] = inputBuffer[36];
        outputBuffer[22] = inputBuffer[37];
        outputBuffer[23] = inputBuffer[39];
        outputBuffer[24] = inputBuffer[40];
        outputBuffer[29] = inputBuffer[27];
        outputBuffer[33] = inputBuffer[18];
        outputBuffer[34] = inputBuffer[19];
        outputBuffer[38] = inputBuffer[39];
    }

    /*
     * This method uses the fields (array elements) in the output
     * buffer to assemble a CSV record (string variable). The
     * CSV record is then written to the output file.
     */
    public static void BuildRecordAndWriteOutput()
    {
        string record = "";

        for (int i = 0; i < outputBuffer.Length; i++)
        {
            if (outputBuffer[i].Contains(","))
            {
                string x = "\"" + outputBuffer[i] + "\"";
                record += x;
            }
            else
            {
                record += outputBuffer[i];
            }
            if (i < outputBuffer.Length - 1)
            {
                record += ",";
            }
        }

        output.WriteLine(record);
    }
}

以下是输出CSV文件损坏的图片

enter image description here

正如你所看到的,顶行是完美的,但是底行是移位的,因为它在逗号"TAPE CLOTH 1 - ADHESIVE BANDAGE ROLL, 2.50cm X 5m"上分开,因为它在读取时有一个逗号,在输出中分割它但我的方法应该照顾这个。

为什么我的方法不起作用?我称错了吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

您似乎缺少设置HasFieldsEnclosedInQuotes属性。 input.HasFieldsEnclosedInQuotes = true应该可以解决您的问题。

  

如果字段用引号括起来,例如abc,   &#34; field2a,field2b&#34;,field3和此属性为True,然后是所有文本   用引号括起来的,将按原样退回;这个例子会   return abc | field2a,field2b | field3。将此属性设置为False   将使这个例子返回abc |&#34; field2a | field2b&#34; | field3。