日期:2014-05-18  浏览次数:20581 次

关于GridView的导出问题
有一个GridVew,想导出Excel。在GridView中有一个模板列,在模板列中一个hyperlink,结果导出之后,在Excel中数据还是超链接形式的。如果只要数据,不要超链接,应该怎么处理?谢谢大家。

------解决方案--------------------
protected virtual string GetStringToExcel()
{
SortPagedDataSource pagedDataSource = this.SortPageDataSource;
if (pagedDataSource.DataSource == null || !(pagedDataSource.DataSource is DataView))
{
return "";
}
DataView dataView = (DataView)pagedDataSource.DataSource;
StringBuilder dataString = new StringBuilder();
if (!this.AutoGenerateColumns)
{
System.Collections.Generic.List<string> columns = new List<string>();
for (int i = 0; i < this.Columns.Count; i++)
{
string dataField = null;
if (this.Columns[i] is BoundColumn)
{
dataField = ((BoundColumn)this.Columns[i]).DataField;
}
else if (this.Columns[i] is HyperLinkColumn)
{
dataField = ((HyperLinkColumn)this.Columns[i]).DataTextField;
}
else if (this.Columns[i] is ButtonColumn)
{
dataField = ((ButtonColumn)this.Columns[i]).DataTextField;
}
if (!string.IsNullOrEmpty(dataField))
{
dataString.Append( this.Columns[i].HeaderText);
dataString.Append( "\t");
columns.Add(dataField);
}
}

dataString.Remove(dataString.Length - 1, 1);
dataString.Append("\n");

for (int i = 0; i < dataView.Count; i++)
{
for (int j = 0; j < columns.Count; j++)
{
try
{
if (j == columns.Count - 1)
{
dataString.AppendFormat("{0}\n",dataView[i][columns[j]]);
}
else
{
dataString.AppendFormat("{0}\t", dataView[i][columns[j]]);
}

}
catch
{ }
}
}
}
else
{
for (int i = 0; i <= dataView.Table.Columns.Count - 1; i++)
{
dataString.Append(dataView.Table.Columns[i].Caption);
if (i == dataView.Table.Columns.Count - 1)
{
dataString.Append("\n");
}
else
{
dataString.Append("\t");
}
}

for (int i = 0; i < dataView.Count; i++)
{
for (int j = 0; j < dataView.Table.Columns.Count; j++)
{
if (j == dataView.Table.Columns.Count - 1)
{
dataString.AppendFormat("{0}\n",dataView.Table.Rows[i].ItemArray[j]);
}
else
{
dataStri