Ask a Question related to ASP.NET General, Design and Development.
-
Kevin Blakeley #1
Export to a Datagrid, YET AGAIN!
I know this was just posted but I did not want this message to get lost in
the other thread as it's slightly different.
Yes I want to export my dataset to excel for my clients, but I don't want it
to show in the browser. Everything I have tried makes it so that IE will
load excel within the browser session and view the file in there. What I
want is for the user to be prompted with the save dialog to save the excel
file disk. How do I do this?
Real simple code:
Response.ContentType = "application/vnd.ms-excel" ;
Response.Redirect("ITErrorLog.csv",true);
Any ideas?
Kevin Blakeley Guest
-
Export Datagrid to PDF
Okay, I have code here to export a datagrid to Excel. Can I modify it to export it to a PDF? Thanks, Function ExcelExport(ByVal DatagridID,... -
Export Datagrid to excel
Hi, I have written the code to export my datagrid to excel. it is working fine. But i have one issue, when ever i click on export button, it... -
Export DataGrid
Hi, Is there anyway to export the data in a DataGrid to excel file? Derek -
Display a Datagrid & Export a Datagrid to Excel
I have a page displaying a datagrid and the user wants to have two options: 1. Export the current "Displayed" datagrid to Excel 2. Export another... -
DataGrid export to excel
I've asked this question once before, but am still having problems. When I try to export this grid to excel i get the error message i've seen... -
George Durzi #2
Re: Export to a Datagrid, YET AGAIN!
I know you asked that you didn't want the Excel to be displayed in the
browser, but you can just cut that out of this code. This code exports to an
Excel file, displays it in the browser, then deletes the Excel file.
The trick here is that you're using Xsl to transform a DataSet into HTML,
then saving it with a .Xls extension. This code obviously won't compile
since I'm referencing a bunch of stuff and values in web.config, but it
should give you the general idea
// get back the search results
DataSet dsSearch = ExecuteSearchReturnDataSet();
// write them out to disk
string exportPath = Facade.Export.ExportDataSet(
dsSearch,
Session["UserLogin"].ToString(),
ConfigurationSettings.AppSettings["ReportPath"].ToString(),
ConfigurationSettings.AppSettings["ApplicationPath"].ToString() +
@"xsl\greenbook_search.xsl",
Convert.ToInt32(cUIHelper.ExportFormat.ExportXls). ToString());
// Write the Report to the Browser
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/vnd.ms-excel";
Response.WriteFile(exportPath);
Response.Flush();
Response.Close();
System.IO.File.Delete(exportPath);
#region ExportDataSet UserName, FilePath, XslPath, ExportFormatChoice
public static string ExportDataSet(
DataSet SearchResults,
string UserName,
string FilePath,
string XslPath,
string ExportFormatChoice)
{
string exportPath = "";
switch ((ExportFormat)Convert.ToInt32(ExportFormatChoice) )
{
case (ExportFormat.ExportXls):
exportPath = FilePath + UserName + "_SearchResults.xls";
break;
case (ExportFormat.ExportHtml):
exportPath = FilePath + UserName + "_SearchResults.html";
break;
}
// Create a FileStream to write with
System.IO.FileStream fs = new System.IO.FileStream(
exportPath, System.IO.FileMode.Create);
// Create an XmlTextWriter for the FileStream
System.Xml.XmlTextWriter xtw = new System.Xml.XmlTextWriter(
fs, System.Text.Encoding.Unicode);
try
{
XmlDataDocument xmlDoc = new XmlDataDocument(SearchResults);
System.Xml.Xsl.XslTransform xslTran = new System.Xml.Xsl.XslTransform();
xslTran.Load(XslPath);
xslTran.Transform(xmlDoc, null, xtw);
xtw.Close();
return exportPath;
}
catch (Exception ex)
{
xtw.Close();
System.IO.File.Delete(exportPath);
ExceptionManager.Publish(ex);
throw(ex);
}
}
#endregion
"Kevin Blakeley" <MSNews@whoiskb.com> wrote in message
news:OCm%23uK1ODHA.2228@tk2msftngp13.phx.gbl...it> I know this was just posted but I did not want this message to get lost in
> the other thread as it's slightly different.
>
> Yes I want to export my dataset to excel for my clients, but I don't want> to show in the browser. Everything I have tried makes it so that IE will
> load excel within the browser session and view the file in there. What I
> want is for the user to be prompted with the save dialog to save the excel
> file disk. How do I do this?
>
> Real simple code:
> Response.ContentType = "application/vnd.ms-excel" ;
> Response.Redirect("ITErrorLog.csv",true);
>
> Any ideas?
>
>
George Durzi Guest



Reply With Quote

