Using VBScript to create CSV files. A function to deal with values to make Excel happy

后端 未结 2 2012
臣服心动
臣服心动 2021-01-23 06:45

I would like opinions/suggestions for improvement etc on the following function. It will be wrapped around every value being passed into a CSV file on a classic ASP page. As is

相关标签:
2条回答
  • 2021-01-23 07:14

    Few things:

    1. This section:

      If "" & txt = "" Then
          insertCSV = ""
      Else
      

      If you just want to return an empty string if txt is empty, you can just do this:

      If Len(txt) = 0 Then Exit Function
      
    2. You don't need to use End If for single-line If statements.

    3. This line:

      if isNumeric(tmp) AND left(tmp, 1) <> "0" then tmp = tmp end if
      

      You're assigning the value back to itself? What purpose does this serve?

    4. Don't you want to replace just the symbol © with &copy;? The way you have it written, you're replacing the entire text with &copy; (same goes for your other tests). I would think you'd want to do this instead:

      If InStr(tmp, "©") Then tmp = Replace(tmp, "©", "&copy;")
      

    Try making those changes and then post an updated version of your routine and let's see how it looks.

    0 讨论(0)
  • 2021-01-23 07:31

    Update (based on latest comments)

    If the database already has values stored like ’ then you need to consider that the web application is inserting the data using the wrong encoding and you are ending up with a mismatch in encoding in the database.

    • Answer to Convert UTF-8 String Classic ASP to SQL Database

    You don't need any of that the issue is Excel interprets the CSV as ASCII when you are sending it as UTF-8. There is a useful trick that fools Excel into using the correct encoding rather then assuming ASCII, I use this technique all the time and works well. It involves writing a BOM (Byte Order Mark) to the stream before displaying your data that tells Excel the data is UTF-8 not ASCII encoded.

    'BOM to dup Excel into encoding the CSV correctly instead of using ASCII!!!
    Call Response.BinaryWrite(ChrB(239) & ChrB(187) & ChrB(191))
    

    This should be specified before you use Response.Write() to output your text from the insertCSV() function.


    Using this kind of code

    if instr(tmp, "Â") then
    

    is just wrong when what the output (Â) is trying to do is tell you something is wrong with the encoding.


    Useful Links

    • Microsoft Excel mangles Diacritics in .csv files?
    • How can I output a UTF-8 CSV in PHP that Excel will read properly?
    • Which encoding opens CSV files correctly with Excel on both Mac and Windows? (still applies in this case)
    0 讨论(0)
提交回复
热议问题