convert HTML string into csv/xls/doc

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • >>Shailesh

    convert HTML string into csv/xls/doc

    CODE:
    $stuff = '<html><head><t itle>stuff</title></head><body><b>H ello</b>
    World</body></html>';


    header("Expires : Mon, 26 Jul 1997 05:00:00 GMT"); //
    Date in the past
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); //
    always modified
    header("Cache-Control: no-store, no-cache, must-revalidate"); //
    HTTP/1.1
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache"); //
    HTTP/1.0


    header("Content-type: application/msword; filename=stuff. doc");
    //header("Content-Disposition: attachment; filename=stuff. doc");
    echo $stuff;


    However, upon attempting to run this script I ran into the following
    error in IE:


    Internet Explorer could not open this script: test.php (note: I am
    trying to change it to be "stuff.doc" ). Nothing ever showed up.


    test.php (the code you see) is in a Linux environment in a folder
    protected by .htaccess.


    In NS 7.0 it produces no errors but it tries to save the downloaded
    file as "test.php" and not "stuff.doc" .


    I am unsure how to do this in PHP although I can do it in TCL and have
    done it that way before, that, however, is not an option here.


    Please help, I'm stuck at the very beginning of attempting customized
    simple reporting.


    Thank You,
    Shailesh P.

  • Gerard van Wilgen

    #2
    Re: convert HTML string into csv/xls/doc

    >>Shailesh<< wrote:[color=blue]
    > CODE:
    > $stuff = '<html><head><t itle>stuff</title></head><body><b>H ello</b>
    > World</body></html>';
    >
    >
    > header("Expires : Mon, 26 Jul 1997 05:00:00 GMT"); //
    > Date in the past
    > header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); //
    > always modified
    > header("Cache-Control: no-store, no-cache, must-revalidate"); //
    > HTTP/1.1
    > header("Cache-Control: post-check=0, pre-check=0", false);
    > header("Pragma: no-cache"); //
    > HTTP/1.0
    >
    >
    > header("Content-type: application/msword; filename=stuff. doc");
    > //header("Content-Disposition: attachment; filename=stuff. doc");
    > echo $stuff;
    >
    >
    > However, upon attempting to run this script I ran into the following
    > error in IE:
    >
    >
    > Internet Explorer could not open this script: test.php (note: I am
    > trying to change it to be "stuff.doc" ). Nothing ever showed up.[/color]

    I do not know if this has anything to do with IE not being able to open
    the document, but why do you specify a content-type of
    "applicatio n/msword" when the document seems to be an HTML-document?


    --

    Gerard van Wilgen

    www.majstro.com - Multilingual translation dictionary
    www.erotikejo.com - International sex portal


    Comment

    • >>Shailesh

      #3
      Re: convert HTML string into csv/xls/doc

      hi,
      i simply wants to convert html string to csv/xls file..i hv tried

      $stuff = '<html><head><t itle>stuff</title></head><body><b>H ello</b>
      World</body></html>';

      header("Content-Type: application/vnd.ms-excel");
      header("Expires : 0");
      header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
      header("content-disposition: attachment;file name=str.xls");

      print $stuff;

      this would ask to save as xls file...but while opening that xls file
      it's giving error..
      because it's saving html coded file with only xls extension.

      would anyone plz help me to save HTML string in xls/cvs format..

      Regards,
      Shailesh P

      Comment

      • Kirsten

        #4
        Re: convert HTML string into csv/xls/doc

        >>Shailesh<< schrieb:[color=blue]
        > this would ask to save as xls file...but while opening that xls file
        > it's giving error..
        > because it's saving html coded file with only xls extension.[/color]
        Excel needs binary![color=blue]
        >
        > would anyone plz help me to save HTML string in xls/cvs format..[/color]
        What did you want, a csv or a Excel File?
        If you need csv you could write it like a normal textfile, if you want
        to get a excel file, try the Excel_Spreadshe et_Writer from the PEAR package.

        -Kirsten

        Comment

        • >>Shailesh

          #5
          Re: convert HTML string into csv/xls/doc

          hi
          In my reporting script i want to give report format option(HTML &
          CSV).
          i hv written a script that generates HTML report...but when it is
          required in CSV form
          is there any way to convert my html embedded string into csv file..????

          Thank you
          Shailesh P.

          Comment

          • >>Shailesh

            #6
            Re: convert HTML string into csv/xls/doc

            hi
            In my reporting script i want to give report format option(HTML &
            CSV).
            i hv written a script that generates HTML report...but when it is
            required in CSV form
            is there any way to convert my html embedded string into csv file by
            setting headers..????

            Thank you
            Shailesh P.

            Comment

            • Kirsten

              #7
              Re: convert HTML string into csv/xls/doc

              >>Shailesh<< schrieb:[color=blue]
              > is there any way to convert my html embedded string into csv file by
              > setting headers..????[/color]
              csv->Comma Separated Values!!!
              Therfor you will need a seperate script or code for generating this csv.
              Changing the header will only inform the browser about the content of
              the output.
              So what should excel do with a file wich contains html-code?

              -Kirsten

              Comment

              • ksmith01
                New Member
                • Sep 2005
                • 1

                #8
                Shailesh,

                Setting the content-type header to "applicatio n/msword" should make the browser open the file as a Word document. This isn't a problem as Word understands HTML (more or less), however it's no use for outputting an Excel spreadsheet.

                CSV is the way to go here and as Kirsten said, a CSV file is just data seperated by commas - easy to output with a simple script once you understand the structure. Try saving an excel spreadsheet as a CSV file, then open the CSV file in notepad to see how the data is formatted - this should give you a good idea of what your script needs to output.

                Theoretically it would be possible to have a similar script to format your data into a valid .xls file, however this would be a MAJOR pain as .xls is a binary file format. Go with CSV.

                Comment

                Working...