Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 385
  • Last Modified:

CSV file and formatting

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"
     
      'Response.Write(strSQL)
        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.")
            rsBOM.Close
            cnBOM.Close
            Set rsBOM = Nothing
            Set cnBOM = Nothing
            Response.End
      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
                              Else
                                    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
            Loop
            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
                  Else
                        PartArray(totalCols, 6) = ""
                  End If
                  
            End If
            PartExists = False
            rsBOM.MoveNext
      Loop

      
       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)
Response.Write(theText&"<br>")
            txtFile.WriteLine(theText)
      Next
 
 
  txtFile.Close
  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
     
     
    xlWrkSheet.Rows("2:2").Select
    xlApp.ActiveWindow.FreezePanes = True
     
     
      xlWrkSheet.Rows("1:1").Select
    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").EntireColumn.AutoFit
            .Columns("A:A").VerticalAlignment = 1
            
            .Columns("B:B").EntireColumn.AutoFit
            .Columns("B:B").VerticalAlignment = 1
            .Columns("C:C").EntireColumn.AutoFit
            .Columns("C:C").VerticalAlignment = 1
            .Columns("D:D").EntireColumn.AutoFit
            .Columns("D:D").VerticalAlignment = 1
            .Columns("E:E").EntireColumn.AutoFit
            .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
    .Range("A1").Select
    End With
       
       
      xlApp.ActiveWorkbook.SaveAs "D:\temp\"&Session.SessionID&"-bomreport.xls", FileFormat=xlNormal
     xlApp.Quit
      
      
            myMail.From= "bsimmons@ciena.com"
            myMail.To= email
            myMail.Subject= subject
            myMail.BodyFormat=0
            myMail.MailFormat=0
            myMail.Body=HTML
            myMail.AttachFile("D:\temp\"&Session.SessionID&"-bomreport.xls")
            myMail.Send
   
      '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
     
%>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>
It worked!!<br>
Email Sent.
<P>&nbsp;</P>

</BODY>
</HTML>

<!---End Code--->
Thanks for all your help.
0
bsimmons
Asked:
bsimmons
  • 2
1 Solution
 
MCMCommented:

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)
          Response.Write(theText&"<br>")
          txtFile.WriteLine(theText)
Next

try:
txtFile.WriteLine("<TABLE>")
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>" & _

          Response.Write(theText)
          txtFile.WriteLine(theText)
Next
txtFile.WriteLine("</TABLE>")

BTW, you can use "" in a string literal to represent a ", instead of Chr(34).
0
 
MCMCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now