Useful export helper class
Below is a helper class that I use for exporting a DataGrids contents to an external file. The below code references gridViewText, but I haven't tested it out on a GridView yet as the code is from an application I'm working on upgrading. But, it works like a charm on the DataGrid.
public sealed class
ExportHelper
{
public static void
ExportToCsv(
string
gridViewText,
string
contentType,
HttpResponse
response)
{
const string
m_Delimiter_Column =
","
;
string
m_Delimiter_Row =
Environment
.NewLine;
response.ContentType = contentType;
Regex
m_RegEx =
new
Regex
(@
"(>\s+<)"
,
RegexOptions
.IgnoreCase);
gridViewText = m_RegEx.Replace(gridViewText,
"><"
);
gridViewText = gridViewText.Replace(m_Delimiter_Row,
String
.Empty);
gridViewText = gridViewText.Replace(
""
, m_Delimiter_Row);
gridViewText = gridViewText.Replace(
"
"
,
String
.Empty);
gridViewText = gridViewText.Replace(m_Delimiter_Column,
"\\"
+ m_Delimiter_Column);
gridViewText = gridViewText.Replace(
"
|
"
, m_Delimiter_Column);
m_RegEx =
new
Regex
(@
"<[^>]*>"
,
RegexOptions
.IgnoreCase);
gridViewText = m_RegEx.Replace(gridViewText,
String
.Empty);
gridViewText =
HttpUtility
.HtmlDecode(gridViewText);
response.Write(gridViewText);
response.End();
}
public static void
ExportToExcelWord(
string
gridViewText,
string
contentType,
HttpResponse
response)
{
response.ContentType = contentType;
response.Write(gridViewText);
response.End();
}
}
New Comments to this post are disabled
|