I tried running this program using a simple excel template of my own and using your same code. I have an asp page with a button so when u click it it generates the sample.xls file. Only problem is when i open it(or save and open it) i get a strict parse
error and the sample.xls file wont open. But, the sample.xls file that is saved in my project folder opens perfectly in excel and displays the data just like it should. Does anyone have any idea on why this is happening and how I can fix it?
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using Excel;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ajax_kid
Member
4 Points
7 Posts
Re: Dynamically Generate An Excel spreadsheet in asp.net
May 11, 2008 02:57 AM|LINK
hi,
I tried running this program using a simple excel template of my own and using your same code. I have an asp page with a button so when u click it it generates the sample.xls file. Only problem is when i open it(or save and open it) i get a strict parse error and the sample.xls file wont open. But, the sample.xls file that is saved in my project folder opens perfectly in excel and displays the data just like it should. Does anyone have any idea on why this is happening and how I can fix it?
here is the SampleRpt.xml file:
<?xml version="1.0"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Author>Abel</Author>
<LastAuthor>Abel</LastAuthor>
<Created>2008-05-10T00:19:22Z</Created>
<LastSaved>2008-05-10T00:21:24Z</LastSaved>
<Company> Barr-era</Company>
<Version>10.3501</Version>
</DocumentProperties>
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
<DownloadComponents/>
<LocationOfComponents href="file:///E:\"/>
</OfficeDocumentSettings>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>9210</WindowHeight>
<WindowWidth>11355</WindowWidth>
<WindowTopX>480</WindowTopX>
<WindowTopY>60</WindowTopY>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s21">
<Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
<Font x:Family="Swiss" ss:Bold="1"/>
<Interior ss:Color="#00CCFF" ss:Pattern="Solid"/>
</Style>
<Style ss:ID="s22">
<Alignment ss:Horizontal="Center" ss:Vertical="Bottom"/>
</Style>
</Styles>
<Worksheet ss:Name="Sheet1">
<Table ss:ExpandedColumnCount="3" ss:ExpandedRowCount="25" x:FullColumns="1"
x:FullRows="1">
<Column ss:AutoFitWidth="0" ss:Width="106.5"/>
<Column ss:AutoFitWidth="0" ss:Width="179.25"/>
<Column ss:AutoFitWidth="0" ss:Width="79.5"/>
<Row>
<Cell ss:StyleID="s21">
<Data ss:Type="String">SID</Data>
</Cell>
<Cell ss:StyleID="s21">
<Data ss:Type="String">Student Name</Data>
</Cell>
<Cell ss:StyleID="s21">
<Data ss:Type="String">User ID</Data>
</Cell>
</Row>
[[rows]]
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<Print>
<ValidPrinterInfo/>
<PaperSizeIndex>9</PaperSizeIndex>
<HorizontalResolution>200</HorizontalResolution>
<VerticalResolution>200</VerticalResolution>
<NumberofCopies>0</NumberofCopies>
</Print>
<Selected/>
<Panes>
<Pane>
<Number>3</Number>
<ActiveRow>1</ActiveRow>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
<Worksheet ss:Name="Sheet2">
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
<Worksheet ss:Name="Sheet3">
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>
and SampleRow.xml:
<Row>
<Cell ss:StyleID="s22">
<Data ss:Type="Number">[[sid]]</Data>
</Cell>
<Cell ss:StyleID="s22">
<Data ss:Type="String">[[name]]</Data>
</Cell>
<Cell ss:StyleID="s22">
<Data ss:Type="String">[[userID]]</Data>
</Cell>
</Row>
finally the code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using Excel;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Context.Response.Clear();
Context.Response.Charset = "";
Context.Response.AddHeader("content-disposition", "attachment;filename=Sample2.xls");
Context.Response.ContentType = "application/vnd.ms-excel";
// read in the SampleRpt.xml file
string rptxml;
StreamReader sr = new StreamReader(Context.Server.MapPath("App_Data/SampleRpt.xml"));
rptxml = sr.ReadToEnd();
sr.Close();
sr.Dispose();
// read in the Samplerow.xml file
string rowxml;
StreamReader sr2 = new StreamReader(Context.Server.MapPath("App_Data/SampleRow.xml"));
rowxml = sr2.ReadToEnd();
sr2.Close();
sr2.Dispose();
// create the rows
System.Text.StringBuilder sb = new System.Text.StringBuilder();
string thisRow;
thisRow = rowxml;
thisRow = thisRow.Replace("[[sid]]", "1242254");
thisRow = thisRow.Replace("[[name]]", "Smith,John");
thisRow = thisRow.Replace("[[userID]]", "jsmith");
sb.Append(thisRow);
thisRow = rowxml;
thisRow = thisRow.Replace("[[sid]]", "5555555");
thisRow = thisRow.Replace("[[name]]", "Moreno,Alex");
thisRow = thisRow.Replace("[[userID]]", "amoreno");
sb.Append(thisRow);
thisRow = rowxml;
thisRow = thisRow.Replace("[[sid]]", "12525745");
thisRow = thisRow.Replace("[[name]]", "Vivino,Jimmy");
thisRow = thisRow.Replace("[[userID]]", "jimmyv");
sb.Append(thisRow);
thisRow = rowxml;
thisRow = thisRow.Replace("[[sid]]", "1542845");
thisRow = thisRow.Replace("[[name]]", "Dartles,Tim");
thisRow = thisRow.Replace("[[userID]]", "dartles");
sb.Append(thisRow);
rptxml = rptxml.Replace("[[rows]]", sb.ToString());
// write out the xml
Context.Response.Write(rptxml);
StreamWriter sw = new StreamWriter(Context.Server.MapPath("./Sample.xls"));
sw.Write(rptxml);
sw.Close();
sw.Dispose();
Context.Response.TransmitFile("Sample.xls");
}
}
any help would be greatly appreciated. thanks.