skip to Main Content

I am exporting a table from DB in csv format. I have numbers starting with 0, such as 04596 or 059438.
If I open this csv in LibreOffice or Excel, the 0 will be thrown away and numbers are shown as 4596 and 59438.
I am exporting numbers as text, and if I open csv file in notepad++ the 0s are there, all of them.

Is there a workaround to make numbers visible as they are in file?

2

Answers


  1. convert it to string such as :
    echo "'".$number."'";

    Login or Signup to reply.
  2. When you open your CSV file in Calc manually, you see the text import filter settings window. Take your time to click OK, don’t skip this step!

    Select the columns that should not be recognized as numbers, but should remain text with a leading zero and specify their type as Text. Also set the correct types for the rest of the columns. You can specify Hide for columns that you do not need. Check the status of the rest of the parameters – code page, language, type of quotes for text fields, and so on.

    ImportCSV.png

    Just now click OK. Make sure the import went as you expected.

    Now run this simple macro

    Sub getMeCode
    Dim oDoc As Variant
    Dim aArgs As Variant
    Dim sResult As String
    Dim sTemp As String
        oDoc = ThisComponent
        aArgs = oDoc.getArgs()
        sTemp = getValArg(aArgs, "URL")
        sResult = "Dim sUrl As String, oDoc As Variant" + Chr(10) + "sUrl = convertToURL(""" + convertFromURL(sTemp) +""")" + Chr(10)
        sResult = sResult + "Dim OpenProp(1) as New com.sun.star.beans.PropertyValue" + Chr(10)
        sResult = sResult + "OpenProp(0).name=""FilterName""" + Chr(10)
        sResult = sResult + "OpenProp(1).name=""FilterOptions""" + Chr(10)
        sTemp = getValArg(aArgs, "FilterName")
        sResult = sResult + "OpenProp(0).value=""" + sTemp + """" + Chr(10)
        sTemp = getValArg(aArgs, "FilterOptions")
        sResult = sResult + "OpenProp(1).value=""" + sTemp + """" + Chr(10)
        sResult = sResult + "If Not FileExists(sUrl) Then Exit Sub" + Chr(10)
        sResult = sResult + "oDoc = stardesktop.LoadComponentFromURL(sUrl, ""_blank"",0, OpenProp())"
        MsgBox(sResult,0,"Select, Press Ctrl+C and Paste to macro")
    End Sub
    

    This macro uses the getValArg helper function, here is its text

    Function getValArg(aArg As Variant, sNameArg As String) As Variant
    Dim i As Long
    Dim tmp As Variant
        getValArg = ""
        For i = LBound(aArg) To UBound(aArg)
            tmp = aArg(i)
            If UCase(tmp.Name) = UCase(sNameArg) Then
                getValArg = tmp.Value
                Exit Function
            EndIf
        Next i
    End Function
    

    As a result, you will receive such a message.

    ImportCSVFilterOptions.png

    Pay attention to the underlined line. If you use just such a parameter for your files (regardless of the programming language that you use to open the file in Calc), then the result will be correct in most cases.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search