【发布时间】:2014-02-20 02:15:37
【问题描述】:
我在代码中添加了PadLeft(8,'0'),以确保插入到数据库表中的所有帐户至少有 8 个字符长。代码如下:
public static string CleanAccount(String strVal)
{
string cleanValue;
string paddedAccount = strVal.PadLeft(8,'0');
//MessageBox.Show("account: " + paddedAccount);
if (paddedAccount == null)
{
throw new System.ArgumentException("Value cannot be null", "original");
}
else
{
cleanValue = paddedAccount.Replace(" ", "").Replace("$", "").Replace("-", "");
}
return cleanValue;
}
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.get_Item(1);
Microsoft.Office.Interop.Excel.Range xlRange = worksheet.UsedRange;
long fullRow = worksheet.Rows.Count;
long lastRow = worksheet.Cells[fullRow, 1].End(Microsoft.Office.Interop.Excel.XlDirection.xlUp).Row;
int colCount = xlRange.Columns.Count;
for (int i = 2; i <= lastRow; i++)
{
lstTran.Add(new LegalTransactionRec()
{
AccountNumber = Form1.CleanAccount(xlRange.Cells[i, 1].Value2.ToString()),
CostAmount = Form1.CleanAmount(Form1.TryToParse(xlRange.Cells[i, 3].Value2.ToString())),
SSN = Form1.CleanString(xlRange.Cells[i, 6].Value2.ToString()),
TransactionDate = Form1.ConvertToDateTime(xlRange.Cells[i, 2].Value),
Description = Form1.CleanDescription(xlRange.Cells[i, 8].Value2.ToString()),
TransactionCode = Form1.CleanTranCode(Form1.CleanExtra(xlRange.Cells[i, 4].Value2.ToString()))
}
);
}
当MessageBox 没有被注释掉时,我看到该帐户确实被填充了,但是在它被添加到数据库表之后它显示没有前导零。为什么会发生这种情况?
【问题讨论】:
-
你为什么用
for (int j = 1; j <= 1; j++)?当j <= 1使其无用并且您没有在循环内使用j时。你可以删除它。 -
@WasifHossain Bandaid 暂时修复,我不需要知道专栏在什么位置。
-
我会随机猜测它与表格列的类型或电子表格单元格的格式有关。不能说是哪个。所以你看到的是一个数字而不是一个字符串,并且数字没有前导零。
-
@HansPassant 先生,您是对的,我很傻,忘记在要添加的项目周围添加单引号。用全新的眼光和我大声谈论它,终于注意到了它。
标签: c# formatting string-formatting