Results 1 to 6 of 6

Thread: First 255 chars from array element

  1. #1

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    First 255 chars from array element

    Guys,

    I am using the code below to get all rows from a datatable into a multidim array, I am then writing each array element to a new row in an Excel workbook.

    I am getting a problem if any of the elements have more than 255 chars, I believe this is the issue anyway. It starts copying fine, but then when I get to what could be a large text field, I get an error.... "Exception from HRESULT: 0x800A03EC"

    How can I only copy the first 255 chars from an array element ? I have marked below where I believe I need to make the change, or would it be when populating the array initially ?

    Thanks in advace,
    Bob


    VB Code:
    1. Public Shared Sub Export(ByVal Filename As String, ByVal dt As DataTable, ByVal MRSMaster As String)
    2.  
    3.             Dim RowNbr As Int32 = 0
    4.             Dim AllRecords(dt.Rows.Count) As Object
    5.  
    6.             Try
    7.  
    8.                 'get MatRec into array
    9.                 For Each Rw As DataRow In dt.Rows
    10.                     AllRecords(RowNbr) = Rw.ItemArray()
    11.                     RowNbr += 1
    12.                 Next Rw
    13.  
    14.                 Dim oExcel As New Excel.Application
    15.                 Dim oBooks As Excel.Workbooks, oBook As Excel.Workbook
    16.                 Dim oSheets As Excel.Sheets, oSheet As Excel.Worksheet
    17.                 Dim oCells As Excel.Range
    18.                 'oExcel.Visible = False : oExcel.DisplayAlerts = False
    19.  
    20.                 'Open MRS template
    21.                 oBook = oExcel.Workbooks.Open(MRSMaster)
    22.  
    23.                 oSheets = oBook.Worksheets
    24.                 oSheet = CType(oSheets.Item(1), Excel.Worksheet)
    25.                 oExcel.AddIns("Analysis ToolPak").Installed = True
    26.  
    27.                 'Do all the data export here.
    28.                 Dim x As Integer
    29.                 Dim y As Integer
    30.                 Dim range As String
    31.                 For x = 0 To AllRecords.GetUpperBound(0)
    32.  
    33. '************** HERE ***************
    34.  
    35.                     range = "A" & x + 2 & ":Q" & x + 2
    36.                     oSheet.Range(range).Value = AllRecords(x)
    37.                 Next
    38.  
    39.                 Select Case Right(MRSMaster, 13)
    40.                     Case "antMaster.xls"
    41.                         oSheet.Columns(17).delete()
    42.                         oSheet.Columns(13).delete()
    43.                         oSheet.Columns(2).delete()
    44.                         oSheet.Columns(1).delete()
    45.                     Case "MRSMaster.xls"
    46.                         oSheet.Columns(14).delete()
    47.                         oSheet.Columns(5).delete()
    48.                         oSheet.Columns(2).delete()
    49.                         oSheet.Columns(1).delete()
    50.                 End Select
    51.  
    52.  
    53.                 Dim MyDateTime As Date = Today
    54.                 Dim MyStr As String
    55.                 MyStr = Format(MyDateTime, "dd-mm-yy")
    56.                 oSheet.Name = "PWBExport " & MyStr
    57.  
    58.  
    59.                 oBook.SaveAs(Filename)
    60.                 oBook.Close()
    61.                 oExcel.DisplayAlerts = True
    62.  
    63.                 'Quit Excel and thoroughly deallocate everything
    64.                 oExcel.Quit()
    65.                 oExcel = Nothing : oBooks = Nothing : oBook = Nothing
    66.                 oSheets = Nothing : oSheet = Nothing
    67.                 AllRecords = Nothing
    68.  
    69.             Catch ex As Exception
    70.                 EH.Log(ex)
    71.             End Try
    72.         End Sub
    "I dislike 7 am. If 7 am were a person, I would punch 7 am in the biscuits." - Paul Ryan, DailyRamblings

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: First 255 chars from array element

    I wouldn't bother loading another object, and just iterate through your datatable:
    VB Code:
    1. Public Shared Sub Export(ByVal Filename As String, ByVal dt As DataTable, ByVal MRSMaster As String)
    2.  
    3.         Dim RowNbr As Int32 = 0
    4.  
    5.         Try
    6.  
    7.             Dim oExcel As New Excel.Application
    8.             Dim oBooks As Excel.Workbooks, oBook As Excel.Workbook
    9.             Dim oSheets As Excel.Sheets, oSheet As Excel.Worksheet
    10.             Dim oCells As Excel.Range
    11.             'oExcel.Visible = False : oExcel.DisplayAlerts = False
    12.  
    13.             'Open MRS template
    14.             oBook = oExcel.Workbooks.Open(MRSMaster)
    15.  
    16.             oSheets = oBook.Worksheets
    17.             oSheet = CType(oSheets.Item(1), Excel.Worksheet)
    18.             oExcel.AddIns("Analysis ToolPak").Installed = True
    19.  
    20.             'Ascii for A
    21.             Const AsciiHolder As Int32 = 65
    22.  
    23.             For iRow As Int32 = 0 To dt.Rows.Count - 1
    24.                 For iCol As Int32 = 0 To dt.Columns.Count - 1
    25.                     Dim ColumnID As String
    26.                     If iCol < 26 Then
    27.                         ColumnID = Convert.ToChar(AsciiHolder + iCol)
    28.                     Else
    29.                         ColumnID = Convert.ToChar(AsciiHolder + CInt(iCol / 26) - 1) & Convert.ToChar(AsciiHolder + (iCol Mod 26) - 1)
    30.                     End If
    31.                     If dt.Rows(iRow).Item(iCol).ToString.Length > 255 Then
    32.                         oSheet.Range(ColumnID & iRow + 2).Value = dt.Rows(iRow).Item(iCol).ToString.Substring(0, 255)
    33.                     Else
    34.                         oSheet.Range(ColumnID & iRow + 2).Value = dt.Rows(iRow).Item(iCol).ToString
    35.                     End If
    36.                 Next
    37.             Next
    38.             '************** HERE ***************
    39.  
    40.             Select Case MRSMaster.Substring(0, 13)
    41.                 Case "antMaster.xls"
    42.                     oSheet.Columns(17).delete()
    43.                     oSheet.Columns(13).delete()
    44.                     oSheet.Columns(2).delete()
    45.                     oSheet.Columns(1).delete()
    46.                 Case "MRSMaster.xls"
    47.                     oSheet.Columns(14).delete()
    48.                     oSheet.Columns(5).delete()
    49.                     oSheet.Columns(2).delete()
    50.                     oSheet.Columns(1).delete()
    51.             End Select
    52.  
    53.  
    54.             Dim MyDateTime As Date = Date.Today
    55.             Dim MyStr As String
    56.             MyStr = MyDateTime.ToString("dd-mm-yy")
    57.             oSheet.Name = "PWBExport " & MyStr
    58.  
    59.  
    60.             oBook.SaveAs(Filename)
    61.             oBook.Close()
    62.             oExcel.DisplayAlerts = True
    63.  
    64.             'Quit Excel and thoroughly deallocate everything
    65.             oExcel.Quit()
    66.             oExcel = Nothing : oBooks = Nothing : oBook = Nothing
    67.             oSheets = Nothing : oSheet = Nothing
    68.  
    69.  
    70.         Catch ex As Exception
    71.  
    72.         End Try
    Last edited by wild_bill; Jan 24th, 2006 at 06:28 PM.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: First 255 chars from array element

    If you want up to the first 255 characters of a string you could use:
    VB Code:
    1. myString.Substring(0, Math.Min(myString.Length, 255))
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: First 255 chars from array element

    Nice tip on the Math.Min, I don't know why I never thought of using that before.

  5. #5

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Re: First 255 chars from array element

    Thanks guys,

    Bill - your code works very well, thanks.

    Is it possible to also to copy over the column/field headers into the first row ? I was using this code for a couple of datatables and I was opening predefined templates with columns headers already defined. I am now using this with lots of datatables and don't want to be opening a different template for each when I'm sure its got to be pretty simple to also dump the field names ?

    Thanks
    Bob
    "I dislike 7 am. If 7 am were a person, I would punch 7 am in the biscuits." - Paul Ryan, DailyRamblings

  6. #6
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: First 255 chars from array element

    VB Code:
    1. If iRow = 0 Then
    2.            oSheet.Range(ColumnID & iRow + 1).Value = dt.Columns(iCol).ColumnName()
    3.         End If
    4. 'Before this line
    5. If dt.Rows(iRow).Item(iCol).ToString.Length > 255 Then

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