2017-02-02 25 views
0

誰でも2つの列の金額データタイプを動的にフォーマットできますか?Cでの金額データテーブル値の書式

using System; 
using System.Data; 
using Microsoft.SqlServer.Dts.Runtime; 
using System.Windows.Forms; 
using System.Data.OleDb; 
using System.IO; 
using System.Reflection; 

namespace ST_367d705c54de4e9e9f890350f933c80b.csproj 

{ 
    [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute] 
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase 
    { 
    #region VSTA generated code 
    enum ScriptResults 
    { 
     Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success, 
     Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure 
    }; 
    #endregion 


    public void Main() 
    { 
     string basePath = Dts.Variables["User::VarDirectoryPath"].Value.ToString(); 

     try 
     { 
      OleDbDataAdapter A = new OleDbDataAdapter(); 
      System.Data.DataTable dt = new System.Data.DataTable(); 

      var delimiter = Dts.Variables["User::VarDelimiter"].Value.ToString(); 
      A.Fill(dt, Dts.Variables["User::VarObject"].Value); 

      basePath = Dts.Variables["User::VarDirectoryPath"].Value.ToString(); 
      string filePath = Path.Combine(basePath, (Dts.Variables["User::VarFileName_1"].Value.ToString() 
       + Dts.Variables["User::VarDate"].Value.ToString() + Dts.Variables["User::VarFileExtension_1"].Value.ToString())); 

      int i = 0; 
      StreamWriter sw = null; 

      using (sw = new StreamWriter(filePath, false)) 
      { 
       for (i = 0; i < dt.Columns.Count; i++) 
       { 
        sw.Write(dt.Columns[i].ToString() + Dts.Variables["User::VarDelimiter"].Value.ToString()); 
       } 

       sw.WriteLine(); 

       foreach (DataRow row in dt.Rows) 
       { 
        object[] array = row.ItemArray; 
        for (i = 0; i < array.Length; i++) 
        { 
         object val = array[i]; 
         if (val == "0") 
          sw.Write(string.Format("#.##", val) + delimiter); 
         else 
          sw.Write(val + delimiter); 
        } 

        sw.WriteLine(); 
       } 

       sw.Close(); 
      } 
     } 
     catch (Exception ex) 
     { 
      string file = string.Format("err_{0}.txt", Dts.Variables["User::VarDate"].Value.ToString()); 
      File.WriteAllText(Path.Combine(basePath, file), ex.ToString()); 
     } 

     Dts.TaskResult = (int)ScriptResults.Success; 
    } 
} 
} 

データテーブルには10個のカラムがあり、カラム3,4,5は小数点以下2桁でフォーマットする必要があり、値がゼロまたはnullの場合は0.00を入力する必要があります。ここで

+0

これらの列のデータ型は何ですか? float.tryparseを使用してfloatまたはdoubleに変換する必要があります – dadde

+0

列のデータ型は小数点以下(18,2)です。これはExcelでデフォルトで0になります。 nullまたは "0"の値には0.00の値が必要です。値529の場合は529.00と表示する必要があります – user3120927

+0

いいえオブジェクトのデータ型は何ですか?val = array [i];それらの列のために?デバッグとチェック。あなたは文字列として解析されていると仮定しています。そうであれば、書式設定を行う前にそれらを解析して倍精度浮動小数点数に変換する必要があります。 nullを処理するには、string.isNullOrEmpty()もチェックします。 – dadde

答えて

0

は、小数点以下のフォーマットの例を示します

https://dotnetfiddle.net/da7UoR

using System; 

public class Program 
{ 
    public static void Main() 
    { 
     decimal val1 = 0m; 
     decimal val2 = 125580m; 


     Console.WriteLine(val1.ToString("0.00")); 
     //Output 0.00 

     Console.WriteLine(val2.ToString("0.00")); 
     //Output 125580.00 

     //Money format 
     Console.WriteLine(val1.ToString("C"));  
     //Output $0.00 

     Console.WriteLine(decimalToFormattedString(val1)); 
     //Output 0.00 

     Console.WriteLine(decimalToFormattedString(val2)); 
     //Output 125580.00 
    } 

    public static string decimalToFormattedString(decimal myValue) 
    { 
     return myValue.ToString("0.00"); 
    } 
}