The following code is not mine and long since forgot where I found it. The
object TheGrid is a populated DataGridView with the data you want to place
into the Excel file. There are countless ways to approach the file i/o which
is up to you. If you need to get fancy surf to MSDN and search for Excel XML
where the tags are documented (no real samples).
Public Sub ExportGridToExcel(ByVal FileName As String)
Try
Dim fs As New IO.StreamWriter(FileName, 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 TheGrid.Columns.Count - 1
fs.WriteLine(" <ss:Column ss:Width=""{0}""/>",
TheGrid.Columns.Item(x).Width)
Next
fs.WriteLine(" <ss:Row ss:StyleID=""1"">")
For i As Integer = 0 To TheGrid.Columns.Count - 1
fs.WriteLine(" <ss:Cell>")
fs.WriteLine(String.Format(" <ss:Data
ss:Type=""String"">{0}</ss:Data>", TheGrid.Columns.Item(i).HeaderText))
fs.WriteLine(" </ss:Cell>")
Next
fs.WriteLine(" </ss:Row>")
For intRow As Integer = 0 To TheGrid.RowCount - 2
fs.WriteLine(String.Format(" <ss:Row ss:Height
=""{0}"">", TheGrid.Rows(intRow).Height))
For intCol As Integer = 0 To TheGrid.Columns.Count - 1
fs.WriteLine(" <ss:Cell>")
fs.WriteLine(String.Format(" <ss:Data
ss:Type=""String"">{0}</ss:Data>", TheGrid.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()
Catch ex As Exception
' For demoing I am simply telling you we had a problem, in a real
application
' we at least remove the MsgBox and uncomment the next line. What
should also
' be considered is not using --> Throw new exception... because
now the exception
' points to the line with that statement rather then the original
problem.
'
'
MsgBox(ex.Message)
'Throw
End Try
OpenExcelFile(FileName)
Application.DoEvents()
End Sub
''' <summary>
''' Opens a file with the Association under Shell in the system registry
''' </summary>
''' <param name="FileName">Opens file by shell association</param>
''' <remarks></remarks>
Private Sub OpenExcelFile(ByVal FileName As String)
If System.IO.File.Exists(FileName) Then
Process.Start(FileName)
End If
End Sub
[quoted text, click to view] <zacks@construction-imaging.com> wrote in message
news:365befe8-bdc3-4a01-8e1c-283ee71ee3d6@e25g2000prg.googlegroups.com...
>I have an application that can read an Excel spreadsheet on a computer
> that doesn't have Excel installed on using the Jet ODBC 4.0 driver.
>
> I know need to modify the application to also be able to create a new
> Excel spreadsheet and write data to it on the same computer, a
> computer that does not have Excel, or even Office installed.
>
> I have done some research on this and it appears it cannot be done.
>
> Can anyone verify that or better yet, point me to a link that explains
> how to do it?