Go Premium for a chance to win a PS4. Enter to Win


CSV file and formatting

Posted on 2001-08-10
Medium Priority
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= "bsimmons@ciena.com"
            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 600 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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

885 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