在Excel文件中读取空值的问题 [英] problem to read null value in excel file
本文介绍了在Excel文件中读取空值的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
我正在开发C#Windows应用程序,在这里我必须读取excel文件并写入.ini文件,但是一个单元格具有空值,我将该值设置为默认值1.将1放在空值中就可以了.如果可能,请在下面提供帮助,我正在提供代码
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 code
foreach (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, "<fe />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, "<fe />NOC", Microsoft.VisualBasic.Strings.Format(int.Parse(dat), "0000").ToString(), 1, -1, Microsoft.VisualBasic.CompareMethod.Text);
}
}
else
{
PCL = Microsoft.VisualBasic.Strings.Replace(PCL , "<fe />" + dg.Columns[i].HeaderText.ToString() + "", dat, 1, -1, Microsoft.VisualBasic.CompareMethod.Text);
}
}
}
PCL = Microsoft.VisualBasic.Strings.Replace(PCL, "<fe />NOC","0001", 1, -1, Microsoft.VisualBasic.CompareMethod.Text);
if (r.Cells[0].Value != null)
{
PCL1 = PCL1 + PCL + CrLf;
PCL = "";
}
else
{
PCL = "";
}
}
谢谢&问候
Indrajit Dasgupta
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<DataRow> dtRow = exDT.AsEnumerable();
var lstExcelData = dtRow.Select(s => new Claim
{
Remarks = IifStr(s.Field<string>("REMARKS")),
ClaimType = IifStr(s.Field<string>("TRANSTYPE"))
}).ToList();
希望这可以帮助...
问候
Hope this could help...
Regards,
这篇关于在Excel文件中读取空值的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文