Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format Excel Column in vb.net

Status
Not open for further replies.

ArizonaRedneck

Programmer
Oct 25, 2004
61
US
I need to transfer a dataset into excel for work. I can do that, but I don't know how to format the column through code. for example...

in excel, you would highlight the column,click format,cells, and under category I would choose text.

I need to do this because some of the fields in my dataset are numbers stored as text (zip code and others). When excel opens the dataset, it converts these columns to numbers and any leading zeros are cut off.

here is the sub I'm using.

Code:
Friend Sub ExportToExcel(ByVal dstDataSet As DataSet, ByVal TableName As String)
        Dim excelApp As New Excel.Application()
        Dim excelBook As Excel.Workbook = excelApp.Workbooks.Add
        Dim excelWorksheet As Excel.Worksheet = CType(excelBook.Worksheets(1), Excel.Worksheet)
        excelApp.Visible = True
        Dim x As Integer = 1
        Dim dcColumn As DataColumn
        For Each dcColumn In dstDataSet.Tables(TableName).Columns
            excelApp.ActiveSheet.Cells(1, x) = dcColumn.ColumnName
            x += 1
        Next
        Dim drDataRow As DataRow
        Dim ColumnCount, RowCount As Integer
        For Each dcColumn In dstDataSet.Tables(TableName).Columns
            ColumnCount += 1
            RowCount = 1
            For Each drDataRow In dstDataSet.Tables(TableName).Rows
                RowCount += 1
                Try
                    excelApp.ActiveSheet.Cells(RowCount, ColumnCount) = CType(drDataRow(dcColumn), String)
                Catch
                    excelApp.ActiveSheet.Cells(RowCount, ColumnCount) = drDataRow(dcColumn)
                End Try
            Next
        Next
        excelApp.Columns.AutoFit()
    End Sub
 
I got it to work. Just in case anyone else out there would find this useful, here's the updated sub.

Code:
 Friend Sub ExportToExcel(ByVal dstDataSet As DataSet, ByVal TableName As String)
        Dim excelApp As New Excel.Application()
        Dim excelBook As Excel.Workbook = excelApp.Workbooks.Add
        Dim excelWorksheet As Excel.Worksheet = CType(excelBook.Worksheets(1), Excel.Worksheet)
        excelApp.Visible = True
        [b]excelApp.Columns.Cells.NumberFormat = "@"[/b]
        Dim x As Integer = 1
        Dim dcColumn As DataColumn
        For Each dcColumn In dstDataSet.Tables(TableName).Columns
            excelApp.ActiveSheet.Cells(1, x) = dcColumn.ColumnName
            x += 1
        Next
        Dim drDataRow As DataRow
        Dim ColumnCount, RowCount As Integer
        For Each dcColumn In dstDataSet.Tables(TableName).Columns
            ColumnCount += 1
            RowCount = 1
            For Each drDataRow In dstDataSet.Tables(TableName).Rows
                RowCount += 1
                excelApp.ActiveSheet.Cells(RowCount, ColumnCount) = drDataRow(dcColumn)
            Next
        Next
        excelApp.Columns.AutoFit()
    End Sub

-
"Do your duty in all things. You cannot do more, you should never wish to do less." Robert E. Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top