473,395 Members | 2,192 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,395 software developers and data experts.

Exporting to Excel and Column Order

Opa
Hi,

I have a DataGrid, whose sourceI am exporting to Excel. This works fine
except for the Column ordering. My datasource is not a datatable, with a
typical
SELECT statement where I can select the column orders. Instead the datasource
is a class which implements IList, containing a collection of my data. My
problem
again is that I don't know how to control the order of the columns that are
exported.

Any Ideas?

Thanks
Sep 11 '06 #1
6 2517
Opa,

This isn't a solution but may lead you in the correct direction. You will
have to find a way to sort the columns. You may need to make a customized
sort. Which can be added on to your class. But here's a simple default
method:

public IList Sort(IList list)
{
((ArrayList)list).Sort();
return list;
}

Regards,

--
S. Justin Gengo
Web Developer / Programmer

Free code library:
http://www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Opa" <Op*@discussions.microsoft.comwrote in message
news:BA**********************************@microsof t.com...
Hi,

I have a DataGrid, whose sourceI am exporting to Excel. This works fine
except for the Column ordering. My datasource is not a datatable, with a
typical
SELECT statement where I can select the column orders. Instead the
datasource
is a class which implements IList, containing a collection of my data. My
problem
again is that I don't know how to control the order of the columns that
are
exported.

Any Ideas?

Thanks

Sep 11 '06 #2
Opa
Jusin, Thanks for the reply.
Perhaps you misunderstand the question.
I am not trying to sort the rows by a specific column,
but rather I am trying to manipulate the ordering or
or columns. Get it?

Any Ideas?
"S. Justin Gengo" wrote:
Opa,

This isn't a solution but may lead you in the correct direction. You will
have to find a way to sort the columns. You may need to make a customized
sort. Which can be added on to your class. But here's a simple default
method:

public IList Sort(IList list)
{
((ArrayList)list).Sort();
return list;
}

Regards,

--
S. Justin Gengo
Web Developer / Programmer

Free code library:
http://www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Opa" <Op*@discussions.microsoft.comwrote in message
news:BA**********************************@microsof t.com...
Hi,

I have a DataGrid, whose sourceI am exporting to Excel. This works fine
except for the Column ordering. My datasource is not a datatable, with a
typical
SELECT statement where I can select the column orders. Instead the
datasource
is a class which implements IList, containing a collection of my data. My
problem
again is that I don't know how to control the order of the columns that
are
exported.

Any Ideas?

Thanks


Sep 11 '06 #3

Opa wrote:
Jusin, Thanks for the reply.
Perhaps you misunderstand the question.
I am not trying to sort the rows by a specific column,
but rather I am trying to manipulate the ordering or
or columns. Get it?

Any Ideas?
"S. Justin Gengo" wrote:
Opa,

This isn't a solution but may lead you in the correct direction. You will
have to find a way to sort the columns. You may need to make a customized
sort. Which can be added on to your class. But here's a simple default
method:

public IList Sort(IList list)
{
((ArrayList)list).Sort();
return list;
}

Regards,

--
S. Justin Gengo
Web Developer / Programmer

Free code library:
http://www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Opa" <Op*@discussions.microsoft.comwrote in message
news:BA**********************************@microsof t.com...
Hi,
>
I have a DataGrid, whose sourceI am exporting to Excel. This works fine
except for the Column ordering. My datasource is not a datatable, with a
typical
SELECT statement where I can select the column orders. Instead the
datasource
is a class which implements IList, containing a collection of my data. My
problem
again is that I don't know how to control the order of the columns that
are
exported.
>
Any Ideas?
>
Thanks
I'm not sure i follow either, but...
I persume your DataGrid is set to autogenerate columns.
Dont.
Define bound (or whatever) columns, in the order that you want.

hope this helps
-- a --

Sep 11 '06 #4
Opa

I tried setting AutoGenerateColumns to false and then adding a single column,
but this does not work. Funny thing is that it seems to ignore the
AutoGenerateColumns=false, and yet it generate all the columns anyway.
Any Ideas?

Here is my code:

public static void Convert(IList list, HttpResponse response, string fileName)
{
//first let's clean up the response.object
response.Clear();
response.Charset = string.Empty;

//This will make the browser interpret the output as an Excel file
response.AddHeader("Content-Disposition", "attachment; filename=" +
fileName + ".xls");

//set the response mime type for excel
response.ContentType = "application/vnd.ms-excel";
//create a string writer
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

//instantiate a datagrid and set the datagrid datasource to the dataset
passed in
DataGrid dg = new DataGrid();
dg.DataSource = list;
dg.DataBind();

dg.AutoGenerateColumns = false;

//DataGridColumnCollection colCol = new DataGridColumnCollection(gd,
cols);
BoundColumn col = new BoundColumn();
col.DataField = "WorkOrderNumber";
col.HeaderText= "Work Order #";
col.ItemStyle.ForeColor = System.Drawing.Color.Blue;

dg.Columns.Add(col);

//tell the datagrid to render itself to our htmltextwriter
dg.RenderControl(htmlWrite);

response.Write("<B>" + fileName + "</B><BR/>");
response.Write("<B>" + DateTime.Now.ToString("F") + "</B><BR/><BR/>");

//all that's left is to output the html
response.Write(stringWrite.ToString());
response.End();
}


"addup" wrote:
>
Opa wrote:
Jusin, Thanks for the reply.
Perhaps you misunderstand the question.
I am not trying to sort the rows by a specific column,
but rather I am trying to manipulate the ordering or
or columns. Get it?

Any Ideas?
"S. Justin Gengo" wrote:
Opa,
>
This isn't a solution but may lead you in the correct direction. You will
have to find a way to sort the columns. You may need to make a customized
sort. Which can be added on to your class. But here's a simple default
method:
>
public IList Sort(IList list)
{
((ArrayList)list).Sort();
return list;
}
>
Regards,
>
--
S. Justin Gengo
Web Developer / Programmer
>
Free code library:
http://www.aboutfortunate.com
>
"Out of chaos comes order."
Nietzsche
>
>
"Opa" <Op*@discussions.microsoft.comwrote in message
news:BA**********************************@microsof t.com...
Hi,

I have a DataGrid, whose sourceI am exporting to Excel. This works fine
except for the Column ordering. My datasource is not a datatable, with a
typical
SELECT statement where I can select the column orders. Instead the
datasource
is a class which implements IList, containing a collection of my data. My
problem
again is that I don't know how to control the order of the columns that
are
exported.

Any Ideas?

Thanks

I'm not sure i follow either, but...
I persume your DataGrid is set to autogenerate columns.
Dont.
Define bound (or whatever) columns, in the order that you want.

hope this helps
-- a --

Sep 11 '06 #5

Opa wrote:
I tried setting AutoGenerateColumns to false and then adding a single column,
but this does not work. Funny thing is that it seems to ignore the
AutoGenerateColumns=false, and yet it generate all the columns anyway.
Any Ideas?

Here is my code:

public static void Convert(IList list, HttpResponse response, string fileName)
{
//first let's clean up the response.object
response.Clear();
response.Charset = string.Empty;

//This will make the browser interpret the output as an Excel file
response.AddHeader("Content-Disposition", "attachment; filename=" +
fileName + ".xls");

//set the response mime type for excel
response.ContentType = "application/vnd.ms-excel";
//create a string writer
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

//instantiate a datagrid and set the datagrid datasource to the dataset
passed in
DataGrid dg = new DataGrid();
dg.DataSource = list;
dg.DataBind();

dg.AutoGenerateColumns = false;

//DataGridColumnCollection colCol = new DataGridColumnCollection(gd,
cols);
BoundColumn col = new BoundColumn();
col.DataField = "WorkOrderNumber";
col.HeaderText= "Work Order #";
col.ItemStyle.ForeColor = System.Drawing.Color.Blue;

dg.Columns.Add(col);

//tell the datagrid to render itself to our htmltextwriter
dg.RenderControl(htmlWrite);

response.Write("<B>" + fileName + "</B><BR/>");
response.Write("<B>" + DateTime.Now.ToString("F") + "</B><BR/><BR/>");

//all that's left is to output the html
response.Write(stringWrite.ToString());
response.End();
}


"addup" wrote:

Opa wrote:
Jusin, Thanks for the reply.
Perhaps you misunderstand the question.
I am not trying to sort the rows by a specific column,
but rather I am trying to manipulate the ordering or
or columns. Get it?
>
Any Ideas?
>
>
"S. Justin Gengo" wrote:
>
Opa,

This isn't a solution but may lead you in the correct direction. You will
have to find a way to sort the columns. You may need to make a customized
sort. Which can be added on to your class. But here's a simple default
method:

public IList Sort(IList list)
{
((ArrayList)list).Sort();
return list;
}

Regards,

--
S. Justin Gengo
Web Developer / Programmer

Free code library:
http://www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche


"Opa" <Op*@discussions.microsoft.comwrote in message
news:BA**********************************@microsof t.com...
Hi,
>
I have a DataGrid, whose sourceI am exporting to Excel. This works fine
except for the Column ordering. My datasource is not a datatable, with a
typical
SELECT statement where I can select the column orders. Instead the
datasource
is a class which implements IList, containing a collection of my data. My
problem
again is that I don't know how to control the order of the columns that
are
exported.
>
Any Ideas?
>
Thanks
I'm not sure i follow either, but...
I persume your DataGrid is set to autogenerate columns.
Dont.
Define bound (or whatever) columns, in the order that you want.

hope this helps
-- a --

That's because you're doing your dg.DataBind first.
Do your databind *after* setting up the columns

i.e. just before the dg.RenderControl

-- a --

Sep 11 '06 #6
Opa
That's it! I did overlook this.
Thanks for your help man.

Case closed!
"addup" wrote:
>
Opa wrote:
I tried setting AutoGenerateColumns to false and then adding a single column,
but this does not work. Funny thing is that it seems to ignore the
AutoGenerateColumns=false, and yet it generate all the columns anyway.
Any Ideas?

Here is my code:

public static void Convert(IList list, HttpResponse response, string fileName)
{
//first let's clean up the response.object
response.Clear();
response.Charset = string.Empty;

//This will make the browser interpret the output as an Excel file
response.AddHeader("Content-Disposition", "attachment; filename=" +
fileName + ".xls");

//set the response mime type for excel
response.ContentType = "application/vnd.ms-excel";
//create a string writer
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

//instantiate a datagrid and set the datagrid datasource to the dataset
passed in
DataGrid dg = new DataGrid();
dg.DataSource = list;
dg.DataBind();

dg.AutoGenerateColumns = false;

//DataGridColumnCollection colCol = new DataGridColumnCollection(gd,
cols);
BoundColumn col = new BoundColumn();
col.DataField = "WorkOrderNumber";
col.HeaderText= "Work Order #";
col.ItemStyle.ForeColor = System.Drawing.Color.Blue;

dg.Columns.Add(col);

//tell the datagrid to render itself to our htmltextwriter
dg.RenderControl(htmlWrite);

response.Write("<B>" + fileName + "</B><BR/>");
response.Write("<B>" + DateTime.Now.ToString("F") + "</B><BR/><BR/>");

//all that's left is to output the html
response.Write(stringWrite.ToString());
response.End();
}


"addup" wrote:
>
Opa wrote:
Jusin, Thanks for the reply.
Perhaps you misunderstand the question.
I am not trying to sort the rows by a specific column,
but rather I am trying to manipulate the ordering or
or columns. Get it?

Any Ideas?


"S. Justin Gengo" wrote:

Opa,
>
This isn't a solution but may lead you in the correct direction. You will
have to find a way to sort the columns. You may need to make a customized
sort. Which can be added on to your class. But here's a simple default
method:
>
public IList Sort(IList list)
{
((ArrayList)list).Sort();
return list;
}
>
Regards,
>
--
S. Justin Gengo
Web Developer / Programmer
>
Free code library:
http://www.aboutfortunate.com
>
"Out of chaos comes order."
Nietzsche
>
>
"Opa" <Op*@discussions.microsoft.comwrote in message
news:BA**********************************@microsof t.com...
Hi,

I have a DataGrid, whose sourceI am exporting to Excel. This works fine
except for the Column ordering. My datasource is not a datatable, with a
typical
SELECT statement where I can select the column orders. Instead the
datasource
is a class which implements IList, containing a collection of my data. My
problem
again is that I don't know how to control the order of the columns that
are
exported.

Any Ideas?

Thanks
>
I'm not sure i follow either, but...
I persume your DataGrid is set to autogenerate columns.
Dont.
Define bound (or whatever) columns, in the order that you want.
>
hope this helps
-- a --


That's because you're doing your dg.DataBind first.
Do your databind *after* setting up the columns

i.e. just before the dg.RenderControl

-- a --

Sep 11 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ali Eghtebas | last post by:
Hi, I tried this demo application in here (watch for any line breaks in the URL): (http://support.crystaldecisions.com/communityCS/FilesAndUpdates/vbnet_win_a dodotnet.exe.asp When exporting...
2
by: pmud | last post by:
Hi, I am exporting data from an EDITABLE DATA GRID EXCEL. But the 1st column in data grid is Edit Column. I want to display all columns in Excel except for the Edit column. The following...
1
by: NancyA | last post by:
I am using the following code to write data from a datagrid to an Excel file: Dim tw As New System.IO.StringWriter Dim hw As New System.Web.UI.HtmlTextWriter(tw) dg.RenderControl(hw)...
7
by: Gal Zilberman | last post by:
Hi I've found a code which should work, but it doesn't. Please Help Imports Microsoft.Office.Interop ' Refence in the Class Dim oExcel As New Object Dim oBook As Object Dim oSheet As Object...
6
by: Kevin Humphreys | last post by:
Hi There, I am trying to export a recordset to an excel file using the Content Type below in the header. Response.ContentType = "application/vnd.ms-excel" Which works fine however the...
21
by: bobh | last post by:
Hi All, In Access97 I have a table that's greater than 65k records and I'm looking for a VBA way to export the records to Excel. Anyone have vba code to export from access to excel and have the...
4
by: Tom | last post by:
I have a gridview on all of my web pages in my web app and they all export to excel. I have one page where the gridview is binding to a datatable that i created and only the first column is...
16
by: robertmeyer1 | last post by:
Hey, I am working on creating a query which will export the information to excel. I have a specific excel document which has line by line items (corresponds to access query). Here's the...
7
by: leninv | last post by:
Hi, I have the following code where 'recs' is a record set. For i=0 to recs.Fields.Count - 1 if i = 0 then pindnt = string(itmlvl*2," ") response.write pindnt &...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.