Page 1 of 2 12 LastLast
Results 1 to 40 of 71

Thread: export DGV to word ~ excel

  1. #1

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    export DGV to word ~ excel

    vb2008 export DGV to word ~ excel

    (see also: export listview to word table: http://www.vbforums.com/showthread.php?t=624967)
    Attached Files Attached Files

  2. #2
    Addicted Member
    Join Date
    Dec 2010
    Posts
    213

    Re: export DGV to word ~ excel

    Hi Paul, how can we append to an already existing excel file?

    I have this code so far:

    Code:
    If Not File.Exists(Application.StartupPath & "\" & "test" & ".xlsx") Then
    
    <insert .Paul.'s original DVG to excel code here>
    
    'I also set visibility = false because I want my program to auto-save without user interaction
    
    'plus I added
    
    xlWorkBook.SaveAs(Application.StartupPath & "\" & "test" & ".xlsx") 
               
     'releasing object references
                xlWorkBook = Nothing
                xlWorkBook = Nothing
                xlApp.Quit()
                xlApp = Nothing
    
    DataGridView1.Rows.Clear() 'clear our datagridview table 
    
    else
    
    <insert .Paul.'s original DVG to excel code with these following changes:>
    
    Dim xlApp as excel.application = new excel.Application
    
    Dim xlWorkBook As excel.Workbook = xlApp.Workbooks.Open(Application.StartupPath & "\" & "test" & ".xlsx")
    
    Dim xlWorkSheet As excel.Worksheet   'how do I select the current sheet on this file?
    
    'And this line I modified the range to:
    Dim range As excel.Range = xlWorkSheet.UsedRange
    
    'And then I have this at the end to save my excel file
    
    xlWorkBook.Save() 
    
    'releasing object references
                xlWorkBook = Nothing
                xlWorkBook = Nothing
                xlApp.Quit()
                xlApp = Nothing
    
    DataGridView1.Rows.Clear() 'clear our datagridview table when user clicks save
    Am I missing anything or do I need to do something else? Would this work?
    Last edited by zero_coke; Mar 9th, 2011 at 06:18 PM.

  3. #3

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: export DGV to word ~ excel

    what do you want to append to your existing worksheet?

  4. #4
    Addicted Member
    Join Date
    Dec 2010
    Posts
    213

    Re: export DGV to word ~ excel

    Whenever my users click "save" I clear the datagridview contents. Then:

    Whenever the user's click "Save" again, program should check if there is an existing excel file named "test" and append to the end of it the current DGV contents. Then the DGV is cleared again and basically this is repeated everytime the user clicks "Save".

    However, if no such file named "test" exists then it should just make a new excel file and paste the DGV contents into it.

    That's all. So if certain file exists then append to it (excluding the column header names) and if doesn't exist then paste all of the DGV contents to a new file.

    Does that make any sense? Do you have any other questions? I hope this made it clear...
    Last edited by zero_coke; Mar 9th, 2011 at 07:46 PM.

  5. #5

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: export DGV to word ~ excel

    well you'd use similar code to my example, but you'd open the existing workbook, set xlWorkSheet to "sheet1" (same worksheet as originally used), copy your dgv + set the range allowing for the previously used cells, then paste the new information from your dgv.

  6. #6
    Addicted Member
    Join Date
    Dec 2010
    Posts
    213

    Re: export DGV to word ~ excel

    Yeah I think the code I pasted above does that when I made the appropriate changes but it doesn't work for some reason....I don't know what I'm missing...

  7. #7

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: export DGV to word ~ excel

    ok post your complete code + i'll tell you where you're going wrong

  8. #8
    Addicted Member
    Join Date
    Dec 2010
    Posts
    213

    Re: export DGV to word ~ excel

    Code:
    Private Sub exportToExcel(ByVal dgv As DataGridView)
            If Not File.Exists(Application.StartupPath & "\" & "TEST" & ".xlsx") Then
                Dim xlApp As excel.Application = New excel.Application
                Dim xlWorkBook As excel.Workbook
                Dim xlWorkSheet As excel.Worksheet
                Dim misValue As Object = System.Reflection.Missing.Value
    
                xlWorkBook = xlApp.Workbooks.Add(misValue)
                xlWorkSheet = DirectCast(xlWorkBook.Sheets("sheet1"), excel.Worksheet)
    
                'xlApp.Visible = True
    
                Dim headers = (From ch In dgv.Columns _
                                Let header = DirectCast(DirectCast(ch, DataGridViewColumn).HeaderCell, DataGridViewColumnHeaderCell) _
                                Select header.Value).ToArray()
                Dim headerText() As String = Array.ConvertAll(headers, Function(v) v.ToString)
    
                Dim items() = (From r In dgv.Rows _
                        Let row = DirectCast(r, DataGridViewRow) _
                        Where Not row.IsNewRow _
                        Select (From cell In row.Cells _
                            Let c = DirectCast(cell, DataGridViewCell) _
                            Select c.Value).ToArray()).ToArray()
    
                Dim table As String = String.Join(vbTab, headerText) & Environment.NewLine
                For Each a In items
                    Dim t() As String = Array.ConvertAll(a, Function(v) v.ToString)
                    table &= String.Join(vbTab, t) & Environment.NewLine
                Next
                table = table.TrimEnd(CChar(Environment.NewLine))
                Clipboard.SetText(table)
    
                Dim alphabet() As Char = "abcdefghijklmnopqrstuvwxyz".ToUpper.ToCharArray
    
                Dim range As excel.Range = xlWorkSheet.Range("B2:" & alphabet(headerText.Length) & (items.Length + 2).ToString)
    
                range.Select()
                xlWorkSheet.Paste()
    
                range.Borders(excel.XlBordersIndex.xlDiagonalDown).LineStyle = excel.XlLineStyle.xlLineStyleNone
                range.Borders(excel.XlBordersIndex.xlDiagonalUp).LineStyle = excel.XlLineStyle.xlLineStyleNone
                With range.Borders(excel.XlBordersIndex.xlEdgeLeft)
                    .LineStyle = excel.XlLineStyle.xlContinuous
                    .ColorIndex = 1 'black
                    .TintAndShade = 0
                    .Weight = excel.XlBorderWeight.xlMedium
                End With
                With range.Borders(excel.XlBordersIndex.xlEdgeTop)
                    .LineStyle = excel.XlLineStyle.xlContinuous
                    .ColorIndex = 1 'black
                    .TintAndShade = 0
                    .Weight = excel.XlBorderWeight.xlMedium
                End With
                With range.Borders(excel.XlBordersIndex.xlEdgeBottom)
                    .LineStyle = excel.XlLineStyle.xlContinuous
                    .ColorIndex = 1 'black
                    .TintAndShade = 0
                    .Weight = excel.XlBorderWeight.xlMedium
                End With
                With range.Borders(excel.XlBordersIndex.xlEdgeRight)
                    .LineStyle = excel.XlLineStyle.xlContinuous
                    .ColorIndex = 1 'black
                    .TintAndShade = 0
                    .Weight = excel.XlBorderWeight.xlMedium
                End With
                With range.Borders(excel.XlBordersIndex.xlInsideVertical)
                    .LineStyle = excel.XlLineStyle.xlContinuous
                    .ColorIndex = 1 'black
                    .TintAndShade = 0
                    .Weight = excel.XlBorderWeight.xlThin
                End With
                With range.Borders(excel.XlBordersIndex.xlInsideHorizontal)
                    .LineStyle = excel.XlLineStyle.xlContinuous
                    .ColorIndex = 1 'black
                    .TintAndShade = 0
                    .Weight = excel.XlBorderWeight.xlThin
                End With
    
                xlWorkBook.SaveAs(Application.StartupPath & "\" & "TEST" & ".xlsx") 'save our workbook
    
                'releasing object references
                xlWorkBook = Nothing
                xlWorkBook = Nothing
                xlApp.Quit()
                xlApp = Nothing
    
                TestDataGridView.Rows.Clear() 'clear the log
    
            ElseIf File.Exists(Application.StartupPath & "\" & "TEST" & ".xlsx") Then
                MsgBox("bump, it exists")
                Dim xlApp As excel.Application = New excel.Application
                Dim xlWorkBook As excel.Workbook = xlApp.Workbooks.Open(Application.StartupPath & "\" & "TEST" & ".xlsx")
                Dim xlWorkSheet As excel.Worksheet
                Dim misValue As Object = System.Reflection.Missing.Value
    
                xlWorkBook = xlApp.Workbooks.Add(misValue)
                xlWorkSheet = DirectCast(xlWorkBook.Sheets("sheet1"), excel.Worksheet)
    
                'xlApp.Visible = True
    
                Dim headers = (From ch In dgv.Columns _
                                Let header = DirectCast(DirectCast(ch, DataGridViewColumn).HeaderCell, DataGridViewColumnHeaderCell) _
                                Select header.Value).ToArray()
                Dim headerText() As String = Array.ConvertAll(headers, Function(v) v.ToString)
    
                Dim items() = (From r In dgv.Rows _
                        Let row = DirectCast(r, DataGridViewRow) _
                        Where Not row.IsNewRow _
                        Select (From cell In row.Cells _
                            Let c = DirectCast(cell, DataGridViewCell) _
                            Select c.Value).ToArray()).ToArray()
    
                Dim table As String = String.Join(vbTab, headerText) & Environment.NewLine
                For Each a In items
                    Dim t() As String = Array.ConvertAll(a, Function(v) v.ToString)
                    table &= String.Join(vbTab, t) & Environment.NewLine
                Next
                table = table.TrimEnd(CChar(Environment.NewLine))
                Clipboard.SetText(table)
    
                Dim alphabet() As Char = "abcdefghijklmnopqrstuvwxyz".ToUpper.ToCharArray
    
                Dim range As excel.Range = xlWorkSheet.UsedRange
    
                range.Select() '? should have this or not? is it over-writing?
                xlWorkSheet.Paste()
    
                range.Borders(excel.XlBordersIndex.xlDiagonalDown).LineStyle = excel.XlLineStyle.xlLineStyleNone
                range.Borders(excel.XlBordersIndex.xlDiagonalUp).LineStyle = excel.XlLineStyle.xlLineStyleNone
                With range.Borders(excel.XlBordersIndex.xlEdgeLeft)
                    .LineStyle = excel.XlLineStyle.xlContinuous
                    .ColorIndex = 1 'black
                    .TintAndShade = 0
                    .Weight = excel.XlBorderWeight.xlMedium
                End With
                With range.Borders(excel.XlBordersIndex.xlEdgeTop)
                    .LineStyle = excel.XlLineStyle.xlContinuous
                    .ColorIndex = 1 'black
                    .TintAndShade = 0
                    .Weight = excel.XlBorderWeight.xlMedium
                End With
                With range.Borders(excel.XlBordersIndex.xlEdgeBottom)
                    .LineStyle = excel.XlLineStyle.xlContinuous
                    .ColorIndex = 1 'black
                    .TintAndShade = 0
                    .Weight = excel.XlBorderWeight.xlMedium
                End With
                With range.Borders(excel.XlBordersIndex.xlEdgeRight)
                    .LineStyle = excel.XlLineStyle.xlContinuous
                    .ColorIndex = 1 'black
                    .TintAndShade = 0
                    .Weight = excel.XlBorderWeight.xlMedium
                End With
                With range.Borders(excel.XlBordersIndex.xlInsideVertical)
                    .LineStyle = excel.XlLineStyle.xlContinuous
                    .ColorIndex = 1 'black
                    .TintAndShade = 0
                    .Weight = excel.XlBorderWeight.xlThin
                End With
                With range.Borders(excel.XlBordersIndex.xlInsideHorizontal)
                    .LineStyle = excel.XlLineStyle.xlContinuous
                    .ColorIndex = 1 'black
                    .TintAndShade = 0
                    .Weight = excel.XlBorderWeight.xlThin
                End With
    
                xlWorkBook.Save()
    
                'releasing object references
                xlWorkBook = Nothing
                xlWorkBook = Nothing
                xlApp.Quit()
                xlApp = Nothing
    
                TestDataGridView.Rows.Clear() 'clear the log
            Else
                Debug.Print("What the hell? Either exists or not lol")
            End If
        End Sub
    Can you also show me how to not write the headers when appending?
    Last edited by zero_coke; Mar 9th, 2011 at 08:05 PM.

  9. #9

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: export DGV to word ~ excel

    try this:

    vb Code:
    1. Private Sub exportToExcel(ByVal dgv As DataGridView)
    2.         If Not File.Exists(Application.StartupPath & "\" & "TEST" & ".xlsx") Then
    3.             Dim xlApp As excel.Application = New excel.Application
    4.             Dim xlWorkBook As excel.Workbook
    5.             Dim xlWorkSheet As excel.Worksheet
    6.             Dim misValue As Object = System.Reflection.Missing.Value
    7.  
    8.             xlWorkBook = xlApp.Workbooks.Add(misValue)
    9.             xlWorkSheet = DirectCast(xlWorkBook.Sheets("sheet1"), excel.Worksheet)
    10.  
    11.             'xlApp.Visible = True
    12.  
    13.             Dim headers = (From ch In dgv.Columns _
    14.                             Let header = DirectCast(DirectCast(ch, DataGridViewColumn).HeaderCell, DataGridViewColumnHeaderCell) _
    15.                             Select header.Value).ToArray()
    16.             Dim headerText() As String = Array.ConvertAll(headers, Function(v) v.ToString)
    17.  
    18.             Dim items() = (From r In dgv.Rows _
    19.                     Let row = DirectCast(r, DataGridViewRow) _
    20.                     Where Not row.IsNewRow _
    21.                     Select (From cell In row.Cells _
    22.                         Let c = DirectCast(cell, DataGridViewCell) _
    23.                         Select c.Value).ToArray()).ToArray()
    24.  
    25.             Dim table As String = String.Join(vbTab, headerText) & Environment.NewLine
    26.             For Each a In items
    27.                 Dim t() As String = Array.ConvertAll(a, Function(v) v.ToString)
    28.                 table &= String.Join(vbTab, t) & Environment.NewLine
    29.             Next
    30.             table = table.TrimEnd(CChar(Environment.NewLine))
    31.             Clipboard.SetText(table)
    32.  
    33.             Dim alphabet() As Char = "abcdefghijklmnopqrstuvwxyz".ToUpper.ToCharArray
    34.  
    35.             Dim range As excel.Range = xlWorkSheet.Range("B2:" & alphabet(headerText.Length) & (items.Length + 2).ToString)
    36.  
    37.             range.Select()
    38.             xlWorkSheet.Paste()
    39.  
    40.             range.Borders(excel.XlBordersIndex.xlDiagonalDown).LineStyle = excel.XlLineStyle.xlLineStyleNone
    41.             range.Borders(excel.XlBordersIndex.xlDiagonalUp).LineStyle = excel.XlLineStyle.xlLineStyleNone
    42.             With range.Borders(excel.XlBordersIndex.xlEdgeLeft)
    43.                 .LineStyle = excel.XlLineStyle.xlContinuous
    44.                 .ColorIndex = 1 'black
    45.                 .TintAndShade = 0
    46.                 .Weight = excel.XlBorderWeight.xlMedium
    47.             End With
    48.             With range.Borders(excel.XlBordersIndex.xlEdgeTop)
    49.                 .LineStyle = excel.XlLineStyle.xlContinuous
    50.                 .ColorIndex = 1 'black
    51.                 .TintAndShade = 0
    52.                 .Weight = excel.XlBorderWeight.xlMedium
    53.             End With
    54.             With range.Borders(excel.XlBordersIndex.xlEdgeBottom)
    55.                 .LineStyle = excel.XlLineStyle.xlContinuous
    56.                 .ColorIndex = 1 'black
    57.                 .TintAndShade = 0
    58.                 .Weight = excel.XlBorderWeight.xlMedium
    59.             End With
    60.             With range.Borders(excel.XlBordersIndex.xlEdgeRight)
    61.                 .LineStyle = excel.XlLineStyle.xlContinuous
    62.                 .ColorIndex = 1 'black
    63.                 .TintAndShade = 0
    64.                 .Weight = excel.XlBorderWeight.xlMedium
    65.             End With
    66.             With range.Borders(excel.XlBordersIndex.xlInsideVertical)
    67.                 .LineStyle = excel.XlLineStyle.xlContinuous
    68.                 .ColorIndex = 1 'black
    69.                 .TintAndShade = 0
    70.                 .Weight = excel.XlBorderWeight.xlThin
    71.             End With
    72.             With range.Borders(excel.XlBordersIndex.xlInsideHorizontal)
    73.                 .LineStyle = excel.XlLineStyle.xlContinuous
    74.                 .ColorIndex = 1 'black
    75.                 .TintAndShade = 0
    76.                 .Weight = excel.XlBorderWeight.xlThin
    77.             End With
    78.  
    79.             xlWorkBook.SaveAs(Application.StartupPath & "\" & "TEST" & ".xlsx") 'save our workbook
    80.  
    81.             'releasing object references
    82.             xlWorkBook = Nothing
    83.             xlWorkBook = Nothing
    84.             xlApp.Quit()
    85.             xlApp = Nothing
    86.  
    87.             TestDataGridView.Rows.Clear() 'clear the email log
    88.  
    89.         ElseIf File.Exists(Application.StartupPath & "\" & "TEST" & ".xlsx") Then
    90.             MsgBox("bump, it exists")
    91.             Dim xlApp As excel.Application = New excel.Application
    92.             Dim xlWorkBook As excel.Workbook = xlApp.Workbooks.Open(Application.StartupPath & "\" & "TEST" & ".xlsx")
    93.             Dim xlWorkSheet As excel.Worksheet
    94.             Dim misValue As Object = System.Reflection.Missing.Value
    95.  
    96.             xlWorkBook = xlApp.Workbooks.Add(misValue)
    97.             xlWorkSheet = DirectCast(xlWorkBook.Sheets("sheet1"), excel.Worksheet)
    98.  
    99.             'xlApp.Visible = True
    100.  
    101.             Dim headers = (From ch In dgv.Columns _
    102.                            Let header = DirectCast(DirectCast(ch, DataGridViewColumn).HeaderCell, DataGridViewColumnHeaderCell) _
    103.                            Select header.Value).ToArray()
    104.             Dim headerText() As String = Array.ConvertAll(headers, Function(v) v.ToString)
    105.  
    106.             Dim items() = (From r In dgv.Rows _
    107.                     Let row = DirectCast(r, DataGridViewRow) _
    108.                     Where Not row.IsNewRow _
    109.                     Select (From cell In row.Cells _
    110.                         Let c = DirectCast(cell, DataGridViewCell) _
    111.                         Select c.Value).ToArray()).ToArray()
    112.  
    113.             Dim table As String
    114.             For Each a In items
    115.                 Dim t() As String = Array.ConvertAll(a, Function(v) v.ToString)
    116.                 table &= String.Join(vbTab, t) & Environment.NewLine
    117.             Next
    118.             table = table.TrimEnd(CChar(Environment.NewLine))
    119.             Clipboard.SetText(table)
    120.  
    121.             Dim alphabet() As Char = "abcdefghijklmnopqrstuvwxyz".ToUpper.ToCharArray
    122.  
    123.             Dim range As excel.Range = xlWorkSheet.Range("B" & (xlWorkSheet.UsedRange.rows.count + 1).tostring & ":" & alphabet(headerText.Length) & (xlWorkSheet.UsedRange.rows.count + items.Length).ToString)
    124.  
    125.             range.Select() '? should have this or not? is it over-writing?
    126.             xlWorkSheet.Paste()
    127.  
    128.             range.Borders(excel.XlBordersIndex.xlDiagonalDown).LineStyle = excel.XlLineStyle.xlLineStyleNone
    129.             range.Borders(excel.XlBordersIndex.xlDiagonalUp).LineStyle = excel.XlLineStyle.xlLineStyleNone
    130.             With range.Borders(excel.XlBordersIndex.xlEdgeLeft)
    131.                 .LineStyle = excel.XlLineStyle.xlContinuous
    132.                 .ColorIndex = 1 'black
    133.                 .TintAndShade = 0
    134.                 .Weight = excel.XlBorderWeight.xlMedium
    135.             End With
    136.             With range.Borders(excel.XlBordersIndex.xlEdgeTop)
    137.                 .LineStyle = excel.XlLineStyle.xlContinuous
    138.                 .ColorIndex = 1 'black
    139.                 .TintAndShade = 0
    140.                 .Weight = excel.XlBorderWeight.xlMedium
    141.             End With
    142.             With range.Borders(excel.XlBordersIndex.xlEdgeBottom)
    143.                 .LineStyle = excel.XlLineStyle.xlContinuous
    144.                 .ColorIndex = 1 'black
    145.                 .TintAndShade = 0
    146.                 .Weight = excel.XlBorderWeight.xlMedium
    147.             End With
    148.             With range.Borders(excel.XlBordersIndex.xlEdgeRight)
    149.                 .LineStyle = excel.XlLineStyle.xlContinuous
    150.                 .ColorIndex = 1 'black
    151.                 .TintAndShade = 0
    152.                 .Weight = excel.XlBorderWeight.xlMedium
    153.             End With
    154.             With range.Borders(excel.XlBordersIndex.xlInsideVertical)
    155.                 .LineStyle = excel.XlLineStyle.xlContinuous
    156.                 .ColorIndex = 1 'black
    157.                 .TintAndShade = 0
    158.                 .Weight = excel.XlBorderWeight.xlThin
    159.             End With
    160.             With range.Borders(excel.XlBordersIndex.xlInsideHorizontal)
    161.                 .LineStyle = excel.XlLineStyle.xlContinuous
    162.                 .ColorIndex = 1 'black
    163.                 .TintAndShade = 0
    164.                 .Weight = excel.XlBorderWeight.xlThin
    165.             End With
    166.  
    167.             xlWorkBook.Save()
    168.  
    169.             'releasing object references
    170.             xlWorkBook = Nothing
    171.             xlWorkBook = Nothing
    172.             xlApp.Quit()
    173.             xlApp = Nothing
    174.  
    175.             TestDataGridView.Rows.Clear() 'clear the email log
    176.         Else
    177.             Debug.Print("What the hell? Either exists or not lol")
    178.         End If
    179.     End Sub

  10. #10
    Addicted Member
    Join Date
    Dec 2010
    Posts
    213

    Re: export DGV to word ~ excel

    I'll try this and let you know.

    In the mean time, do you know how to make the columns in excel file auto-size so I don't have to manually shift the cells width since they're too small and text overwrites onto next cell if you know what I mean.

  11. #11

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: export DGV to word ~ excel

    add this after the other range formatting code:

    vb Code:
    1. range.Columns.AutoFit()

  12. #12
    Addicted Member
    Join Date
    Dec 2010
    Posts
    213

    Re: export DGV to word ~ excel

    Hey Paul, can you please upload the class file? I just copied and pasted your code and went through removing all the line numbers manually and still I'm getting a whole bunch of errors (the squiggly blue line under variables).

    Also, can you please make that autofit adjustment? I'm not too sure where you mean by "other range" code.

  13. #13

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: export DGV to word ~ excel

    i didn't make a class file. those squiggly lines probably mean you don't have the right imports in your class. which version of windows are you using? i can copy that code without the line numbers...
    Attached Files Attached Files

  14. #14
    Addicted Member
    Join Date
    Dec 2010
    Posts
    213

    Re: export DGV to word ~ excel

    Alright, that fixed it. First I removed the line numbers manually and then I made a program to remove the line numbers using regex I guess that messed it up.

    So where exactly do I put the range.Columns.AutoFit() ?

    EDIT: Nevermind, just saw it. I was checking the top the whole time meanwhile it was at the bottom.

    Thanks Paul! I'll try this out, hopefully works without flaws. I'll keep you updated.

  15. #15
    Addicted Member
    Join Date
    Dec 2010
    Posts
    213

    Re: export DGV to word ~ excel

    Uh oh...it is not appending...What do I do now Paul?

  16. #16

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: export DGV to word ~ excel

    ok. can you upload your project + i'll debug it.

  17. #17
    Addicted Member
    Join Date
    Dec 2010
    Posts
    213

    Re: export DGV to word ~ excel

    I'm sorry Paul, I can't upload the project. It's just some agreement that I have with my client and partners. However, I greatly appreciate your help. Would you like me to make you a test project and see if it appends?

  18. #18

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: export DGV to word ~ excel

    actually i think i found it. in the append part remove this line:

    vb Code:
    1. xlWorkBook = xlApp.Workbooks.Add(misValue)

  19. #19
    Addicted Member
    Join Date
    Dec 2010
    Posts
    213

    Re: export DGV to word ~ excel

    When I do that it I can't open the excel file at all. Just opens and closes really fast.

  20. #20

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: export DGV to word ~ excel

    ok. post the code again

  21. #21
    Addicted Member
    Join Date
    Dec 2010
    Posts
    213

    Re: export DGV to word ~ excel

    vb Code:
    1. Private Sub exportToExcel_new(ByVal dgv As DataGridView)
    2.         If Not File.Exists(Application.StartupPath & "\" & "TEST" & ".xlsx") Then
    3.             Dim xlApp As excel.Application = New excel.Application
    4.             Dim xlWorkBook As excel.Workbook
    5.             Dim xlWorkSheet As excel.Worksheet
    6.             Dim misValue As Object = System.Reflection.Missing.Value
    7.  
    8.             xlWorkBook = xlApp.Workbooks.Add(misValue)
    9.             xlWorkSheet = DirectCast(xlWorkBook.Sheets("sheet1"), excel.Worksheet)
    10.  
    11.             Dim headers = (From ch In dgv.Columns _
    12.                             Let header = DirectCast(DirectCast(ch, DataGridViewColumn).HeaderCell, DataGridViewColumnHeaderCell) _
    13.                             Select header.Value).ToArray()
    14.             Dim headerText() As String = Array.ConvertAll(headers, Function(v) v.ToString)
    15.  
    16.             Dim items() = (From r In dgv.Rows _
    17.                     Let row = DirectCast(r, DataGridViewRow) _
    18.                     Where Not row.IsNewRow _
    19.                     Select (From cell In row.Cells _
    20.                         Let c = DirectCast(cell, DataGridViewCell) _
    21.                         Select c.Value).ToArray()).ToArray()
    22.  
    23.             Dim table As String = String.Join(vbTab, headerText) & Environment.NewLine
    24.  
    25.             For Each a In items
    26.                 Dim t() As String = Array.ConvertAll(a, Function(v) v.ToString)
    27.                 table &= String.Join(vbTab, t) & Environment.NewLine
    28.             Next
    29.  
    30.             table = table.TrimEnd(CChar(Environment.NewLine))
    31.             Clipboard.SetText(table)
    32.  
    33.             Dim alphabet() As Char = "abcdefghijklmnopqrstuvwxyz".ToUpper.ToCharArray
    34.  
    35.             Dim range As excel.Range = xlWorkSheet.Range("B2:" & alphabet(headerText.Length) & (items.Length + 2).ToString)
    36.  
    37.             range.Select()
    38.             xlWorkSheet.Paste()
    39.  
    40.             range.Borders(excel.XlBordersIndex.xlDiagonalDown).LineStyle = excel.XlLineStyle.xlLineStyleNone
    41.             range.Borders(excel.XlBordersIndex.xlDiagonalUp).LineStyle = excel.XlLineStyle.xlLineStyleNone
    42.             With range.Borders(excel.XlBordersIndex.xlEdgeLeft)
    43.                 .LineStyle = excel.XlLineStyle.xlContinuous
    44.                 .ColorIndex = 1 'black
    45.                 .TintAndShade = 0
    46.                 .Weight = excel.XlBorderWeight.xlMedium
    47.             End With
    48.             With range.Borders(excel.XlBordersIndex.xlEdgeTop)
    49.                 .LineStyle = excel.XlLineStyle.xlContinuous
    50.                 .ColorIndex = 1 'black
    51.                 .TintAndShade = 0
    52.                 .Weight = excel.XlBorderWeight.xlMedium
    53.             End With
    54.             With range.Borders(excel.XlBordersIndex.xlEdgeBottom)
    55.                 .LineStyle = excel.XlLineStyle.xlContinuous
    56.                 .ColorIndex = 1 'black
    57.                 .TintAndShade = 0
    58.                 .Weight = excel.XlBorderWeight.xlMedium
    59.             End With
    60.             With range.Borders(excel.XlBordersIndex.xlEdgeRight)
    61.                 .LineStyle = excel.XlLineStyle.xlContinuous
    62.                 .ColorIndex = 1 'black
    63.                 .TintAndShade = 0
    64.                 .Weight = excel.XlBorderWeight.xlMedium
    65.             End With
    66.             With range.Borders(excel.XlBordersIndex.xlInsideVertical)
    67.                 .LineStyle = excel.XlLineStyle.xlContinuous
    68.                 .ColorIndex = 1 'black
    69.                 .TintAndShade = 0
    70.                 .Weight = excel.XlBorderWeight.xlThin
    71.             End With
    72.             With range.Borders(excel.XlBordersIndex.xlInsideHorizontal)
    73.                 .LineStyle = excel.XlLineStyle.xlContinuous
    74.                 .ColorIndex = 1 'black
    75.                 .TintAndShade = 0
    76.                 .Weight = excel.XlBorderWeight.xlThin
    77.             End With
    78.  
    79.             range.Columns.AutoFit() 'auto-size the columns so the columns look nice
    80.             xlWorkBook.SaveAs(Application.StartupPath & "\" & "TEST" & ".xlsx") 'save our workbook
    81.  
    82.             'releasing object references
    83.             xlWorkBook = Nothing
    84.             xlWorkBook = Nothing
    85.             xlApp.Quit()
    86.             xlApp = Nothing
    87.  
    88.             DataGridView1.Rows.Clear() 'clear the log
    89.  
    90.         ElseIf File.Exists(Application.StartupPath & "\" & "TEST" & ".xlsx") Then
    91.             MsgBox("bump, it exists")
    92.             Dim xlApp As excel.Application = New excel.Application
    93.             Dim xlWorkBook As excel.Workbook = xlApp.Workbooks.Open(Application.StartupPath & "\" & "TEST" & ".xlsx")
    94.             Dim xlWorkSheet As excel.Worksheet
    95.             'Dim misValue As Object = System.Reflection.Missing.Value
    96.  
    97.             'xlWorkBook = xlApp.Workbooks.Add(misValue)
    98.             xlWorkSheet = DirectCast(xlWorkBook.Sheets("sheet1"), excel.Worksheet)
    99.  
    100.             Dim headers = (From ch In dgv.Columns _
    101.                            Let header = DirectCast(DirectCast(ch, DataGridViewColumn).HeaderCell, DataGridViewColumnHeaderCell) _
    102.                            Select header.Value).ToArray()
    103.             Dim headerText() As String = Array.ConvertAll(headers, Function(v) v.ToString)
    104.  
    105.             Dim items() = (From r In dgv.Rows _
    106.                     Let row = DirectCast(r, DataGridViewRow) _
    107.                     Where Not row.IsNewRow _
    108.                     Select (From cell In row.Cells _
    109.                         Let c = DirectCast(cell, DataGridViewCell) _
    110.                         Select c.Value).ToArray()).ToArray()
    111.  
    112.             Dim table As String = String.Empty
    113.  
    114.             For Each a In items
    115.                 Dim t() As String = Array.ConvertAll(a, Function(v) v.ToString)
    116.                 table &= String.Join(vbTab, t) & Environment.NewLine
    117.             Next
    118.  
    119.             table = table.TrimEnd(CChar(Environment.NewLine))
    120.             Clipboard.SetText(table)
    121.  
    122.             Dim alphabet() As Char = "abcdefghijklmnopqrstuvwxyz".ToUpper.ToCharArray
    123.             Dim range As excel.Range = xlWorkSheet.Range("B" & (xlWorkSheet.UsedRange.Rows.Count + 1).ToString & ":" & alphabet(headerText.Length) & (xlWorkSheet.UsedRange.Rows.Count + items.Length).ToString)
    124.  
    125.             range.Select() '? should have this or not? is it over-writing?
    126.             xlWorkSheet.Paste()
    127.  
    128.             range.Borders(excel.XlBordersIndex.xlDiagonalDown).LineStyle = excel.XlLineStyle.xlLineStyleNone
    129.             range.Borders(excel.XlBordersIndex.xlDiagonalUp).LineStyle = excel.XlLineStyle.xlLineStyleNone
    130.  
    131.             With range.Borders(excel.XlBordersIndex.xlEdgeLeft)
    132.                 .LineStyle = excel.XlLineStyle.xlContinuous
    133.                 .ColorIndex = 1 'black
    134.                 .TintAndShade = 0
    135.                 .Weight = excel.XlBorderWeight.xlMedium
    136.             End With
    137.             With range.Borders(excel.XlBordersIndex.xlEdgeTop)
    138.                 .LineStyle = excel.XlLineStyle.xlContinuous
    139.                 .ColorIndex = 1 'black
    140.                 .TintAndShade = 0
    141.                 .Weight = excel.XlBorderWeight.xlMedium
    142.             End With
    143.             With range.Borders(excel.XlBordersIndex.xlEdgeBottom)
    144.                 .LineStyle = excel.XlLineStyle.xlContinuous
    145.                 .ColorIndex = 1 'black
    146.                 .TintAndShade = 0
    147.                 .Weight = excel.XlBorderWeight.xlMedium
    148.             End With
    149.             With range.Borders(excel.XlBordersIndex.xlEdgeRight)
    150.                 .LineStyle = excel.XlLineStyle.xlContinuous
    151.                 .ColorIndex = 1 'black
    152.                 .TintAndShade = 0
    153.                 .Weight = excel.XlBorderWeight.xlMedium
    154.             End With
    155.             With range.Borders(excel.XlBordersIndex.xlInsideVertical)
    156.                 .LineStyle = excel.XlLineStyle.xlContinuous
    157.                 .ColorIndex = 1 'black
    158.                 .TintAndShade = 0
    159.                 .Weight = excel.XlBorderWeight.xlThin
    160.             End With
    161.             With range.Borders(excel.XlBordersIndex.xlInsideHorizontal)
    162.                 .LineStyle = excel.XlLineStyle.xlContinuous
    163.                 .ColorIndex = 1 'black
    164.                 .TintAndShade = 0
    165.                 .Weight = excel.XlBorderWeight.xlThin
    166.             End With
    167.  
    168.             range.Columns.AutoFit() 'auto-size the columns so the excel columns look nice
    169.             xlWorkBook.Save() 'save our workbook
    170.  
    171.             'releasing object references
    172.             xlWorkBook = Nothing
    173.             xlWorkBook = Nothing
    174.             xlApp.Quit()
    175.             xlApp = Nothing
    176.  
    177.             DataGridView1.Rows.Clear() 'clear the log
    178.         Else
    179.             Debug.Print("What the hell? Either exists or not lol")
    180.         End If
    181.     End Sub

  22. #22

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: export DGV to word ~ excel

    it does work. you don't see it because you removed:

    vb Code:
    1. xlApp.Visible = True

    + you close excel after the code has run, which is almost instantly

  23. #23
    Addicted Member
    Join Date
    Dec 2010
    Posts
    213

    Re: export DGV to word ~ excel

    Yeah but it should at least be saved right? I mean, i have

    vb Code:
    1. xlWorkBook.Save() 'save our workbook

    At the end so would it matter if the visibility was on or off? And I don't want the visibility to be on, I just want the program to save it automatically without showing the user. Do I have to turn visibility on?

  24. #24

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: export DGV to word ~ excel

    have you tried opening the file in excel? it works + saves whether you make excel visible or not

  25. #25
    Addicted Member
    Join Date
    Dec 2010
    Posts
    213

    Re: export DGV to word ~ excel

    Here's my tester project. It doesn't save for some reason. Can you have a look at it please Paul?

    Thanks

    It's 4 am in the morning here. I'll be back tomorrow Paul. Thanks!
    Attached Files Attached Files
    Last edited by zero_coke; Mar 10th, 2011 at 02:05 AM.

  26. #26

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: export DGV to word ~ excel

    you didn't remove these lines from the append part as i told you to:

    vb Code:
    1. Dim misValue As Object = System.Reflection.Missing.Value
    2.  
    3. xlWorkBook = xlApp.Workbooks.Add(misValue)

    if you remove these lines it works

  27. #27

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: export DGV to word ~ excel

    also, before the 2nd range.columns.autofit, put this:

    vb Code:
    1. range = xlWorkSheet.Range("B2:" & alphabet(xlWorkSheet.UsedRange.Columns.Count) & (xlWorkSheet.UsedRange.Rows.Count).ToString)

  28. #28
    Addicted Member
    Join Date
    Dec 2010
    Posts
    213

    Re: export DGV to word ~ excel

    Yay! Thanks Paul it works now! It appends exactly the way I wanted to!

    So Paul, final question if you do not mind:

    How can you get the application to overwrite the existing file automatically? When I click save I see this:



    When you click "no" my debugger points out an error on some line because I think the user must click Yes or the append part of the code results in an error.

    I don't even know why Excel is prompting to replace the file when you're just opening it, writing some data do it and saving it.

  29. #29

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: export DGV to word ~ excel

    i'm not sure about that. that didn't happen when i ran it.
    start a new thread for that...

  30. #30
    Addicted Member
    Join Date
    Dec 2010
    Posts
    213

    Re: export DGV to word ~ excel

    Hey Paul,

    Excel doesn't prompt me anymore. I made a stupid mistake and added

    vb Code:
    1. xlapp.SaveWorkspace()

    So I removed that ^ and it works now.

    Just one question: When a cell is empty, the code gives me an error here:

    ERROR: Object reference not set to an instance of an object.

    Error location:

    For Each a In items
    Dim t() As String = Array.ConvertAll(a, Function(v) v.ToString)
    table &= String.Join(vbTab, t) & Environment.NewLine
    Next

    And it highlights the Function(v) v.ToString) part.

    You can check if a cell is empty by:

    Code:
    String.IsNullOrEmpty(CStr(DataGridView1.Item(i, x).Value))
    But I don't know where to put it. Seems like you're using an array function...

  31. #31

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: export DGV to word ~ excel

    this works:

    Dim t() As String = Array.ConvertAll(a, Function(v) if(not v is nothing, v.ToString, ""))

  32. #32
    Addicted Member
    Join Date
    Dec 2010
    Posts
    213

    Re: export DGV to word ~ excel

    Awesome, that works

    I want to ask one more question. May I? I really don't want to bother you anymore. You've done so much.

  33. #33

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: export DGV to word ~ excel

    what's the question?

  34. #34
    Addicted Member
    Join Date
    Dec 2010
    Posts
    213

    Re: export DGV to word ~ excel

    The excel application doesn't close. When I go to task manager, its still there so how can I close it properly?

    I could use

    Code:
    Dim proc As System.Diagnostics.Process
    
    For Each proc In System.Diagnostics.Process.GetProcessesByName("EXCEL")
    proc.Kill()
    Next
    But I don't think this is the right way to end it right?
    Last edited by zero_coke; Mar 10th, 2011 at 12:26 PM.

  35. #35

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: export DGV to word ~ excel

    try:
    xlworkbook.close
    xlapp.close (or dispose)

  36. #36
    New Member
    Join Date
    May 2011
    Posts
    6

    Question Re: export DGV to word ~ excel

    Hi Paul,

    I'm having trouble making the gridview go to a specific bookmark, it's probably my ignorance. How would I go about doing this?

    Thanks,

    Verra

  37. #37

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: export DGV to word ~ excel

    my example uses a new word doc with no bookmarks set. i'm assuming you're using an existing word doc with predefined bookmarks?

  38. #38
    New Member
    Join Date
    May 2011
    Posts
    6

    Re: export DGV to word ~ excel

    Yes, I've got a few data grid views which will display in a report in a word document; all the bookmarks are pre-defined.

  39. #39

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: export DGV to word ~ excel

    could you post the word doc you're using + clarify which dgv should go where?

  40. #40
    New Member
    Join Date
    May 2011
    Posts
    6

    Re: export DGV to word ~ excel

    The word document I'm using is HUGE, approx 200 pages. Here's an example of part of it:

    "Blah blah blah assets etc (talks about assets)

    [Bookmark here for Assets Summary]

    more text here about other things

    -a few pages later-

    text about liabilities

    [Bookmark for Liabilities Summary]

    then more text"

    The bookmarked grid views don't get anymore complex than that, just sticking them after or before paragraphs and headings.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width