[php] Word document genereren

Pagina: 1
Acties:
  • 130 views sinds 30-01-2008
  • Reageer

  • _-= Erikje =-_
  • Registratie: Maart 2000
  • Laatst online: 01-09 12:46
Voor een php projectje wat ik aan het bouwen ben wil ik gegevens uit mijn database beschikbaar maken als word document (niet PDF, het moet als txt te bewerken zijn zonder veel poespas).
Ik heb al het een en ander afgezocht, maar ik kom alleen via php & COM tegen dat er word documenten gemaakt worden, hetgeen alleen op een windows platform werkt.

Dat is niet de bedoeling aangezien het de bedoeling is dat men het word document gewoon kan downloaden & openen.

Mijn vraag is dan ook: Is er iemand die dit wel eens gedaan heeft, kan het en zo ja hoe?

  • ACM
  • Registratie: Januari 2000
  • Niet online

ACM

Software Architect

Werkt hier

Je kan proberen het word-html documentformaat te simuleren.
Maar officieel mag je geen word-docs maken omdat dat een eigen bestandsindeling is, de nieuwe office krijgt trouwens XML-bestanden, dat wordt veel makkelijker na te bouwen :)

  • The Bad Seed
  • Registratie: November 2001
  • Laatst online: 31-08 08:50

The Bad Seed

Chaotic since 1983

Ik meen ergens gelezen te hebben dat het.doc formaat niet open is voor andere devers dan MS , mis kan je een RTF ervan maken? Die zijn door word te openen en het formaat is wel open.

Hail to the guardians of the watchtowers of the north


  • _-= Erikje =-_
  • Registratie: Maart 2000
  • Laatst online: 01-09 12:46
hmm, es kijken of je daar ook tabellen/kleurtjes enz in hebt, alvast bedankt iig

  • ^Mo^
  • Registratie: Januari 2001
  • Laatst online: 04-11-2025
Hier had ik laatst ook een topic over: [rml][ PHP] Forcing download of MS Word doc[/rml]

"There are 10 kinds of people in the world, those who understand binary and those who don't" | Werkbak specs


  • flat
  • Registratie: Mei 2000
  • Niet online
heeft papa-eend ooit niet zoiets geschreven?

"Happiness is a way of travel, not a destination."
--Roy Goodman


  • TheRebell
  • Registratie: Oktober 2000
  • Laatst online: 02:07
Hmmmm... hierin heb ik me ook eens verdient.

Idd Flat, ik dacht dat papa-eend idd eens zoiets heeft gemaakt. Iig heeft hij deze eens gemaakt, alleen is dat van Word ->html.

Ik denk dat je, als je persé opmaakt wilt behouden, toch RTF zult moeten gebruiken aangezien je geen PDF wilt.

  • SchizoDuckie
  • Registratie: April 2001
  • Laatst online: 18-02-2025

SchizoDuckie

Kwaak

Aangezien ik hier zelf ook naar op zoek ben voor mn nieuwe site binnenkort, heb ik ff gegoocheld... [google=htmltodoc]

http://www.bayes.co.uk/xm...?/xml/utils/htmltodoc.xml
is iig voor windows volgens mij. ff verder zoeken naar een linux tooltje :9

Stop uploading passwords to Github!


  • TheRebell
  • Registratie: Oktober 2000
  • Laatst online: 02:07
Hey da's een leuke Papa Eend :) , nu idd nog even voor *nix en dan zijn we d'r.

Al blijf ik PDF's nog steeds het leukst vinden :P

  • CyberJack
  • Registratie: Augustus 2002
  • Laatst online: 19-08 20:21
Dit is wel mogelijk, (ook vanaf een Unix/Linux systeem), alleen als je er voor wilt zorgen dat het ook echt een word document word, moet dit geopend worden vanuit een brouwser die onder Windows draait, en op een systeem waar Word geinstalleerd is.
Het is ook mogelijk om er een Excel file van te maken.

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
/*
--Dynamic Excel or Word File from MySQL--
php-doc-xls-gen for php/MySQL: (.doc or .xls dumper):

This script takes the contents of a MySQL table and dumps it to
either a dynamically-generated MS Word File (file with ending '.doc')
or a dynamically-generated MS Excel File (file with ending '.xls').

Prerequisites:  You Must have MS Word and/or MS Excel installed on
the same computer as your web browser for this to work (although
the script can be placed on any Unix/Linux server, you have to access it using
a browser on a Windows machine with either Word or Excel installed).

How to use:
1)edit the MySQL Connection Info below for your MySQL connection
  & for the name of the MySQL table that you would like to make the dump for
2)save this file somewhere on your server
3)link to this file from another page:
  a)for Word dumps:
    <a href="this_file_name.php?w=1">link to word dump</a>
  b)for Excel dumps:
    <a href="this_file_name.php">link to excel dump</a>
  --or else--
  create a Bookmark to this page (include any of the optional parameters
  described below as part of the query string for the bookmarked URL)
4)how to reuse this code to create a dump for ANY MySQL table on your server:
  a)comment-out this line below under MySQL Connection Info:
    //$DB_TBLName = "your_table_name";
  b)include the name of your MySQL table in links (or bookmarks) to this page
    as an extra parameter:
    ie: for word dump--
    <a href="this_file_name.php?w=1&DB_TBLName=your_table_name">link to word dump</a>
    ie: for excel dump--
    <a href="this_file_name.php?DB_TBLName=your_table_name">link to excel dump</a>
  c)all of the above also holds true for the name of the Database:
    you could pass along the name of the Database as a parameter to this script
    in order to use it on many different databases on your server:
    comment out //$DB_DBName = "your database"; in this script below
    and then link to this file like:
    "this_file_name.php?$DB_DBName=your_database&DB_TBLName=your_table_name..."
5)if you're resourceful, you could also pass the sql statement to be used for this
  script as a parameter: "this_file_name.php?sql=..."
  but you might have to URL-ENCODE your sql statement before passing it to this script,
  and then URL-DECODE it in the beginning of this script for it to work.

To change the formatting of the Word or Excel File generated:
change the respective parts of the coding for the word or the excel file that format
the database info sent to the browser.  Most useful for this are the escape characters
for tabs ('\t') & line returns ('\n').  Experiment with these until you get the formatting
that you desire.

This code is freeware (GPL).  Please feel free to do with it what you'd like.
Comments, bugs, fixes to:
churmtom@hotmail.com

Originally: Nov. 25th, 2001
Updated:    May  12th, 2002
Updated:    July  1st, 2002

Thanks to Josue & Steven d.B. for helping point out
improvements for this code!

Interested in a desktop application that backs up a
COMPLETE MySQL database to an Excel File--without using ODBC?

Then try out my MySQL Database 2 Excel KonvertR program:
http://www.churm.com/konvertr/index.php
*/

//EDIT YOUR MySQL Connection Info:
$DB_Server       = "";      //your MySQL Server
$DB_Username = "";  //your MySQL User Name
$DB_Password = "";      //your MySQL Password
$DB_DBName   = "";      //your MySQL Database Name
$DB_TBLName  = "";      //your MySQL Table Name
//$DB_TBLName,  $DB_DBName, may also be commented out & passed to the browser
//as parameters in a query string, so that this code may be easily reused for
//any MySQL table or any MySQL database on your server

//DEFINE SQL QUERY:
//you can use just about ANY kind of select statement you want -
//edit this to suit your needs!
$sql = "Select * from $DB_TBLName";

//Optional: print out title to top of Excel or Word file with Timestamp
//for when file was generated:
//set $Use_Titel = 1 to generate title, 0 not to use title
$Use_Title = 1;
//define date for title: EDIT this to create the time-format you need
$now_date = date('m-d-Y H:i');
//define title for .doc or .xls file: EDIT this if you want
$title = "Dump For Table $DB_TBLName from Database $DB_DBName on $now_date";
/*

Leave the connection info below as it is:
just edit the above.

(Editing of code past this point recommended only for advanced users.)
*/
//create MySQL connection
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password)
    or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . mysql_errno());
//select database
$Db = @mysql_select_db($DB_DBName, $Connect)
    or die("Couldn't select database:<br>" . mysql_error(). "<br>" . mysql_errno());
//execute query
$result = @mysql_query($sql,$Connect)
    or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno());

//if this parameter is included ($w=1), file returned will be in word format ('.doc')
//if parameter is not included, file returned will be in excel format ('.xls')
if (isset($w) && ($w==1)){
$file_type = "msword";
$file_ending = "doc";
}
else {
$file_type = "vnd.ms-excel";
$file_ending = "xls";
}
//header info for browser: determines file type ('.doc' or '.xls')
header("Content-Type: application/$file_type");
header("Content-Disposition: attachment; filename=database_dump.$file_ending");
header("Pragma: no-cache");
header("Expires: 0");

/*    Start of Formatting for Word or Excel    */

if (isset($w) && ($w==1)) //check for $w again
/*    FORMATTING FOR WORD DOCUMENTS ('.doc')   */
{
//create title with timestamp:
if ($Use_Title == 1){
echo("$title\n\n");
}
//define separator (defines columns in excel & tabs in word)
$sep = "\n"; //new line character

    while($row = mysql_fetch_row($result))
    {
        //set_time_limit(60); // HaRa
        $schema_insert = "";
        for($j=0; $j<mysql_num_fields($result);$j++)
        {
        //define field names
        $field_name = mysql_field_name($result,$j);
        //will show name of fields
        $schema_insert .= "$field_name:\t";
            if(!isset($row[$j])) {
                $schema_insert .= "NULL".$sep;
                }
            elseif ($row[$j] != "") {
                $schema_insert .= "$row[$j]".$sep;
                }
            else {
                $schema_insert .= "".$sep;
                }
        }
        $schema_insert = str_replace($sep."$", "", $schema_insert);
        $schema_insert .= "\t";
        print(trim($schema_insert));
        //end of each mysql row
        //creates line to separate data from each MySQL table row
        print "\n----------------------------------------------------\n";
}
}
else
/*    FORMATTING FOR EXCEL DOCUMENTS ('.xls')   */
{
//create title with timestamp:
if ($Use_Title == 1){
echo("$title\n");
}
//define separator (defines columns in excel & tabs in word)
$sep = "\t"; //tabbed character

//start of printing column names as names of MySQL fields
for ($i = 0; $i < mysql_num_fields($result); $i++) {
echo mysql_field_name($result,$i) . "\t";
}
print("\n");
//end of printing column names

//start while loop to get data
/*
note: the following while-loop was taken from phpMyAdmin 2.1.0.
--from the file "lib.inc.php".
*/
    while($row = mysql_fetch_row($result))
    {
        //set_time_limit(60); // HaRa
        $schema_insert = "";
        for($j=0; $j<mysql_num_fields($result);$j++)
        {
            if(!isset($row[$j]))
                $schema_insert .= "NULL".$sep;
            elseif ($row[$j] != "")
                $schema_insert .= "$row[$j]".$sep;
            else
                $schema_insert .= "".$sep;
        }
        $schema_insert = str_replace($sep."$", "", $schema_insert);
        //following fix suggested by Josue (thanks, Josue!)
        //this corrects output in excel when table fields contain \n or \r
        //these two characters are now replaced with a space
        $schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
        $schema_insert .= "\t";
        print(trim($schema_insert));
        print "\n";
    }
}
?>

https://bottenberg.dev


  • ReLight
  • Registratie: Augustus 2001
  • Laatst online: 05-08 21:32

ReLight

echo("What Now ? !")

Dit is zo precies wat ik zocht.... Brrrr.. Kippevel.
Thanxs ! 8)

Mijn zoon & dochter zijn de toekomst, de rest is tijdsvermaak. Home assistant & & Nibe S2125-12/SMO-S40, RMU-s40 & Tado - Volvo C40 ER, SE


Verwijderd

ik heb ook zoiets wel es gezien op phpfreakz.com

GEVONDEN !! :)

  • SchizoDuckie
  • Registratie: April 2001
  • Laatst online: 18-02-2025

SchizoDuckie

Kwaak

mmm dit is niet precies wat ik zocht tough :{ Ik wil eigenlijk -echt- doc bestanden kunnen maken vanuit html, maar het enige wat ik vind op de site van w3 en google zijn html -> rtf converters :{

Stop uploading passwords to Github!

Pagina: 1