Hi
I'm new to vb, I'm using VB 2005
Ihave a DataGridView that i want to export to Excel
what is the simple way of doing that ?
|
| Elad_23 Wednesday, September 20, 2006 6:33 AM |
You can write following code on button click
Response.Clear(); Response.AddHeader("content-disposition", "attachment;filename=YourFileName.xls"); Response.ContentType = "application/vnd.xls";
System.IO.StringWriter sw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.RenderControl(hw); Response.Write(sw.ToString()); Response.End();
You will also need to override the VerifyRenderingInServerForm method.
public override void VerifyRenderingInServerForm(Control control) { return; }
- HTH
Regards Jignesh Desai |
| Jignesh Desai Wednesday, September 20, 2006 8:12 AM |
it's not a VB syntax am I right ?
and it's not recognize the Responce function at all |
| Elad_23 Wednesday, September 20, 2006 11:35 AM |
|
| Paul P Clement IV Wednesday, September 20, 2006 11:53 AM |
You can ignore my previous post since it looks like the same code that Jignesh posted. |
| Paul P Clement IV Wednesday, September 20, 2006 12:07 PM |
yes do you know why i keep getting Response is not declared ? |
| Elad_23 Wednesday, September 20, 2006 12:38 PM |
Are you creating a desktop application or a web app? Unfortunatelyalmost allcode examples are for a web app and use ASP objects, which wouldn't be available to a desktop application.
|
| Paul P Clement IV Wednesday, September 20, 2006 12:43 PM |
I'm creating a desktop application
what syntax do i need to use ? |
| Elad_23 Wednesday, September 20, 2006 1:08 PM |
There are two ways to do this, neither of which is particularly efficient. Will Excel be installed on the machine where you are performing the export?
|
| Paul P Clement IV Wednesday, September 20, 2006 4:17 PM |
|
| Elad_23 Wednesday, September 20, 2006 4:38 PM |
I usually export it as an xml spreadsheet which will work with office xp, and 2003. There is an example on the VB-tips website. |
| Ken Tucker Thursday, September 21, 2006 12:38 AM |
great it works but i have some questions :
1. how do i make the user choose the name of the file and the location ?
2. how do i open the file automaticly ?
3. in my DataGridView i have many numbers, How do i convert them all to be numbers and not text on the Exel file ?
Thanks.
|
| Elad_23 Thursday, September 21, 2006 5:41 AM |
i got it all
just need to find out how to get the excel file show numbers and not text
CSV can solve this ?
|
| Elad_23 Thursday, September 21, 2006 1:43 PM |
Check out the speadsheet xml's SS:Data tag you can specify the type of data. Represent a number: <ss:Cell>
<ss:Data ss:Type="Number">123</ss:Data>
</ss:Cell>
|
| Ken Tucker Thursday, September 21, 2006 2:06 PM |
Can you tell me what do i need to change in my code ?
Thanks
Dim fs As New IO.StreamWriter("C:\temp\" & returnValue & ".xls", False)
fs.WriteLine( "<?xml version=""1.0""?>")
fs.WriteLine( "<?mso-application progid=""Excel.Sheet""?>")
fs.WriteLine( "<ss:Workbook xmlns:ss=""urn:schemas-microsoft-com:office:spreadsheet"">")
fs.WriteLine( " <ss:Styles>")
fs.WriteLine( " <ss:Style ss:ID=""1"">")
fs.WriteLine( " <ss:Font ss:Bold=""1""/>")
fs.WriteLine( " </ss:Style>")
fs.WriteLine( " </ss:Styles>")
fs.WriteLine( " <ss:Worksheet ss:Name=""Sheet1"">")
fs.WriteLine( " <ss:Table>")
For x As Integer = 0 To DataGridView1.Columns.Count - 1
fs.WriteLine( " <ss:Column ss:Width=""{0}""/>", DataGridView1.Columns.Item(x).Width)
Next
fs.WriteLine( " <ss:Row ss:StyleID=""1"">")
For i As Integer = 0 To DataGridView1.Columns.Count - 1
fs.WriteLine( " <ss:Cell>")
fs.WriteLine( String.Format(" <ss:Data ss:Type=""String"">{0}</ss:Data>", DataGridView1.Columns.Item(i).HeaderText))
fs.WriteLine( " </ss:Cell>")
Next
fs.WriteLine( " </ss:Row>")
For intRow As Integer = 0 To DataGridView1.RowCount - 2
fs.WriteLine( String.Format(" <ss:Row ss:Height =""{0}"">", DataGridView1.Rows(intRow).Height))
For intCol As Integer = 0 To DataGridView1.Columns.Count - 1
fs.WriteLine( " <ss:Cell>")
fs.WriteLine( String.Format(" <ss:Data ss:Type=""String"">{0}</ss:Data>", DataGridView1.Item(intCol, intRow).Value.ToString))
fs.WriteLine( " </ss:Cell>")
Next
fs.WriteLine( " </ss:Row>")
Next
fs.WriteLine( " </ss:Table>")
fs.WriteLine( " </ss:Worksheet>")
fs.WriteLine( "</ss:Workbook>")
fs.Close()
|
| Elad_23 Thursday, September 21, 2006 7:30 PM |
You need to change
| |
fs.WriteLine(String.Format(" <ss:Data ss:Type=""String"">{0}</ss:Data>", DataGridView1.Columns.Item(i).HeaderText))
|
to
| |
fs.WriteLine(String.Format(" <ss:Data ss:Type=""Number"">{0}</ss:Data>", DataGridView1.Columns.Item(i).HeaderText))
|
for the number columns |
| Ken Tucker Thursday, September 21, 2006 9:02 PM |
it didn't work
i changed the intCOl
and it works
thanks |
| Elad_23 Saturday, September 23, 2006 3:55 PM |
How exactly did you change intCOL to make it work, thanks!
|
| Uniquekaiser Tuesday, June 19, 2007 10:09 PM |