reader 读到空值怎么处理_在excel文件中读取空值的问题

Hi All

I am developing C# windows application where I have to read excel file and write to .ini file but one cell has null value which I value to put default 1. Every thing is ok the problem to put 1 in the place null value. If it is possible please help below I am giving codeforeach (DataGridViewRow r in dg.SelectedRows)

{

fs = new FileStream(fp, FileMode.Open, FileAccess.Read);

StreamReader rd = new StreamReader(fs);

rd.BaseStream.Seek(0, SeekOrigin.Begin);

PCL = rd.ReadToEnd().ToString();

rd.Close();

fs.Close();

string dat="";

for (int i = 0; i < r.Cells.Count; i++)

{

if (r.Cells[i].Value != null)

{

dat = r.Cells[i].Value.ToString();

if (dg.Columns[i].HeaderText == "NOC")

{

if (r.Cells[4].Value == "")

{

PCL = Microsoft.VisualBasic.Strings.Replace(PCL, "NOC", Microsoft.VisualBasic.Strings.Format(int.Parse(dat), "0000").ToString(), 1, -1, Microsoft.VisualBasic.CompareMethod.Text);// here I think I have to do some thing which make default value 1

}

else

{

PCL = Microsoft.VisualBasic.Strings.Replace(PCL, "NOC", Microsoft.VisualBasic.Strings.Format(int.Parse(dat), "0000").ToString(), 1, -1, Microsoft.VisualBasic.CompareMethod.Text);

}

}

else

{

PCL = Microsoft.VisualBasic.Strings.Replace(PCL , "" + dg.Columns[i].HeaderText.ToString() + "", dat, 1, -1, Microsoft.VisualBasic.CompareMethod.Text);

}

}

}

PCL = Microsoft.VisualBasic.Strings.Replace(PCL, "NOC","0001", 1, -1, Microsoft.VisualBasic.CompareMethod.Text);

if (r.Cells[0].Value != null)

{

PCL1 = PCL1 + PCL + CrLf;

PCL = "";

}

else

{

PCL = "";

}

}

Thanks & Regards

Indrajit Dasgupta

解决方案Hi,

I think you should make a function to trapped said error:

Example:

public static string IifStr(object p)

{

string retVal = string.Empty;

if (p != DBNull.Value || p != null)

{

retVal = Convert.ToString(p);

}

return retVal;

}

//Overload IifStr

public static string IifStr(string p)

{

string retVal = string.Empty;

if (!string.IsNullOrEmpty(p))

{

retVal = Convert.ToString(p);

}

return retVal;

}

IEnumerable dtRow = exDT.AsEnumerable();

var lstExcelData = dtRow.Select(s => new Claim

{

Remarks = IifStr(s.Field("REMARKS")),

ClaimType = IifStr(s.Field("TRANSTYPE"))

}).ToList();

Hope this could help...

Regards,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值