CSV file and formatting

Posted on 2001-08-10
Last Modified: 2008-02-01
I'm trying to pull a large ammount of data from an Access 97 database and put it in an Excel spreadsheet programatically through ASP.  My problem became the limitaion of how many characters can be put into an excel cell.  I decided to put my data into a CSV file and then open that  CSV file in excel and format it in excel from within my script.  My problem is that the cell that I want to hold the large ammount of characters is not becomming one cell.  My understanding of CSV is that if you want to have comma separated strings in one cell, you put those values in quotes.  But this isn't working for me and I was wondering if it's my code or something else.  I'm including the script in it's entirety.  Please help.

<!---Begin Code--->
<%@Language = VBScript%>
<!-- #include file="stdinclude.asp"-->
<%      Dim xlApp
    Dim xlWrkBook
    Dim xlWrkSheet
    Dim cnBOM, rsBOM, strSQL, i, j, totalCols
    Dim myMail, email, fso, assembly
    Dim PartExists
    Dim partQ
     Dim PartArray()
    email = Request.Form("email")
    assembly = Request.Form("assembly")
    If assembly = "" Then
            Response.Write("You must enter an assembly to continue.")
      End If
      Set cnBOM = Server.CreateObject("ADODB.Connection")
      Set rsBOM = Server.CreateObject("ADODB.RecordSet")
      cnBOM.Open GetConnection
      rsBOM.CursorLocation = 3
      'Build the SQL string for creating the desired report !=
      strSQL = "select tblModules.SubmittedBy, tblModules.DateSubmitted, tblModules.BOMNumber, tblModules.Module, "
      strSQL = strSQL & "tblComponents.RefDes, tblComponents.CIENAPartNumber "
      strSQL = strSQL & "FROM tblModules INNER JOIN tblComponents ON tblComponents.BOMNumber = tblModules.BOMNumber "
      strSQL = strSQL & "WHERE tblComponents.MarkedForDeletion <> Yes AND tblModules.MarkedForDeletion <> Yes "
      strSQL = strSQL & "AND tblModules.Module = '"&assembly&"' order by tblComponents.CIENAPartNumber asc"
        rsBOM.Open strSQL, cnBOM
      'If the assembly doesn't exist in the database, error out and exit.
      If rsBOM.EOF Then
            Response.Write("The assembly <font color='red'><b>"&assembly&"</b></font> could not be found.")
            Set rsBOM = Nothing
            Set cnBOM = Nothing
      End If
  Dim count
  count = rsBOM.RecordCount
  Redim PartArray(count, 6)
      totalCols = 0
      i = 0
      PartExists = False
      Do While not rsBOM.EOF
            'Look for the current part in the array.
            Do While i <= totalCols
                  'If the part was found in the array...
                  If PartArray(i, 4) = rsBOM.Fields("CIENAPartNumber").Value Then
                        '..add the refdes to the current block of the array.
                        'Make sure you aren't adding an empty refdes.
                        If rsBOM.Fields("RefDes").Value <> "" AND rsBOM.Fields("RefDes").Value <> "REFDES MISSING" Then
                              If PartArray(i, 6) <> "" Then
                                    PartArray(i, 6) = PartArray(i, 6) &", "& rsBOM.Fields("RefDes").Value
                                    PartArray(i, 6) = rsBOM.Fields("RefDes").Value
                              End If
                        End If
                        'Increment the quantity of this part in the quantity block of the array.
                        PartArray(i, 5) = PartArray(i, 5) + 1
                        PartExists = True
                  End If
                  i = i + 1
            i = 0
            'If the part didn't already exist in the array, create a place for
            'it and put it in.
            If PartExists = False Then
                  totalCols = totalCols + 1
                  PartArray(totalCols, 1) = rsBOM.Fields("Module").Value
                  PartArray(totalCols, 2) = rsBOM.Fields("SubmittedBy").Value
                  PartArray(totalCols, 3) = rsBOM.Fields("DateSubmitted").Value
                  PartArray(totalCols, 4) = rsBOM.Fields("CIENAPartNumber").Value
                  PartArray(totalCols, 5) = 1
                  If rsBOM.Fields("RefDes").Value <> "" AND rsBOM.Fields("RefDes").Value <> "REFDES MISSING" Then
                        PartArray(totalCols, 6) = rsBOM.Fields("RefDes").Value
                        PartArray(totalCols, 6) = ""
                  End If
            End If
            PartExists = False

       Set fso = Server.CreateObject("Scripting.FileSystemObject")
      Path = "D:\temp\"&Session.SessionID&"bomreport.csv"

      'Create the scripting objects.
      Set txtFile = fso.CreateTextFile(Path)
      txtFile.WriteLine("Assembly, Submitted By, Date Submitted, CIENAPartNumber, Quantity, RefDes")
      For i = 0 to totalCols
            theText = PartArray(i, 1)&", "&PartArray(i, 2)&", "&PartArray(i, 3)&", "&PartArray(i, 4)&", "&PartArray(i, 5)&", "&Chr(34)&PartArray(i, 6)&Chr(34)
  Set txtFile = Nothing
        Set myMail = Server.CreateObject("CDONTS.NewMail")
      'Start Excel and get Application object.
      Set xlApp = CreateObject("Excel.Application")
      xlApp.Visible = False
      xlApp.Workbooks.OpenText Path
   ' Get a new workbook.
      Set xlWrkSheet = xlApp.ActiveWorkbook.ActiveSheet
    xlApp.ActiveWindow.FreezePanes = True
    With xlWrkSheet.Rows("1:1").Interior
        .ColorIndex = 5
        .Pattern = xlSolid
    End With
    With xlWrkSheet
    .Rows("1:1").Font.Bold = True
    .Rows("1:1").Font.ColorIndex = 3
            .Columns("A:A").VerticalAlignment = 1
            .Columns("B:B").VerticalAlignment = 1
            .Columns("C:C").VerticalAlignment = 1
            .Columns("D:D").VerticalAlignment = 1
            .Columns("E:E").VerticalAlignment = 1
            .Columns("F:F").ColumnWidth = 26.14
        .Columns("F:F").HorizontalAlignment = 1
        .Columns("F:F").VerticalAlignment = 1
        .Columns("F:F").WrapText = True
        .Columns("F:F").Orientation = 0
        .Columns("F:F").AddIndent = False
        .Columns("F:F").IndentLevel = 0
        .Columns("F:F").ShrinkToFit = False
        .Columns("F:F").MergeCells = False
            .Range("F1").HorizontalAlignment = 3
        .Range("F1").VerticalAlignment = 1
        .Range("F1").WrapText = True
        .Range("F1").Orientation = 0
        .Range("F1").AddIndent = False
        .Range("F1").ShrinkToFit = False
        .Range("F1").MergeCells = False
    End With
      xlApp.ActiveWorkbook.SaveAs "D:\temp\"&Session.SessionID&"-bomreport.xls", FileFormat=xlNormal
            myMail.From= ""
            myMail.To= email
            myMail.Subject= subject
      'Set myMail = Nothing
      Set rsBOM = Nothing
      Set cnBOM = Nothing
      Set xlApp = Nothing
      Set xlWrkBook = Nothing
      Set xlWrkSheet = Nothing
     fso.DeleteFile "D:\temp\"&Session.SessionID&"-bomreport.xls", True
     fso.DeleteFile "D:\temp\"&Session.SessionID&"bomreport.csv", True
     Set fso = Nothing
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
It worked!!<br>
Email Sent.


<!---End Code--->
Thanks for all your help.
Question by:bsimmons
  • 2

Expert Comment

ID: 6372717

CSV is fussy. Excel will take HTML, tho. instead of:

For i = 0 to totalCols
     theText = PartArray(i, 1)& ", " & _
          PartArray(i, 2) & ", " & _
          PartArray(i, 3) & ", " & _
          PartArray(i, 4) & ", " & _
          PartArray(i, 5) & ", " & _
          Chr(34) & PartArray(i, 6) & Chr(34)

For i = 0 to totalCols
     theText =
          "<TR><TD>" & _
          PartArray(i, 1)& "</TD><TD>" & _
          PartArray(i, 2) & "</TD><TD>" & _
          PartArray(i, 3) & "</TD><TD>" & _
          PartArray(i, 4) & "</TD><TD>" & _
          PartArray(i, 5) & "</TD><TD>" & _
          PartArray(i, 6) & "</TD>" & _
          "</TR>" & _


BTW, you can use "" in a string literal to represent a ", instead of Chr(34).

Accepted Solution

MCM earned 200 total points
ID: 6372746
and then for excel to read it, I think you want a .xls extension on the filename.

Excel is not optimized for use under MTS, so if your server gets any kind of load, you really don't want to be calling Excel from ASP. The formatting you're doing can probably be included in the HTML you're writing in my example above.

Microsoft Office Web Components (OWC)(incl w/Office 2000) may be better suited for doing the file manipulation you're doing, if you can't use the HTML technique. But I don't actually know if they expose Excel's functionality, I'm just suggesting you look into them.

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question