2014年7月13日 星期日

[RESOLVED] How to Set Excel file in format cell as text ?


Dear All,


 May i know how to set the Excel file in   format cell as  text ? kindly advise, thank you


As below is the Script 


protected void WExportToCSV(Object sender, EventArgs e)
{

//Export the GridView to Excel

PrepareGridViewForExport(EMMSTransHist);

ExportGridView();

}



private void ExportGridView()
{

string attachment = "attachment; filename=EMMS_TransHistReport.xls";

Response.ClearContent();

Response.AddHeader("content-disposition", attachment);

Response.ContentType = "application/ms-excel";

StringWriter sw = new StringWriter();

HtmlTextWriter htw = new HtmlTextWriter(sw);

EMMSTransHist.RenderControl(htw);

Response.Write(sw.ToString());

Response.End();

}



public override void VerifyRenderingInServerForm(Control control)
{

}





private void PrepareGridViewForExport(Control gv)
{

LinkButton lb = new LinkButton();

Literal l = new Literal();

string name = String.Empty;

for (int i = 0; i < gv.Controls.Count; i++)
{

if (gv.Controls[i].GetType() == typeof(LinkButton))
{

l.Text = (gv.Controls[i] as LinkButton).Text;

gv.Controls.Remove(gv.Controls[i]);

gv.Controls.AddAt(i, l);

}

else if (gv.Controls[i].GetType() == typeof(DropDownList))
{

l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;

gv.Controls.Remove(gv.Controls[i]);

gv.Controls.AddAt(i, l);

}

else if (gv.Controls[i].GetType() == typeof(CheckBox))
{

l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False";

gv.Controls.Remove(gv.Controls[i]);

gv.Controls.AddAt(i, l);

}

if (gv.Controls[i].HasControls())
{

PrepareGridViewForExport(gv.Controls[i]);

}

}
}







What I have understood from your problem is that, you only want to remove the style or specific format on data, then you can simply remove it with the help of following line of code,


HtmlTextWriter htw = new HtmlTextWriter(sw);

EMMSTransHist.RenderControl(htw);
var newString = sw.ToString().Replace("old format","New format");
Response.Write(newString);

thanks,



hi,


    Acording to your  description, now I  give piece of code for your reference, the key place in bold and marked red , try doing ,plus you can solve your problem.


 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim style As String = "