2013-10-13 83 views
-1

我有擁有約hundres電子郵件,在第一列的Excel文件分割..Excel數據讀取和PHP

它這個樣子,

Ameerali Uncle. <[email protected]> 
Afkaar (Habeeb). <[email protected]> 
Ameerali Uncle. <[email protected]> 

我想借此從打破這種<>簽名並將其放入新列,並留下名稱的第一列。所以這樣做後,它會像

column 1   column 2 
Afkaar (Habeeb) [email protected] 

這是一個可能的事情與PHP?有人可以建議我有一個很好的機制來做到這一點。

感謝

EDIT 1

我的樣品excel文件如下所示。

enter image description here

+1

讀取Excel內容到PHP,操作字符串的內容(正則表達式可能是必要的),然後重新創建excel文件。 – ihsan

+1

查看[PHP-ExcelReader](http://sourceforge.net/projects/phpexcelreader/) – gwillie

+1

@mazraara你需要VBA而不是php。 – ihsan

回答

3

您可以使用PHPExcel庫來讀取/ /寫的Excel文件。 (下載和文件可在http://phpexcel.codeplex.com/)。然後使用RegEx來分隔字符串。

編輯1

該代碼將是這樣的:

// Include PHPExcel_IOFactory 
include 'PHPExcel/IOFactory.php'; 

$inputFileName = './example1.xlsx'; 
$outputFileName = './output.xlsx'; 

// Read your Excel workbook 
try { 
    $inputFileType = PHPExcel_IOFactory::identify($inputFileName); 
    $objReader = PHPExcel_IOFactory::createReader($inputFileType); 
    $objPHPExcel = $objReader->load($inputFileName); 

    $outputObj = new PHPExcel(); 
} catch(Exception $e) { 
    die('Error loading file: '. $e->getMessage()); 
} 

// Get worksheet dimensions 
$sheet = $objPHPExcel->getSheet(0); 
$highestRow = $sheet->getHighestRow(); 

$outputObj->setActiveSheetIndex(0); 
$outSheet = $outputObj->getActiveSheet(); 

// Loop through each row of the worksheet in turn 
for ($row = 2; $row <= $highestRow; $row++){ // As row 1 seems to be header 
    // Read cell A2, A3, etc. 
    $line = $sheet->getCell('A' . $row)->getValue(); 
    preg_match("|([^\.]+)\. <([^>]+)>|", $line, $data); 
    // $data[1] will be name & $data[2] will be email 
    $outSheet->setCellValue('A' . $row, $data[1]); 
    $outSheet->setCellValue('B' . $row, $data[2]); 
} 

// write new data into a .xlsx file 
$objWriter = new PHPExcel_Writer_Excel2007($outputObj); 
$objWriter->save($outputFileName); 

來源:Stackoverflow QuestionPHPExcel Example

+0

tnx很多這個,順便說一句可能顯示這個例子與PHPExcel庫的使用情況嗎? – dev1234

+0

@ SignNaL89請檢查上面編輯的問題。我已經放了一張照片。 – dev1234