如何把 Execl读到DataGrid中
下面的代码有问题?请大哥指教
DataTable dt=new DataTable();
dt.Columns.Add("gzh", typeof(System.String));
dt.Columns.Add("xm", typeof(System.String));
dt.Columns.Add("dw", typeof(System.String));
dt.Columns.Add("zz", typeof(System.String));
dt.Columns.Add("mj", typeof(System.String));
dt.Columns.Add("kf", typeof(System.String));
dt.Columns.Add("nf", typeof(System.String));
DataRow row;
string delimStr = ",";
char [] delimiter = delimStr.ToCharArray();
string [] split = null;
int rlong=0;
try
{
string path = @"c:\暖气扣费.csv";
FileStream fs = new FileStream( path, FileMode.Open, FileAccess.Read);
//StreamReader sr = new StreamReader( fs,System.Text.Encoding.Default );
//StreamReader sr = new StreamReader( fs,System.Text.Encoding.GetEncoding("gb2312"));
StreamReader sr = new StreamReader( fs,System.Text.Encoding.GetEncoding("utf-8"));
this.Label1.Text="正在读取,请稍候...";
string curLine;
while( (curLine = sr.ReadLine())!= null )
{
Console.WriteLine( curLine );
split=curLine.Split(delimiter,10);
row = dt.NewRow();
string s;
for (int i=0;i<7;i++)
{
s=split[i].ToString();
row[i]=s;
}
dt.Rows.Add(row);
rlong++;
}
sr.Close();
}
catch (Exception sendex)
{
this.Label1.Text="<font color=red>操作失败"+sendex.Message+"</font>";
}
dt.AcceptChanges();
this.Label1.Text="读取完毕!共"+rlong.ToString()+"条数据";
this.DataGrid1.DataSource=dt;
this.DataGrid1.DataBind();
if( dt.Rows.Count>0 )
this.ImageButton2.Visible=true;