EDN Admin
Well-known member
Hello ,<br/><br/> I have the following code to Convert the DataSet to Excel File :<br/><br/> <span style="font-size:x-small;color:#0000ff <span style="font-size:x-small;color:#0000ff <font size=2 color="#0000ff <font size=2 color="#0000ff
public
</font></font><font size=2 color="#0000ff
</font>
<span style="font-size:x-small <span style="font-size:x-small;color:#0000ff <span style="font-size:x-small;color:#0000ff static<span style="font-size:x-small <span style="font-size:x-small;color:#0000ff <span style="font-size:x-small;color:#0000ff void<span style="font-size:x-small ExportDataSetToExcel(<span style="font-size:x-small;color:#008080 <span style="font-size:x-small;color:#008080 DataSet<span style="font-size:x-small ds, <span style="font-size:x-small;color:#0000ff <span style="font-size:x-small;color:#0000ff string<span style="font-size:x-small filename)<br/>{<br/><span style="font-size:x-small;color:#008080 <span style="font-size:x-small;color:#008080 HttpResponse<span style="font-size:x-small response = <span style="font-size:x-small;color:#008080 <span style="font-size:x-small;color:#008080 HttpContext<span style="font-size:x-small .Current.Response;<br/>response.Clear();<br/>response.Charset = <span style="font-size:x-small;color:#800000 <span style="font-size:x-small;color:#800000 ""<span style="font-size:x-small ;<br/>response.ContentType = <span style="font-size:x-small;color:#800000 <span style="font-size:x-small;color:#800000 "application/vnd.ms-excel"<span style="font-size:x-small ;<br/>response.AddHeader(<span style="font-size:x-small;color:#800000 <span style="font-size:x-small;color:#800000 "Content-Disposition"<span style="font-size:x-small , <span style="font-size:x-small;color:#800000 <span style="font-size:x-small;color:#800000 "attachment;filename=""<span style="font-size:x-small + filename + <span style="font-size:x-small;color:#800000 <span style="font-size:x-small;color:#800000 """<span style="font-size:x-small );<br/><span style="font-size:x-small;color:#0000ff <span style="font-size:x-small;color:#0000ff using<span style="font-size:x-small (<span style="font-size:x-small;color:#008080 <span style="font-size:x-small;color:#008080 StringWriter<span style="font-size:x-small sw = <span style="font-size:x-small;color:#0000ff <span style="font-size:x-small;color:#0000ff new<span style="font-size:x-small <span style="font-size:x-small;color:#008080 <span style="font-size:x-small;color:#008080 StringWriter<span style="font-size:x-small ())<br/>{<br/><span style="font-size:x-small;color:#0000ff <span style="font-size:x-small;color:#0000ff using<span style="font-size:x-small (<span style="font-size:x-small;color:#008080 <span style="font-size:x-small;color:#008080 HtmlTextWriter<span style="font-size:x-small htw = <span style="font-size:x-small;color:#0000ff <span style="font-size:x-small;color:#0000ff new<span style="font-size:x-small <span style="font-size:x-small;color:#008080 <span style="font-size:x-small;color:#008080 HtmlTextWriter<span style="font-size:x-small (sw))<br/>{<br/><span style="font-size:x-small;color:#008080 <span style="font-size:x-small;color:#008080 DataGrid<span style="font-size:x-small dg = <span style="font-size:x-small;color:#0000ff <span style="font-size:x-small;color:#0000ff new<span style="font-size:x-small <span style="font-size:x-small;color:#008080 <span style="font-size:x-small;color:#008080 DataGrid<span style="font-size:x-small ();<br/>dg.DataSource = ds.Tables[0];<br/>dg.DataBind();<br/>dg.RenderControl(htw);<br/>response.Write(sw.ToString());<br/>response.End();<br/>}<br/>}<br/>}<br/><br/>The code works fine and the File is Created . but in the file there is two problems :<br/><br/> 1- When Open the file I show the following message :<br/><br/> "The file is trying to open is in different format than specified by the file extension . verify that the file is not Corrupted <br/> and is from a trusted source before opening the file . Do u want to open the file ?<br/><br/> 2- When Press yes , then the file is Opened . I see the Columns created from right to left .<br/><br/><br/>so : <br/><br/> - How Can I perevnt the message to appear .<br/><br/> - How Can Columns created from Left to Right in English Culture and Right to Left in Arabic Culture .<br/><br/> Thanks <hr class="sig ASk
View the full article
public
</font></font><font size=2 color="#0000ff
</font>
<span style="font-size:x-small <span style="font-size:x-small;color:#0000ff <span style="font-size:x-small;color:#0000ff static<span style="font-size:x-small <span style="font-size:x-small;color:#0000ff <span style="font-size:x-small;color:#0000ff void<span style="font-size:x-small ExportDataSetToExcel(<span style="font-size:x-small;color:#008080 <span style="font-size:x-small;color:#008080 DataSet<span style="font-size:x-small ds, <span style="font-size:x-small;color:#0000ff <span style="font-size:x-small;color:#0000ff string<span style="font-size:x-small filename)<br/>{<br/><span style="font-size:x-small;color:#008080 <span style="font-size:x-small;color:#008080 HttpResponse<span style="font-size:x-small response = <span style="font-size:x-small;color:#008080 <span style="font-size:x-small;color:#008080 HttpContext<span style="font-size:x-small .Current.Response;<br/>response.Clear();<br/>response.Charset = <span style="font-size:x-small;color:#800000 <span style="font-size:x-small;color:#800000 ""<span style="font-size:x-small ;<br/>response.ContentType = <span style="font-size:x-small;color:#800000 <span style="font-size:x-small;color:#800000 "application/vnd.ms-excel"<span style="font-size:x-small ;<br/>response.AddHeader(<span style="font-size:x-small;color:#800000 <span style="font-size:x-small;color:#800000 "Content-Disposition"<span style="font-size:x-small , <span style="font-size:x-small;color:#800000 <span style="font-size:x-small;color:#800000 "attachment;filename=""<span style="font-size:x-small + filename + <span style="font-size:x-small;color:#800000 <span style="font-size:x-small;color:#800000 """<span style="font-size:x-small );<br/><span style="font-size:x-small;color:#0000ff <span style="font-size:x-small;color:#0000ff using<span style="font-size:x-small (<span style="font-size:x-small;color:#008080 <span style="font-size:x-small;color:#008080 StringWriter<span style="font-size:x-small sw = <span style="font-size:x-small;color:#0000ff <span style="font-size:x-small;color:#0000ff new<span style="font-size:x-small <span style="font-size:x-small;color:#008080 <span style="font-size:x-small;color:#008080 StringWriter<span style="font-size:x-small ())<br/>{<br/><span style="font-size:x-small;color:#0000ff <span style="font-size:x-small;color:#0000ff using<span style="font-size:x-small (<span style="font-size:x-small;color:#008080 <span style="font-size:x-small;color:#008080 HtmlTextWriter<span style="font-size:x-small htw = <span style="font-size:x-small;color:#0000ff <span style="font-size:x-small;color:#0000ff new<span style="font-size:x-small <span style="font-size:x-small;color:#008080 <span style="font-size:x-small;color:#008080 HtmlTextWriter<span style="font-size:x-small (sw))<br/>{<br/><span style="font-size:x-small;color:#008080 <span style="font-size:x-small;color:#008080 DataGrid<span style="font-size:x-small dg = <span style="font-size:x-small;color:#0000ff <span style="font-size:x-small;color:#0000ff new<span style="font-size:x-small <span style="font-size:x-small;color:#008080 <span style="font-size:x-small;color:#008080 DataGrid<span style="font-size:x-small ();<br/>dg.DataSource = ds.Tables[0];<br/>dg.DataBind();<br/>dg.RenderControl(htw);<br/>response.Write(sw.ToString());<br/>response.End();<br/>}<br/>}<br/>}<br/><br/>The code works fine and the File is Created . but in the file there is two problems :<br/><br/> 1- When Open the file I show the following message :<br/><br/> "The file is trying to open is in different format than specified by the file extension . verify that the file is not Corrupted <br/> and is from a trusted source before opening the file . Do u want to open the file ?<br/><br/> 2- When Press yes , then the file is Opened . I see the Columns created from right to left .<br/><br/><br/>so : <br/><br/> - How Can I perevnt the message to appear .<br/><br/> - How Can Columns created from Left to Right in English Culture and Right to Left in Arabic Culture .<br/><br/> Thanks <hr class="sig ASk
View the full article