473,385 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
2 6524
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Saur | last post by:
Hi, I am using an export to excel functionality from my ASP page. I have set the content type as Response.ContentType = "application/vnd.ms-excel" The data i am exporting has values like 1-2,...
4
by: Gary Wright | last post by:
I have an Access 2K database split into front and back. Quite often the users want to do some data analysis that I have not created a report for so they want to export some subset of the data into...
6
by: Robin Cushman | last post by:
Hi all, I need some help -- I'm working with an A2K database, using DAO, and am trying to read records into a Crystal Report and then export it to a folder on our network as an Excel...
4
by: Prasad Dannani | last post by:
Hi, We have to export our data in sql server table to any excel sheet. We have a template to export, using oledb we are inserting rows to this template and generating the excel output. The...
3
by: Marja Ribbers-de Vroed | last post by:
Hi, One of my clients has a strange problem with my webapplication which I think may be related to some firewall setting on his computer. On several screens in the application, it is possible...
13
by: Hemant Sipahimalani | last post by:
The following piece of code is being used to export HTML to excel. HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"...
5
by: karthick | last post by:
Hi, I am exporting a Gridview to Excel and it works fine without any issues. But as one of the field holds values such as "71646E100" it gets converted to: "7.16E+104" (like a formula) in...
1
by: smaczylo | last post by:
Hello, I've recently been asked to work with Microsoft Access, and while I feel quite comfortable with Excel, I'm at a complete loss with databases. If someone could help me with this issue I'm...
2
by: leena13 | last post by:
Hi, The number of rows in the datagrid on my page are approx. around 14130. When I click on the button that exports the records to excel sheet, the page goes blank (I observed that none of the...
19
by: cj2 | last post by:
#1 Is there a quick way to export a datatable to an excel file? Or delimited file? #2 Where is the appropriate Microsoft monitored group to ask about writing reports in SQL Reporting services...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.