467,188 Members | 1,507 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,188 developers. It's quick & easy.

Export to Excel issue (C#)

Hello,

how to ask saveas dialog before writing the data to the excel file?
right now im able to save in c drive or any other specified location only.

any solution will be a great help!

System.Text.StringBuilder sbrHTML=new System.Text.StringBuilder("");

//StringBuilder sbrHTML = new StringBuilder();
SqlConnection cn = new
SqlConnection(System.Configuration.ConfigurationSe ttings.AppSettings["connectionString"]);

SqlCommand dc = new SqlCommand("Select * From wc_ingredient",cn);

SqlDataReader dr ;

int i=0;

cn.Open();

dr = dc.ExecuteReader();

//Making HTML

sbrHTML.Append("<TABLE Border=1 ID='Table1'>");

sbrHTML.Append("<TR><TD ColSpan=35><Font Size=5>Report</Font></TD><TR>");

sbrHTML.Append("<TR><TH>Id</TH><TH>Ingredient
Name</TH><TH>measure</TH><TH>Cost</TH><TH>Onhand Qty</TH><TH>Supplier
Code</TH><TH>Supplier Name</TH><TH>Supplier purchase unit</TH><TH>Supplier
Factor</TH><TH>Onhand Factor</TH><TH>Supplier Brand</TH><TH>Supplier
Case</TH><TH>Supplier Purchase unit</TH><TH>Supplier use
unit</TH><TH>Supplier cost unit</TH></TR>");

while(dr.Read())

{

i++;

sbrHTML.Append("<TR><TD>" + dr.GetValue(0).ToString() + "</TD><TD>"

+ dr.GetValue(1).ToString() + "</TD><TD>"

+ dr.GetValue(2).ToString() + "</TD><TD>"

+ dr.GetValue(3).ToString() + "</TD><TD>"

+ "</TR>" );

}

sbrHTML.Append("</TABLE>");

sbrHTML.Append("<BR><BR><B>Total Number of Ingredients are " + i.ToString()
+ "</B>");

string path="c:\\Ingredient_Report.xls";

StreamWriter swXLS =new StreamWriter("c:\\Report.xls");

swXLS.Write(sbrHTML.ToString());
swXLS.Close();

--
--
Thanks,

Arvind
Jan 24 '06 #1
  • viewed: 6261
Share:
2 Replies
Arvind,

I have an example on my website about how to output a datagrid as an excel
spreadsheet. It's very similar to what you're doing here. You may want to
take a look at the code: http://www.aboutfortunate.com?page=codelibrary.
It's the second example in the datagrid.

Also, if you want to force the save as dialog check out this article:
http://aspdot.net/articles/forcedialogandcompression/

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Arvind R" <arvind r@erivasystems.com> wrote in message
news:uo**************@TK2MSFTNGP14.phx.gbl...
Hello,

how to ask saveas dialog before writing the data to the excel file?
right now im able to save in c drive or any other specified location only.

any solution will be a great help!

System.Text.StringBuilder sbrHTML=new System.Text.StringBuilder("");

//StringBuilder sbrHTML = new StringBuilder();
SqlConnection cn = new
SqlConnection(System.Configuration.ConfigurationSe ttings.AppSettings["connectionString"]);

SqlCommand dc = new SqlCommand("Select * From wc_ingredient",cn);

SqlDataReader dr ;

int i=0;

cn.Open();

dr = dc.ExecuteReader();

//Making HTML

sbrHTML.Append("<TABLE Border=1 ID='Table1'>");

sbrHTML.Append("<TR><TD ColSpan=35><Font Size=5>Report</Font></TD><TR>");

sbrHTML.Append("<TR><TH>Id</TH><TH>Ingredient
Name</TH><TH>measure</TH><TH>Cost</TH><TH>Onhand Qty</TH><TH>Supplier
Code</TH><TH>Supplier Name</TH><TH>Supplier purchase unit</TH><TH>Supplier
Factor</TH><TH>Onhand Factor</TH><TH>Supplier Brand</TH><TH>Supplier
Case</TH><TH>Supplier Purchase unit</TH><TH>Supplier use
unit</TH><TH>Supplier cost unit</TH></TR>");

while(dr.Read())

{

i++;

sbrHTML.Append("<TR><TD>" + dr.GetValue(0).ToString() + "</TD><TD>"

+ dr.GetValue(1).ToString() + "</TD><TD>"

+ dr.GetValue(2).ToString() + "</TD><TD>"

+ dr.GetValue(3).ToString() + "</TD><TD>"

+ "</TR>" );

}

sbrHTML.Append("</TABLE>");

sbrHTML.Append("<BR><BR><B>Total Number of Ingredients are " +
i.ToString() + "</B>");

string path="c:\\Ingredient_Report.xls";

StreamWriter swXLS =new StreamWriter("c:\\Report.xls");

swXLS.Write(sbrHTML.ToString());
swXLS.Close();

--
--
Thanks,

Arvind

Jan 24 '06 #2
I see a lot of the same code out there to export data from an ASP.NET
DataGrid to Excel. All the solutions are pretty similar. One thing I have
noticed with the solution(s) provided is that if a user opens the file,
rather than saving it, the Excel file will open in the user's browser window
and disable the Back button. How can you allow a user to open the Excel file
and be able to use the Back button to get back to the site?

Thanks!

Brian
"S. Justin Gengo [MCP]" wrote:
Arvind,

I have an example on my website about how to output a datagrid as an excel
spreadsheet. It's very similar to what you're doing here. You may want to
take a look at the code: http://www.aboutfortunate.com?page=codelibrary.
It's the second example in the datagrid.

Also, if you want to force the save as dialog check out this article:
http://aspdot.net/articles/forcedialogandcompression/

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Arvind R" <arvind r@erivasystems.com> wrote in message
news:uo**************@TK2MSFTNGP14.phx.gbl...
Hello,

how to ask saveas dialog before writing the data to the excel file?
right now im able to save in c drive or any other specified location only.

any solution will be a great help!

System.Text.StringBuilder sbrHTML=new System.Text.StringBuilder("");

//StringBuilder sbrHTML = new StringBuilder();
SqlConnection cn = new
SqlConnection(System.Configuration.ConfigurationSe ttings.AppSettings["connectionString"]);

SqlCommand dc = new SqlCommand("Select * From wc_ingredient",cn);

SqlDataReader dr ;

int i=0;

cn.Open();

dr = dc.ExecuteReader();

//Making HTML

sbrHTML.Append("<TABLE Border=1 ID='Table1'>");

sbrHTML.Append("<TR><TD ColSpan=35><Font Size=5>Report</Font></TD><TR>");

sbrHTML.Append("<TR><TH>Id</TH><TH>Ingredient
Name</TH><TH>measure</TH><TH>Cost</TH><TH>Onhand Qty</TH><TH>Supplier
Code</TH><TH>Supplier Name</TH><TH>Supplier purchase unit</TH><TH>Supplier
Factor</TH><TH>Onhand Factor</TH><TH>Supplier Brand</TH><TH>Supplier
Case</TH><TH>Supplier Purchase unit</TH><TH>Supplier use
unit</TH><TH>Supplier cost unit</TH></TR>");

while(dr.Read())

{

i++;

sbrHTML.Append("<TR><TD>" + dr.GetValue(0).ToString() + "</TD><TD>"

+ dr.GetValue(1).ToString() + "</TD><TD>"

+ dr.GetValue(2).ToString() + "</TD><TD>"

+ dr.GetValue(3).ToString() + "</TD><TD>"

+ "</TR>" );

}

sbrHTML.Append("</TABLE>");

sbrHTML.Append("<BR><BR><B>Total Number of Ingredients are " +
i.ToString() + "</B>");

string path="c:\\Ingredient_Report.xls";

StreamWriter swXLS =new StreamWriter("c:\\Report.xls");

swXLS.Write(sbrHTML.ToString());
swXLS.Close();

--
--
Thanks,

Arvind


Feb 24 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Prasad Dannani | last post: by
3 posts views Thread by Marja Ribbers-de Vroed | last post: by
13 posts views Thread by Hemant Sipahimalani | last post: by
5 posts views Thread by karthick | last post: by
1 post views Thread by smaczylo@gmail.com | last post: by
19 posts views Thread by cj2 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.