比较数据集中的单元格和预置信息

本文关键字:信息 单元格 数据 数据集 集中 比较 | 更新日期: 2023-09-27 18:36:15

好吧,我对我的 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();
    }
}

}

比较数据集中的单元格和预置信息

假设我正确理解您的目标,并且您想要 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);
}

将"6"更改为所需的最终宽度。

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

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

这将确保所有字符串都是 5 个字符,并在较短时添加前导零。

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;
                        }
    }