Export to Excel issue (C#)

A

Arvind R

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.ConfigurationSettings.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();
 
S

S. Justin Gengo [MCP]

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
 
G

Guest

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 said:
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 said:
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.ConfigurationSettings.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();



--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top