excel文件上传功能

<?php


session_start();
//if(!isset($_SESSION['userid'])){
//    header("Location:index.php");
//    exit();
//}
$userid             = $_SESSION['userid'];
$Product_info_total = 0;


$Contract_ID = $_REQUEST['Contract_ID'];
$t      = $_REQUEST["t"];
$save_filename  = $_REQUEST['save_filename'];


$thbgcolor ="bgcolor='#C9D6A6'";
$tdbgcolor0="bgcolor='#DFE3E6'";
$tdbgcolor1="bgcolor='#EDF1F4'";
$tdbgcolor2="bgcolor='#66CCFF'";
$tdbgcolor3="bgcolor='#B0C4DE'";
$tdbgcolor4="bgcolor='#FFE4C4'";
$tdbgcolor5="bgcolor='#FF0000'";


define("MSSP_TABDEF", "border='1' cellpadding='1' cellspacing='1' width='100%'");
define("MSSP_TABDEF2", "border='1' cellpadding='1' cellspacing='1' width='50%'");


$uploadfile='';
$upload_file_name = '';
if($t == 1) //选择附件
{
$upload_file_name=$_FILES['input_execel_contract_button']['name']; 

$uploadfile = "/var/www/html/htdocs/upload/" . $upload_file_name; 

$tmp_filename = $_FILES['input_execel_contract_button']['tmp_name'];
$bOK = 0;
if (@move_uploaded_file($tmp_filename, $uploadfile))
{
$bOK = 1;
}
else
{
echo json_encode(array('success'=>false, 'message'=> $upload_file_name));
exit;
}
}
else
if($t == 2) //保存附件
{
$uploadfile = "/var/www/html/htdocs/upload/" . $save_filename; 
}
else
{
echo json_encode(array('success'=>false, 'message'=> '参数错误'));
exit;
}




/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('PRC');


if (PHP_SAPI == 'cli')
die('This example should only be run from a Web Browser');


/** Include PHPExcel */
//require_once '/var/www/html/htdocs/phpexcel/Classes/PHPExcel.php';
require_once '/var/www/html/htdocs/phpexcel/Classes/PHPExcel/IOFactory.php';
require_once('/var/www/html/htdocs/database.php');


$objPHPExcel = PHPExcel_IOFactory::load($uploadfile);


if(!$objPHPExcel)
{
echo json_encode(array('success'=>false, 'message'=> '格式错误!'));
exit;
}
/**读取excel文件中的第一个工作表*/ 
$currentSheet = $objPHPExcel->getSheet(0); 
/**取得最大的列号*/ 
$allColumn = $currentSheet->getHighestColumn(); 
/**取得一共有多少行*/ 
$allRow = $currentSheet->getHighestRow(); 


$thtml = display_contract_content_excel($currentSheet, $allRow, $MaxContractID, $t, $msspdb);




function display_contract_content_excel($currentSheet, $allRow, $MaxContractID, $t, $msspdb)
{
global $thbgcolor ;
global $tdbgcolor0;
global $tdbgcolor1;
global $tdbgcolor2;

$col = ord('B')-65;// B4
$Contract_Date      = getCellDate($currentSheet, $col, 4);
$Employee_Name      = $currentSheet->getCellByColumnAndRow($col, 5)->getValue();
$Department_Name    = $currentSheet->getCellByColumnAndRow($col, 6)->getValue();

$col = ord('E')-65;// E4
$Contract_No        = $currentSheet->getCellByColumnAndRow($col, 4)->getValue();
$Contract_Customer  = $currentSheet->getCellByColumnAndRow($col, 5)->getValue();
$Contract_Verb_Date = getCellDate($currentSheet, $col, 6);

if($t == 2)
{
$sql = "update tbl_contract set ";
$sql.= " Date            = '$Contract_Date', ";
$sql.= " No              = '$Contract_No', ";
$sql.= " Customer        = '$Contract_Customer', ";
$sql.= " Verb_Date       = '$Contract_Verb_Date' ";
$sql.= " where ID=$MaxContractID";
mysql_query($sql,$msspdb);



$thtml = '<table '.MSSP_TABDEF2 . '>';

$html = sprintf("<tr> <td %s align='right'>日期:</td><td>%s</td>", $tdbgcolor0, $Contract_Date); //第一个%s是颜色,第二个%s是值
$thtml .= $html; 
$html = sprintf(" <td %s align='right'>合同编号:</td><td>%s</td>", $tdbgcolor0, $Contract_No);
$thtml .= $html; 
$thtml .= "</tr> "; 

$html = sprintf("<tr> <td %s align='right'>销售负责人:</td><td>%s</td>", $tdbgcolor0, $Employee_Name);
$thtml .= $html; 
$html = sprintf(" <td %s align='right'>客户名称:</td><td>%s</td>", $tdbgcolor0, $Contract_Customer);
$thtml .= $html; 
$thtml .= "</tr> "; 

$html = sprintf("<tr> <td %s align='right'>部门:</td><td>%s</td>", $tdbgcolor0, $Department_Name);
$thtml .= $html; 
$html = sprintf(" <td %s align='right'>合同签订日期:</td><td>%s</td>", $thbgcolor, $Contract_Verb_Date);
$thtml .= $html; 
$thtml .= "</tr> "; 
$thtml .= "</table>"; 


$thtml .= '<table '.MSSP_TABDEF . '>';


$html = sprintf("<tr > <td %s align=\"center\" colspan=\"7\">合同产品信息表(销售人员填写)</td>", $tdbgcolor0);
 $thtml .= $html; 
$html = sprintf(" <td %s align=\"center\" colspan=\"7\" >考核成本信息表(产品经理填写)</td>", $tdbgcolor2);
 $thtml .= $html; 
 $thtml .= "</tr>"; 
$thtml .= display_contract_product_and_assessment_info_excel($currentSheet, $allRow, $MaxContractID, $t, $msspdb);
 $thtml .= display_contract_other_info_excel($currentSheet, $allRow, $MaxContractID, $t, $msspdb);
 
 $thtml .= "</table>"; 
 
return $thtml;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!关于Java上传Excel文件的功能开发,你可以使用Apache POI库来实现。下面是一个简单的步骤指南: 1. 导入Apache POI库:在你的项目中添加Apache POI的依赖,可以通过Maven或Gradle来进行配置。 2. 创建上传文件的表单:在你的前端页面中创建一个表单,用于选择Excel文件并提交上传。 3. 后端处理上传文件:在后端代码中,你需要处理上传的文件。可以使用Spring MVC或Servlet来接收文件,并保存到服务器的临时目录。 4. 解析Excel文件:使用Apache POI库来解析上传Excel文件。根据你的需求,可以选择使用HSSF(用于处理Excel 2003及之前版本)或XSSF(用于处理Excel 2007及之后版本)。 5. 读取Excel数据:通过POI库提供的API,你可以读取Excel文件中的数据。根据Excel文件的结构,使用合适的API方法来提取数据。 6. 处理Excel数据:根据你的业务需求,对读取到的Excel数据进行处理。你可以将数据存储到数据库中,进行进一步的计算或展示等操作。 7. 返回处理结果:将处理后的结果返回给前端页面,可以是成功或失败的消息,或者是处理后的数据展示。 需要注意的是,对于大型Excel文件或者需要处理大量数据的情况,建议采用流式处理(Streaming API)来避免内存溢出的问题。 希望以上步骤能对你的Java上传Excel文件功能开发有所帮助!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值