gpt4 book ai didi

javascript - 全部居中并根据数据长度调整excel列

转载 作者:行者123 更新时间:2023-11-29 13:14:46 25 4
gpt4 key购买 nike

如何使用 PHPexcel 根据 Excel 报告中的数据长度调整列宽?....以及如何使 Excel 中的所有数据居中?

enter image description here

当前代码:

<?php
if (!isset($_POST['send'])) { ?>
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<?php } else {
require_once 'C:\xampp\htdocs\test\Classes\PHPExcel\IOFactory.php';
$filename = 'file.xlsx';
$title = $_POST['title'];
mysql_connect("localhost","root","") or die ("cant connect!");
mysql_select_db("test") or die ("cant find database!");

$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);

$objPHPExcel = $objReader->load($filename);
$objWorksheet = $objPHPExcel->getActiveSheet();
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);

$result = mysql_query("SELECT * FROM score");
if(isset($_POST['send'])){

$headings = array(
'ID',
'NAME',
'SCORE 1',
'SCORE 2',
'OTHER QUALITIES',
'INTERVIEW',
'TOTAL',
'AIC',
'BATCHCODE',
);
$objPHPExcel->getActiveSheet()->fromArray($headings, null, 'A1');
$row = 2;
while( $rows = mysql_fetch_row($result)){
$objPHPExcel->getActiveSheet()->fromArray($rows, null, 'A' . $row);
$row++;
}
}

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$title.'.xlsx"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
}
if (!isset($_POST['send'])) { ?>

<form id="form1" name="form1" method="post" action="" >
<input name="title" type="text" id="title" value="title" />
<input type="submit" name="send" value="send to excel" id="send" />
</form>
</body>
</html>
<?php }

我想要的是...列会根据存储的数据长度自动调整...并使数据居中。

最佳答案

开发者文档的

4.6.28 部分(标题为“设置列宽”)展示了如何设置列宽,并提供了一个将其设置为自动调整其宽度以适应数据的示例:

If you want PHPExcel to perform an automatic width calculation, use the following code. PHPExcel will approximate the column with to the width of the widest column value.

$objPHPExcel->getActiveSheet()
->getColumnDimension('E')->setAutoSize(true);

可以按照开发者文档的 4.6.18 部分(标题为“格式化单元格”)中的说明设置对齐方式(例如 CENTER)

$objPHPExcel->getActiveSheet()
->getStyle('C2:I5')
->getAlignment()
->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

关于javascript - 全部居中并根据数据长度调整excel列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21613816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com