I have code that uses an XSL stylesheet to transform a dataset into a file that MS Excel can read. Below is a simplified version of the code. This works..for the most part. It sucessfully sends the excel file to the client, but only if the client saves it to thier HD can they open it. If they click the open button (instead of Save As), Excel opens and says it cannot find the file.
Any Ideas??
protected void Page_Load(Objec t sender, EventArgs e)
{
Database db = new Database();
String ls_file;
//db.ApplicantsBy Specialty returns valid dataset
ls_file = DataSetTransfor m(db.Applicants BySpecialty("as pecialty"), Server.MapPath( "Styles/Excel.xsl"));
db = null;
Response.Conten tType = "applicatio n/vnd.ms-excel";
Response.AddHea der("Content-disposition", "attachment ; filename=\"exce l.xls\"");
Response.Charse t = "";
Response.Write( ls_file);
Response.Flush( );
Response.End();
}
public string DataSetTransfor m(DataSet ds, String as_stylesheet_p ath)
{
XmlDataDocument xmlDataDoc = new XmlDataDocument (ds);
XslTransform xt = new XslTransform();
StreamReader reader = new StreamReader(as _stylesheet_pat h);
XmlTextReader xRdr = new XmlTextReader(r eader);
xt.Load(xRdr, null, null);
StringWriter sw = new StringWriter();
xt.Transform(xm lDataDoc, null, sw, null);
xmlDataDoc = null;
xt = null;
sw.Close();
reader.Close();
xRdr.Close();
return sw.ToString();
}
Any Ideas??
protected void Page_Load(Objec t sender, EventArgs e)
{
Database db = new Database();
String ls_file;
//db.ApplicantsBy Specialty returns valid dataset
ls_file = DataSetTransfor m(db.Applicants BySpecialty("as pecialty"), Server.MapPath( "Styles/Excel.xsl"));
db = null;
Response.Conten tType = "applicatio n/vnd.ms-excel";
Response.AddHea der("Content-disposition", "attachment ; filename=\"exce l.xls\"");
Response.Charse t = "";
Response.Write( ls_file);
Response.Flush( );
Response.End();
}
public string DataSetTransfor m(DataSet ds, String as_stylesheet_p ath)
{
XmlDataDocument xmlDataDoc = new XmlDataDocument (ds);
XslTransform xt = new XslTransform();
StreamReader reader = new StreamReader(as _stylesheet_pat h);
XmlTextReader xRdr = new XmlTextReader(r eader);
xt.Load(xRdr, null, null);
StringWriter sw = new StringWriter();
xt.Transform(xm lDataDoc, null, sw, null);
xmlDataDoc = null;
xt = null;
sw.Close();
reader.Close();
xRdr.Close();
return sw.ToString();
}
Comment