我正在做一个项目,我需要将每个mysql记录导出到一个单独的word文件,但我不知道如何做到这一点?有很多文章在网上讨论导出数据在一个word文件,但我需要每个记录被导出到一个单独的word file.here是我使用的代码,但它的输出只是一个word文件包含所有的记录,而我需要每个记录被保存在一个单独的Microsoft Office File.the代码下面使用PHPexcel类读取excel文件,然后导入到Mysql现在我想保存mysql的每个记录到一个单独的Microsoft word文件。谢谢你的回答。
<?php
require_once 'Classes/PHPExcel.php';
$objReader = new PHPExcel_Reader_Excel2007();
$objPHPExcel = $objReader->load('book1.xlsx');
$rowIterator = $objPHPExcel->getActiveSheet()->getRowIterator();
$skip_rows = 0;
$excell_array_data = array();
foreach($rowIterator as $row){
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
if($skip_rows >= $row->getRowIndex ()) continue;
$rowIndex = $row->getRowIndex ();
$excell_array_data[$rowIndex] = array();
foreach ($cellIterator as $cell) {
$excell_array_data[$rowIndex][$cell->getColumn()] = $cell->getCalculatedValue();
}
}
$link = @mysql_connect('localhost', 'root', '');
if ($link){
$db_selected = mysql_select_db('proje', $link);
@mysql_set_charset('utf8',$link);
//Create Database table with one Field
$sql = "CREATE TABLE xlsx (
rowID INT NOT NULL ,
PRIMARY KEY (rowID)
)";
mysql_query($sql);
//Create Others Field (A, B, C & ...)
$columns_name = array();
$columns_name = $excell_array_data[$skip_rows+1];
foreach (array_keys($columns_name) as $fieldname ){
$sql = "ALTER TABLE xlsx ADD $fieldname VARCHAR(1000)";
mysql_query($sql);
}
$i = 0;
//Insert Excel data to MySQL
foreach( $excell_array_data as $k=>$v){
//echo $keys."\n<br/>";
//var_dump ($v);
$keys = join(array_keys($v), ',');
$values = join($v, "','");
/*
ini_set("com.allow_dcom","true");
// Create new COM object – word.application
$word = new COM("word.application");
// Hide MS Word application window
$word->Visible = 0;
//Create new document
$word->Documents->Add();
// Define page margins
$word->Selection->PageSetup->LeftMargin = '2';
$word->Selection->PageSetup->RightMargin = '2';
// Define font settings
$word->Selection->Font->Name = 'Arial';
$word->Selection->Font->Size = 10;
// Add text
$word->Selection->TypeText("word");
$word->Selection->TypeText("$v[B]");
$word->Selection->TypeText("$v[C]");
$word->Selection->TypeText("$v[D]");
$word->Selection->TypeText("$v[E]");
$word->Selection->TypeText("$v[F]");
// Save document
$filename = tempnam(__FILE__, "word");
$word->Documents[1]->SaveAs($filename);
// Close and quit
$word->quit();
unset($word);
$i++;
*/
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment; Filename=SaveAsWordDoc.doc");
$sql = "insert into xlsx (rowID, $keys) values ($k, '$values') " ;
mysql_query($sql);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">
<title>Saves as a Word Doc</title>
</head>
<body>
<h1><?php echo $k; ?></h1>
<?php
var_dump($v) ;
?>
<?php
}
} else {
echo "Error in database connection.";
}
?>发布于 2013-11-20 07:32:27
从你的问题中,我了解到问题在于创建多个Word文档,而不是从PHP创建一个Word文档。从PHP (网站)创建一个Word文档可以使用多种方法完成。当用户在网站中工作而不是在Word中建立文档时,我更喜欢使用带有一些占位符的RTF文件。对于低音量,它们很容易设置。尽管如此,对于大量不同的模板或表格,从Invantive Composition (我在那里工作)的开发中我了解到,您可能想要切换到XML格式或其函数表示形式。在您的情况下,我建议从PHP生成一些RTF文件,然后将它们压缩在一起,因为一个网站通常在每次迭代中只提供一个附件。将它们压缩在一起可以在内存中完成,但您可能还希望考虑在磁盘上的某个位置进行压缩,这取决于创建的文件的大小。
https://stackoverflow.com/questions/15085334
复制相似问题