php导入excel 代码,php excel 导入导出源代码

news/2024/11/14 15:03:27/

导入

require_once './includes/reader.php';

// ExcelFile($filename, $encoding);

$data = new Spreadsheet_Excel_Reader();

// Set output Encoding.

$data->setOutputEncoding('gbk');

//”data.xls”是指要导入到mysql中的excel文件

$data->read('date.xls');

@ $db = mysql_connect('localhost', 'root', '1234') or

die("Could not connect to database.");//连接数据库

mysql_query("set names 'gbk'");//输出中文

mysql_select_db('wenhuaedu'); //选择数据库

error_reporting(E_ALL ^ E_NOTICE);

for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {

//以下注释的for循环打印excel表数据

/*

for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {

echo """.$data->sheets[0]['cells'][$i][$j]."",";

}

echo "n";

*/

//以下代码是将excel表数据【3个字段】插入到mysql中,根据你的excel表字段的多少,改写以下代码吧!

$sql = "INSERT INTO test VALUES('".

$data->sheets[0]['cells'][$i][1]."','".

$data->sheets[0]['cells'][$i][2]."','".

$data->sheets[0]['cells'][$i][3]."')";

echo $sql.'
';

$res = mysql_query($sql);

ini_set('memory_limit', '-1');

set_time_limit(0);

$callStartTime = microtime(true);

PHPExcel_CachedObjectStorageFactory::cache_in_memory_serialized;

$objExcel = new PHPExcel();

if (isset($datas) && isset($startdate) && isset($enddate)){

$title = time();

$objProps = $objExcel->getProperties();

$objProps->setCreator("cbsi ");

$objProps->setLastModifiedBy("report for finance");

$objProps->setTitle("report for finance");

$objProps->setSubject("report for finance");

$objProps->setDescription("report for finance");

$objProps->setKeywords("report for finance");

$objProps->setCategory("report for finance");

$objExcel->setActiveSheetIndex(0);

$objActSheet = $objExcel->getActiveSheet();

//设置当前活动sheet的名称

$objActSheet->setTitle('report for finance');

/设置标题

$already_income_amount = 0;

$invoice_amount = 0;

$The_invoices_amount = 0;

foreach ($datas as $key => $val){

$already_income_amount = $already_income_amount + $val['already_income_amount'];

$invoice_amount = $invoice_amount + $val['invoice_amount'];

$The_invoices_amount = $The_invoices_amount + $val['The_invoices_amount'];

}

foreach ($datas as $key => $val){

$key = $key+3;

//$objActSheet->setCellValue('A'.$key, $val['id']);

$objActSheet->setCellValue('A'.$key, $key-2);

$objActSheet->setCellValue('B'.$key, $val['company_name']);

$objActSheet->setCellValue('C'.$key, $val['bu_name']);

$objActSheet->setCellValue('D'.$key, $val['customer_name']);

$objActSheet->setCellValue('E'.$key, $val['contract_number']);

$objActSheet->setCellValue('F'.$key, $val['already_income_amount']);

$objActSheet->setCellValue('G'.$key, $val['invoice_amount']);

$objActSheet->setCellValue('H'.$key, $val['The_invoices_amount']);

$objActSheet->setCellValue('I'.$key, $val['Differences_note']);

$objExcel->getActiveSheet()->setSharedStyle(Custom_ExcelStyle::line(), "A".$key.":I".$key);

}

$count = count($datas)+3;  //=27

$objActSheet->setCellValue("A"."$count", '总计');

$objActSheet->setCellValue("B"."$count", '-');

$objActSheet->setCellValue("C"."$count", '-');

$objActSheet->setCellValue("D"."$count", '-');

$objActSheet->setCellValue("E"."$count", '-');

$objActSheet->setCellValue("F"."$count", $already_income_amount);

$objActSheet->setCellValue("G"."$count", $invoice_amount);

$objActSheet->setCellValue("H"."$count", $The_invoices_amount);

$objActSheet->setCellValue("I"."$count", '-');

$objExcel->getActiveSheet()->setSharedStyle(Custom_ExcelStyle::total(), "A".$count.":I".$count);

//表格第一行内容

$objActSheet->setCellValue('A1', 'CCOM月度/季度/年度销售及发票开具情况统计表');

$objActSheet->setCellValue('A2', '日期:'.$startdate."到".$enddate);

//显式指定内容类型

//$objActSheet->setCellValueExplicit('A5', '847475847857487584',PHPExcel_Cell_DataType::TYPE_STRING);

//合并单元格

$objActSheet->mergeCells('A1:I1');

$objActSheet->mergeCells('A2:I2');

//设置表格内容居中

//$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);

//分离单元格

//$objActSheet->unmergeCells('B1:C22');

//*************************************

//设置宽度

$objActSheet->getColumnDimension('A')->setWidth(10);

$objActSheet->getColumnDimension('B')->setWidth(40);

$objActSheet->getColumnDimension('C')->setWidth(30);

$objActSheet->getColumnDimension('D')->setWidth(40);

$objActSheet->getColumnDimension('E')->setWidth(20);

$objActSheet->getColumnDimension('F')->setWidth(30);

$objActSheet->getColumnDimension('G')->setWidth(30);

$objActSheet->getColumnDimension('H')->setWidth(40);

$objActSheet->getColumnDimension('I')->setWidth(30);

$objExcel->createSheet();

$objExcel->getActiveSheet()->setSharedStyle(Custom_ExcelStyle::head(), "A1:I3");

$objWriter = new PHPExcel_Writer_Excel2007($objExcel);     $objWriter->setUseDiskCaching(true);   $outputFileName = "download/excel/";   $outputFileName .= Custom_Session::Get('id')."invoice_issue".".xlsx";         $objWriter->save($outputFileName);   //$objPHPExcel->disconnectWorksheets();   unset($objPHPExcel);   }   $back =  array(1,$outputFileName);         return $back;


http://www.ppmy.cn/news/630510.html

相关文章

PHP面试题及答案解析(1)—PHP语法基础

1. strlen( )与 mb_strlen( )的作用分别是什么&#xff1f; strlen和mb_strlen都是用于获取字符串长度。strlen只针对单字节编码字符&#xff0c;也就是说它计算的是字符串的总字节数。如果是多字节编码&#xff0c;如 gbk 和 utf-8&#xff0c;使用 strlen 得到是该字符的总字…

matlab中diamond,my-DIAMOND 经典的莫泰识别分析软件 ,在matlab平台下 238万源代码下载- www.pudn.com...

文件名称: my-DIAMOND下载 收藏√ [ 5 4 3 2 1 ] 开发工具: matlab 文件大小: 8907 KB 上传时间: 2014-01-22 下载次数: 1 提 供 者: lxiu 详细说明&#xff1a;经典的莫泰识别分析软件 diamond,在matlab平台下-diamond matlab toolbox 文件列表(点击判断是否您需要的文件…

php面试题__语言基础

1. strlen( )与 mb_strlen( )的作用分别是什么(新浪网技术部) strlen和mb_strlen都是用于获取字符串长度。 strlen只针对单字节编码字符&#xff0c;也就是说它计算的是字符串的总字节数。如果是多字节编码&#xff0c;如 gbk 和 utf-8&#xff0c;使用 strlen 得到是该字符的…

如何提升近红外实验的信噪比?如何去除伪迹?

关注“心仪脑”查看更多脑科学知识的分享。 近红外光谱脑成像&#xff08;fNIRS&#xff09;作为一项无创技术&#xff0c;存在无创脑成像技术的一个共同弱点&#xff1a;信噪比较低。但是因其便携&#xff0c;鲁棒性高&#xff0c;造价较低廉&#xff0c;生态效度高&#xff…

PHP笔试题总结1

---恢复内容开始--- 1.1 PHP中判断变量的相关函数 1.2 strlen( )与mb_strlen( )的作用分别是什么(新浪网技术部) strlen 和mb_strlen 都是用于获取字符串长度的&#xff0c;其中strlen只针对单字节编码字符&#xff0c;也就是说它计算的是字符串的总字节数&#xff0c;如…

php面试题之三——PHP语言基础(基础部分)

1. strlen( )与 mb_strlen( )的作用分别是什么(新浪网技术部) strlen和mb_strlen都是用于获取字符串长度。strlen只针对单字节编码字符&#xff0c;也就是说它计算的是字符串的总字节数。如果是多字节编码&#xff0c;如 gbk 和 utf-8&#xff0c;使用 strlen 得到是该字符的总…

scrapy爬虫

scrapy爬虫 爬取单个页面 douban.py import scrapy from scrapy import Selector from spiderTest.items import MovieItemclass DoubanSpider(scrapy.Spider):name "douban"allowed_domains ["movie.douban.com"]start_urls ["https://movie.do…

VMware CEO接受CBSi专访实录

::   各位网友&#xff1a;2010年春天好! ::   有小半年没见面了&#xff0c;春节前听说VMware CEO保罗来ZDnet中国做嘉宾和ZDnet商量后&#xff0c;觉得应该让更多层面的网友来参与&#xff0c;所以就在CBSi(中国)各家网站上做了这次联播。 ::   VMware公司12年前成立于美…