Hi inexplicable,
May following will help you.
C# class library for exporting data to CSV/Excel file
http://www.codeproject.com
Export Data to CSV ( Excel )
http://www.dotnetspider.co
Cheers!
Main Topics
Browse All TopicsI've got a datagrid on my page along with a button which when clicked should generate the excel sheet. I've got the code to work for generating the excel sheet the same manner the datagrid is displayed. But there is one column in the datagrid which i need to get value for and then save to excel. For instance if the column has "1" as value, before exporting i need to check that and display and answer for it. For "1" it would be "xxxx", "2" - xxxxx..and so on
here's what ive :
private void Button1_Click(object sender, System.EventArgs e)
{
Response.Clear();
Response.Buffer= true;
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWrit
this.ClearControls(ItemLis
datagrid.RenderControl(oHt
Response.Write(oStringWrit
Response.End();
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Hi inexplicable,
May following will help you.
C# class library for exporting data to CSV/Excel file
http://www.codeproject.com
Export Data to CSV ( Excel )
http://www.dotnetspider.co
Cheers!
Q:How can i use StreamWriter to create the file automatically and save to a location (server.mappath(./abc.xls)
A:StreamWriter sw;
sw = File.CreateText(filePath);
sw.Write(Contents);
sw.Close();
return true;
From what I understand about your first question it is that you are already creating the Excel file no problem but you want to change some of the values before creating the file?
If this is the case then you can iterate through the datagrid before creating the excel file and change the values, then create the file as required.
Basically something along the lines of
foreach( Row r in DataGrid.Rows )
{
switch r.cells[0].Text
{
case "1":
r.cells[0].Text = "xxxx";
break;
}
}
hth
Thanks for the streamwriter reply. I managed to get the answer from another post :
public void WriteFile(string data)
{
string FILENAME = Server.MapPath("abc.xls");
System.IO.StreamWriter objStreamWriter = new System.IO.StreamWriter(FIL
objStreamWriter.WriteLine(
objStreamWriter.Close();
}
I guess the iteration process is right, but im getting to frame it properly. Logically it should be like this :
for each (condition)
if condition for cell1 and display cell2=""
if condition for cell2 and display cell2=""
display cell3
Okay so you are saying that if Cell 1's value is X and Cell 2's value is Y then only display Cell 3?
If that is the case it would be much easier to just hit the DB again and return the actual grid that you want to display in Excel.
Or you are going to be spending a lot of time manipulating the HTML to wrte properly to Excel.
Okay then the I would go for the following
foreach (DataRow dr in DataGrid.Rows)
{
switch (dr.ItemArray[0].ToString(
{
case "Hi":
dr.ItemArray[0] = "Bye";
break;
case "Good":
dr.ItemArray[0] = "no-bye";
break;
}
switch (dr.ItemArray[1].ToString(
{
case "test":
dr.ItemArray[0] = "Bye";
break;
}
}
It's not pretty but you can split it into some smaller methods for ease of use.
Easy enough....
In my example I have a datagrid (DataGrid1) which has the values of either 'True' or 'False' in the 4th cell.
On button click I do the following
int i = 0;
while( i < DataGrid1.Items.Count )
{
switch (DataGrid1.Items[i].Cells[
{
case "False":
DataGrid1.Items[i].Cells[3
break;
case "True":
DataGrid1.Items[i].Cells[3
break;
}
i++;
}
Export(); //Call function to export
private void Export()
{
Response.Clear();
Response.Buffer= true;
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWrit
DataGrid1.RenderControl(oH
Response.Write(oStringWrit
Response.End();
}
......
I have tested it and it works perfectly...
Business Accounts
Answer for Membership
by: inexplicablePosted on 2006-01-17 at 00:38:15ID: 15717829
There's also another part to the question :
) ..?
How can i use StreamWriter to create the file automatically and save to a location (server.mappath(./abc.xls)