我正在做一个项目,我需要将每个 mysql 记录导出到一个单独的 word 文件,但我不知道该怎么做?网上有很多文章谈论在 word 文件中导出数据,但我需要将每条记录导出到单个 word 文件中。这是我使用的代码,但它的输出只是一个 word 文件,其中包含所有记录,而我需要将每条记录保存在单独的 Microsoft Office 文件中。下面的代码使用 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.";
}
?>