Results 1 to 15 of 15

Thread: Exporting data to Excel

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Posts
    156

    Exclamation Exporting data to Excel

    I think I read about this before but I want to make sure:

    I am exporting data from a datagridview to an excel sheet, and I am coming across an apparent limitation as far as the maximum cell length for excel.

    I am pretty sure you can normally put more than 255 characters into a cell in excel, but when I try to do the same thing programmatically, I get an error message saying that I exceeded the max limit for number of characters for a cell.

    Is there any way around this?
    Thanx
    Iggy

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Exporting data to Excel

    Yes, it is a limitation when using ADO.NET and even the older ADO. The default data field type is set to Text (255) so if you have a Memo field in your database you will generate the error if the text is more then the 255. You can not change the data type of the Excel sheet but you can use other methods for getting the memo field into Excel like using the Excel Object Model.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Posts
    156

    Re: Exporting data to Excel

    would you have any examples of how to do this?
    Thank You

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Exporting data to Excel

    No, but I do have code on automating Excel from vb.net that should help get you started.

    http://www.vbforums.com/showpost.php...38&postcount=3
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Posts
    156

    Re: Exporting data to Excel

    I will be honest, I have not seen anything to could help me, but I could be missing the obvious.

    Here is my code that populates the excel sheet. could you please make suggestions?
    Thanx
    VB Code:
    1. MyConnection = New OleDb.OleDbConnection( _
    2.                             "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\ECN Database\Reports\" & strJob & ".xls" & _
    3.                             ";Extended Properties=""Excel 8.0;HDR=No;IMEX=0""")
    4.             MyConnection.Open()
    5.  
    6.             MyCommand = New OleDb.OleDbDataAdapter("SELECT * FROM [Sheet1$]", MyConnection)
    7.             Dim cmdBuilder As New OleDbCommandBuilder(MyCommand)
    8.             cmdBuilder.QuotePrefix = "["
    9.             cmdBuilder.QuoteSuffix = "]"
    10.  
    11.             DSExcel = New DataSet
    12.             MyCommand.FillSchema(DSExcel, SchemaType.Source, "excel")
    13.             MyCommand.Fill(DSExcel, "excel")
    14.  
    15.             'Debug.WriteLine(DSData.Tables(0).Columns.Count)
    16.             'Debug.WriteLine(DSData.Tables(0).Rows.Count)
    17.  
    18.             For i As Integer = 0 To DsecnEntriesV.Tables(0).Rows.Count - 1
    19.                 Dim dr As DataRow = DSExcel.Tables(0).NewRow
    20.                 For j As Integer = 0 To DsecnEntriesV.Tables(0).Columns.Count - 2
    21.                     dr(j) = DsecnEntriesV.Tables(0).Rows(i).Item(j)
    22.                 Next
    23.                 DSExcel.Tables(0).Rows.Add(dr)
    24.                 dr = Nothing
    25.             Next
    26.             MyCommand.Update(DSExcel, "excel")
    27.             MyConnection.Close()
    28.             DSExcel.Tables(0).Clear()

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Exporting data to Excel

    Looks fine. Using ADO.NET does leave you with no options for formatting the excel sheet but if exporting the data is all thats needed then you should be good to go.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Posts
    156

    Re: Exporting data to Excel

    but this is where I run into the maxlength issue. I have use this code many times before and it works fine...unless i run into 255+ characters.
    so I guess I am trying to see how to modify the code to make it work :-(

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Exporting data to Excel

    Oh thats right. Forgot what the thread issue was.

    I had a thread on this before but cant find it right now. I think I had solved it by using an ADO recordset but the problem was in VB 6 not .NET. So for this I know you can use the Excel Object Model with no issues but then using Excel seem to be something you dont want to do?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Posts
    156

    Re: Exporting data to Excel

    i'll admit that I do not know what exactly you are talking about :-)

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Exporting data to Excel

    By adding a reference to Excel you can use ifts functions and properties to make it do what ever you want it to do, etc.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Posts
    156

    Re: Exporting data to Excel

    I am not opposed to it at all. I actually do this later when I end up formating the workbook. I just never considered dumping the data that way, because it seemed tidious

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Exporting data to Excel

    Nothing more then looping like you were doing to populate your dataset.

    Did you also try using an OleDbCommand object and passing Parameters already?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Posts
    156

    Re: Exporting data to Excel

    :-) yeah I just realized that.
    I did it the way you suggested and it worked fine.

    what do you mean by oledbcommand object and passing parameters?
    Obviously I have not, but if you explain how, I could try.

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Exporting data to Excel

    I remembered this thread - http://vbforums.com/showthread.php?t=422955

    The sql insert into string may be too long as a result of the memo fields contents.


    But if your going to be formatting the worksheet using the EOM then there is not too many reasons to not use it to insert your data unless you have a large sheet where performance may be an issue.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Aug 2005
    Posts
    156

    Re: Exporting data to Excel

    many thanx

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