Hello Trapulo,
I find a few more methods to add the x:str="'***" into the table cells.
Below is a summary of the solution.
Step1. Prepare the html header of export excel yourself. (NOTE: do not let
Excel itself automatically add the header, otherwise, the namespace in
your
html would be replace)
String header = "<html xmlns
=\"urn:schemas-microsoft-com
ffice
ffice\"
xmlns:x=\"urn:schemas-microsoft-com
ffice:excel\"
xmlns=\"
http://www.w3.org/TR/REC-html40\"> <head> <meta
http-equiv=Content-Type content=\"text/html; charset=windows-1252\">
<meta
name=ProgId content=Excel.Sheet> <meta name=Generator content=\"Microsoft
Excel 11\"> <style id=\"STI_5961_Styles\"><!--table
{mso-displayed-decimal-separator:\"\\.\";
mso-displayed-thousand-separator:\"\\,\";}.xlGeneral {padding-top:1px;
padding-right:1px; padding-left:1px; mso-ignore
adding; color:windowtext;
font-size:10.0pt; font-weight:400; font-style:normal;
text-decoration:none;
font-family:Arial; mso-generic-font-family:auto; mso-font-charset:0;
mso-number-format:General; text-align:general; vertical-align:bottom;
mso-background-source:auto; mso-pattern:auto; white-space:nowrap;}
</style>--></head><body>"
Step2. Prepare the Response object
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=test.xls");
Response.ContentType = "application/vnd.xls";
Step3. Render the GridView
Now that you are using gridView.RenderControl(htmlTextWriter); to get it
html string, in order to add x:str="'***" into the table cells, here are
two approaches for your reference:
Approach 1. Add the attribute "x:str" to each cell of GridView before it's
rendered.
foreach (GridViewRow row in view.Rows) {
if (row.RowType == DataControlRowType.DataRow) {
foreach (TableCell cell in row.Cells) {
if (int.TryParse(cell.Text)) {
cell.Attributes.Add("x:str", "'" + cell.Text);
}}}}
Approach 2. Get the GridView's html first, then add the x:str into <td>
cells with Regex
Suppose the string htmlStr point to the render result of GridView. We
could
use the regex:
<td (?<tdAttributes>[^>]*?)>(?<IntValue>\d*)</td>
to capture all the table cells with integer value. The group
(?<intValue\d*) refers to the int value, and the group
<tdAttributes>[^>]*?) is the original td attributes. We add a
MatchEvaluator function for the regex's Relace method:
public string replaceFunction(Match m)
{
String intValue = m.Groups["IntValue"].Value;
String tdAttributes = m.Groups["tdAttributes"].Value;
return "<td " + tdAttributes + " x:str=\"'" + intValue + "\">" + intValue
+
"</td>";
}
Step4.
Response.Write(the html result string);
Response.End();
The methods above works well in my side. Please have a try and let me know
the result. If you have any other concern or need anything else, please
feel free to let me know.
Sincerely,
Jialiang Ge (
[email protected], remove 'online.')
Microsoft Online Community Support
=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.