Click here to Skip to main content
16,004,882 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: "Live" variable names Pin
Christian Graus28-Nov-06 11:48
protectorChristian Graus28-Nov-06 11:48 
Questionreading data from ms Access database Pin
collo28-Nov-06 0:18
collo28-Nov-06 0:18 
AnswerRe: reading data from ms Access database Pin
Paul Conrad28-Nov-06 12:23
professionalPaul Conrad28-Nov-06 12:23 
Questionmake file excel(*.xls) from vb .NET 2003 Pin
aphei27-Nov-06 22:57
aphei27-Nov-06 22:57 
AnswerRe: make file excel(*.xls) from vb .NET 2003 Pin
nguyenvhn27-Nov-06 23:29
nguyenvhn27-Nov-06 23:29 
GeneralRe: make file excel(*.xls) from vb .NET 2003 Pin
Tamimi - Code27-Nov-06 23:40
Tamimi - Code27-Nov-06 23:40 
GeneralRe: make file excel(*.xls) from vb .NET 2003 Pin
Dave Kreskowiak28-Nov-06 4:29
mveDave Kreskowiak28-Nov-06 4:29 
AnswerRe: make file excel(*.xls) from vb .NET 2003 [modified] Pin
Kevin Nicol28-Nov-06 1:42
Kevin Nicol28-Nov-06 1:42 
Here is a simple app that takes a dataset and writes a worksheet per data table in the set
Public Sub writeToExcel(ByVal source As DataSet)
Dim doc As New System.IO.StreamWriter("Excel.xls") 'use .xls even though its an xml file

Dim startExcelXML As String = ""

startExcelXML &= "<xml version>"
startExcelXML &= vbNewLine & "<Workbook " + "xmlns=""urn:schemas-microsoft-com:office:spreadsheet"""
startExcelXML &= vbNewLine & Microsoft.VisualBasic.Chr(10) & "" + " xmlnsBlush | :O =""urn:schemas-microsoft-com:office:office"""
startExcelXML &= vbNewLine & "xmlns:x=""urn:schemas- microsoft-com:office:" + "excel"""
startExcelXML &= vbNewLine & "xmlns:ss=""urn:schemas-microsoft-com:" + "office:spreadsheet"">"


'write the styles tags that format the data and cells properly
startExcelXML &= vbNewLine & "<Styles>"

startExcelXML &= vbNewLine & " <Style ss:ID=""Default"" ss:Name=""Normal"">"
startExcelXML &= vbNewLine & " <Alignment ss:Vertical=""Bottom""/>"
startExcelXML &= vbNewLine & " <Borders/>"
startExcelXML &= vbNewLine & " <Font/>"
startExcelXML &= vbNewLine & " <Interior/>"
startExcelXML &= vbNewLine & " <NumberFormat/>"
startExcelXML &= vbNewLine & " <Protection/>"
startExcelXML &= vbNewLine & " </Style>"

startExcelXML &= vbNewLine & "<Style ss:ID=""ColHeader"">"
startExcelXML &= vbNewLine & "<Alignment ss:Horizontal=""Center"" ss:Vertical=""Bottom""/>"
startExcelXML &= vbNewLine & "<Font x:Family=""Swiss"" ss:Size=""8"" ss:Bold=""1""/>"
startExcelXML &= vbNewLine & "<Interior ss:Color=""#C0C0C0"" ss:Pattern=""Solid""/>"
startExcelXML &= vbNewLine & "</Style>"

startExcelXML &= vbNewLine & " <Style ss:ID=""Reg"">"
startExcelXML &= vbNewLine & " <Font " + "x:Family=""Swiss"" ss:Bold""0""/>"
startExcelXML &= vbNewLine & " </Style>"

startExcelXML &= vbNewLine & "</Styles>"

'write the header to the file
doc.WriteLine(startExcelXML)


'write one sheet per table
For Each tab As DataTable In source.Tables
Dim sheetname As String = "<Worksheet ss:Name="
sheetname &= Microsoft.VisualBasic.Chr(34)
sheetname &= tab.TableName.Trim
sheetname &= Microsoft.VisualBasic.Chr(34)
sheetname &= ">"
doc.WriteLine(sheetname)

doc.WriteLine("<Table>")

'write the column headers
doc.WriteLine("<Row>")
For Each col As DataColumn In tab.Columns
doc.Write("<Cell ss:StyleID=""ColHeader""><Data ss:Type=""String"">")
doc.Write(col.ColumnName)
doc.WriteLine("</Data></Cell>")
Next
doc.WriteLine("</Row>")


'write the table
For Each row As DataRow In tab.Rows
doc.WriteLine("<Row>")

'Dim i As Integer
For i As Integer = 0 To tab.Columns.Count - 1
doc.Write("<Cell ss:StyleID=""Reg""><Data ss:Type=""String"">")
doc.Write(row.Item(i))
doc.WriteLine("</Data></Cell>")
Next

doc.WriteLine("</Row>")
Next


doc.WriteLine("</Table>")
doc.WriteLine("</Worksheet>")
Next

doc.WriteLine("</Workbook>")
doc.Close()

End Sub


this can be opened in excel. You will have to add your own formating to make the sheet look like you want, to do that just make changes, save it and open it and see what it did to the tags. Then make your code do the same.

Hope this helps

Kevin



-- modified at 9:55 Tuesday 28th November, 2006
Questionreading a block of data from a text file Pin
charchabil0327-Nov-06 22:49
charchabil0327-Nov-06 22:49 
AnswerRe: reading a block of data from a text file Pin
Christian Graus27-Nov-06 23:49
protectorChristian Graus27-Nov-06 23:49 
GeneralRe: reading a block of data from a text file Pin
charchabil0328-Nov-06 1:16
charchabil0328-Nov-06 1:16 
GeneralRe: reading a block of data from a text file Pin
Christian Graus28-Nov-06 8:13
protectorChristian Graus28-Nov-06 8:13 
GeneralRe: reading a block of data from a text file Pin
charchabil0328-Nov-06 9:33
charchabil0328-Nov-06 9:33 
GeneralRe: reading a block of data from a text file Pin
Christian Graus28-Nov-06 11:47
protectorChristian Graus28-Nov-06 11:47 
QuestionWeb Control Pin
riyaz_786_p27-Nov-06 21:05
riyaz_786_p27-Nov-06 21:05 
QuestionStopping a service Pin
nitin_ion27-Nov-06 20:50
nitin_ion27-Nov-06 20:50 
AnswerRe: Stopping a service Pin
Dave Kreskowiak28-Nov-06 4:35
mveDave Kreskowiak28-Nov-06 4:35 
GeneralRe: Stopping a service Pin
nitin_ion28-Nov-06 16:34
nitin_ion28-Nov-06 16:34 
GeneralRe: Stopping a service Pin
Dave Kreskowiak29-Nov-06 2:44
mveDave Kreskowiak29-Nov-06 2:44 
Questionvb wininet Problem Pin
psiva198427-Nov-06 20:34
psiva198427-Nov-06 20:34 
Question[Message Deleted] Pin
PREMSONBABY27-Nov-06 20:01
PREMSONBABY27-Nov-06 20:01 
GeneralRe: Regarding data binding Pin
jsampsonPC28-Nov-06 4:08
jsampsonPC28-Nov-06 4:08 
General[Message Deleted] Pin
PREMSONBABY28-Nov-06 5:06
PREMSONBABY28-Nov-06 5:06 
AnswerRe: Regarding data binding Pin
jsampsonPC28-Nov-06 5:08
jsampsonPC28-Nov-06 5:08 
Questionhow to insert data into datagrid dynamically usin insert command Pin
tirumal123127-Nov-06 18:12
tirumal123127-Nov-06 18:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.