比如数据库里面有张表。 test 有个字段 model (varchar)model字段存了几千条数据或者几万条数据 现在要对其字段值全部修改如果是几千条数据以内的我是这样做的:<?PHP
if(ini_get('max_execution_time')<>0)ini_set('max_execution_time',0);
if (@ini_get('com.allow_dcom') != true) @ini_set('magic_quotes_sybase', true);
$host="********";
$user="********";
$pwd="********";
$db="********";
$tb="********";
$filename = "D:\www\xls\\model.xls";
$sheet1 = "sheet2";
$excel_app = new COM("Excel.application") or Die ("Did not connect");
$Workbook = $excel_app->Workbooks->Open("$filename") or Die("Did not open $filename $Workbook");
$con1=mysql_connect($host,$user,$pwd);
$db_query=mysql_select_db($db,$con1); for($i=2;$i>0;$i++)
{
$Worksheet = $Workbook->Worksheets($sheet1);
$Worksheet->activate;
$excel_cell = $Worksheet->Range("A".$i);
$excel_cell->activate;
$excel_result1 = $excel_cell->value;
$excel_cell = $Worksheet->Range("B".$i);
$excel_cell->activate;
$excel_result2 = $excel_cell->value;
if($excel_result1<>""&&$excel_result2<>"")
{
$sql="update `".$tb."` set `products_model` = '".$excel_result1."' WHERE `products_model` = '".$excel_result2."'";
$query=mysql_query($sql);
}else
{
break;
}
}
#To close all instances of excel:
$Workbook->Close;
unset($Worksheet);
unset($Workbook);
$excel_app->Workbooks->Close();
$excel_app->Quit();
unset($excel_app);
?> 我excle表里面存两行数据一行新的数据,另一行是旧的需要改的数据
今天我差不多8000条数据运行一下结果花了20几分钟。
要是以后需要变成几万条甚至几十万条数据这样肯定是不行了
有没有什么好的办法。需求到时很简单提供一个新值已旧值做条件修改,
if(ini_get('max_execution_time')<>0)ini_set('max_execution_time',0);
if (@ini_get('com.allow_dcom') != true) @ini_set('magic_quotes_sybase', true);
$host="********";
$user="********";
$pwd="********";
$db="********";
$tb="********";
$filename = "D:\www\xls\\model.xls";
$sheet1 = "sheet2";
$excel_app = new COM("Excel.application") or Die ("Did not connect");
$Workbook = $excel_app->Workbooks->Open("$filename") or Die("Did not open $filename $Workbook");
$con1=mysql_connect($host,$user,$pwd);
$db_query=mysql_select_db($db,$con1); for($i=2;$i>0;$i++)
{
$Worksheet = $Workbook->Worksheets($sheet1);
$Worksheet->activate;
$excel_cell = $Worksheet->Range("A".$i);
$excel_cell->activate;
$excel_result1 = $excel_cell->value;
$excel_cell = $Worksheet->Range("B".$i);
$excel_cell->activate;
$excel_result2 = $excel_cell->value;
if($excel_result1<>""&&$excel_result2<>"")
{
$sql="update `".$tb."` set `products_model` = '".$excel_result1."' WHERE `products_model` = '".$excel_result2."'";
$query=mysql_query($sql);
}else
{
break;
}
}
#To close all instances of excel:
$Workbook->Close;
unset($Worksheet);
unset($Workbook);
$excel_app->Workbooks->Close();
$excel_app->Quit();
unset($excel_app);
?> 我excle表里面存两行数据一行新的数据,另一行是旧的需要改的数据
今天我差不多8000条数据运行一下结果花了20几分钟。
要是以后需要变成几万条甚至几十万条数据这样肯定是不行了
有没有什么好的办法。需求到时很简单提供一个新值已旧值做条件修改,
你可以考虑1. 将excel导入到数据库的一个临时表中,修改完毕再导出到excel.
2. excel保存为csv格式,然后直接对csv字符串操作.
如果你不想用大型数据库,可以考虑用桌面数据库,SQLite什么的,都可以。