比较数据集中的单元格并预先添加信息

时间:2015-01-05 03:36:58

标签: c#

好吧,我的C#上有点生锈,所以这可能是我不能得到的容易,但它已经很晚了,我的大脑很疼。我试图获取我的数据集中的每个值,如果它们比给定长度短,则将零添加到开头。问题是我不知道如何实施它的概念。任何帮助赞赏。这就是我到目前为止所拥有的。

        //Connection and location to Excel file
        OleDbConnection conn = new OleDbConnection();
        conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\\Users\\User\\Desktop\\New folder\\DHS_Weekly.xlsx; Extended Properties= \"Excel 12.0;HDR=No;IMEX=1;ImportMixedTypes=Text;\"";

        //Select location in the file
        OleDbCommand command = new OleDbCommand
            (
                "SELECT * FROM [WEEKLY.COOLING.PAYMENT$C:D]", conn
            );
        //Create a dataset to temp store the information
        DataSet dsDHS = new DataSet();
        OleDbDataAdapter adapter = new OleDbDataAdapter(command);
        adapter.Fill(dsDHS);


        //Having the data Grid display the results of the data set DHS(dsDHS)
        dataGridView1.DataSource = dsDHS.Tables[0];

         //New string that sets the path to the output of a text file 
        //The dataset will be output to this text file
        string path = @"C:\\Users\\User\\Desktop\\New folder\\DHStest.txt";

        //New streamwriter and stringbuilder that uses the path created above
       StreamWriter textOut = new StreamWriter(new FileStream(path, FileMode.Create, FileAccess.Write));
       StringBuilder str = new StringBuilder();

       //uses strings to correctly display the dataset 
       //removes the last 3 rows from output (2 Null rows and last line not needed)
       //removes the first row by starting at 1 instead of zero. (first row is null)
       for (int i = 1; i <= dsDHS.Tables[0].Rows.Count - 3; i++)
       {
           for (int j = 0; j <= dsDHS.Tables[0].Columns.Count - 1; j++)
           {
               str.Append(dsDHS.Tables[0].Rows[i][j].ToString());
           }
           //Adds a new line between each row
           str.AppendLine();
       }
        //writes the output,  this is just so I can check things before finalizing
       textOut.Write(str.ToString());
       textOut.Close();

    }
}

}

3 个答案:

答案 0 :(得分:4)

假设我正确理解了你的内容,并且你希望最终长度为6个字符:

for (int j = 0; j <= dsDHS.Tables[0].Columns.Count - 1; j++)
{
    var originalValue = dsDHS.Tables[0].Rows[i][j].ToString();
    var padded = originalValue.PadLeft(6, '0');

    str.Append(padded);
}

改变&#39; 6&#39;对于所需的最终宽度。

答案 1 :(得分:3)

我不确定您要在代码中执行此操作的位置,但您需要使用String.PadLeft

str.Append(dsDHS.Tables[0].Rows[i][j].ToString().PadLeft(5, "0"c));

这将确保所有字符串都是5个字符,并且当它们更短时添加前导零。

答案 2 :(得分:1)

I recently had a similar requirement where decimal/float values were needed in a 10-digit format with 4 decimal places.  (For example:  "000123.4560").

Here's how I did it.  (There's probably a simpler method, but it's been working great so far...):
int k = 0;
bool DecFound = false;
string s1=TodaysResults[1, i].ToString();
string IntegerDigits = "";
string DecimalDigits = "";
for (int j = 0; j < s1.Length; j++)
    {
        if (s1.Substring(j, 1) == ".")
                    {
                            k = j;
                                DecFound = true;
            }
            if (DecFound == false)
                            {
                                IntegerDigits += s1.Substring(j, 1);
                            }
                            else
                            {
                                DecimalDigits += s1.Substring(j, 1);
                            }
                        }
                        switch (IntegerDigits.Length)
                        {
                            case 0:
                                IntegerDigits = "000000";
                                break;
                            case 1:
                                IntegerDigits = "00000" + IntegerDigits;
                                break;
                            case 2:
                                IntegerDigits = "0000" + IntegerDigits;
                                break;
                            case 3:
                                IntegerDigits = "000" + IntegerDigits;
                                break;
                            case 4:
                                IntegerDigits = "00" + IntegerDigits;
                                break;
                            case 5:
                                IntegerDigits = "0" + IntegerDigits;
                                break;
                            default:
                                break;
                        }
                        switch (DecimalDigits.Length)
                        {
                            case 0:
                                DecimalDigits = DecimalDigits + ".0000";
                                break;
                            case 1:
                                DecimalDigits = DecimalDigits + "0000";
                                break;
                            case 2:
                                DecimalDigits = DecimalDigits + "000";
                                break;
                            case 3:
                                DecimalDigits = DecimalDigits + "00";
                                break;
                            case 4:
                                DecimalDigits = DecimalDigits + "0";
                                break;
                            default:
                                break;
                        }
    }