2013-10-16 3 views
1

단일 xlsx 파일 내에서 올바른 순서로 데이터를 내보내려고합니다.PHPExcel에서 데이터를 내보내 excel

이 코드는 작동하지만 모든 데이터를 xlsx 파일에 저장합니다. 그것은 꽤 많이 밖으로 정렬과 함께 예를 같은 사람을 배치하는 것입니다 내가 뭘하려고 오전 스프레드 시트

eg. 
ID Name Type Amount 
1 aaaa Income 20 
2 bbbb Exped 30 
3 cccc Income 40 

에, 그것은 현재 데이터베이스 내부에 어떤 순서로 내 데이터베이스에서 모든 데이터를 덤프합니다. 어떻게해야 탁월합니까 :

row1: ID Name Type Amount 
row2: INCOME 
row3: 1  aaaa Income 20 
row4: 3  cccc Income 40 



row6: EXPENDITURE 
row7: 2  bbbb Exped 30 

나는 이것을 어떻게 달성 할 수 있을지에 대한 아이디어가 있습니까? 다음은 제 코드입니다.

$query = "SELECT DISTINCT Type FROM financial"; 
$types = mysql_fetch_array($query); 

을 그리고 각 유형을 통해 루프는 개별적으로 데이터를 가져 오는 : 건배,

/** Query 1.0 */ 
    $query = "SELECT * FROM financial"; 

    if ($result = mysql_query($query) or die(mysql_error())) { 
/** Create a new PHPExcel object 1.0 */ 
    $objPHPExcel = new PHPExcel(); 
    $objPHPExcel->getActiveSheet()->setTitle('Data'); 
    } 

    /**HEADINGS*/ 
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'FINANCIAL_ID'); 
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'TYPE'); 
$objPHPExcel->getActiveSheet()->setCellValue('C1', 'NAME'); 
$objPHPExcel->getActiveSheet()->setCellValue('D1', 'YEAR'); 
$objPHPExcel->getActiveSheet()->setCellValue('E1', 'AMOUNT'); 

/** Loop through the result set 1.0 */ 
    $rowNumber = 2; //start in cell 1 
    while ($row = mysql_fetch_row($result)) { 
     $col = 'A'; // start at column A 
     foreach($row as $cell) { 
      $objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell); 
      $col++; 
     } 
     $rowNumber++; 
} 
+0

+1 난 당신이 할 수 몰랐다 그런 문자를 증가 시키십시오. 멋진 물건 – immulatin

답변

1

당신이 얻을 수있는 모든 종류의 시작

foreach($types as $type){ 
    $query = "SELECT * FROM financial WHERE TYPE='$type'"; 
    while($row = mysql_fetch_row($query)){ 
     // Output your data into the spreadsheet 
    } 

    // Get the subtotal for each type. 
    $query = "SELECT SUM(Amount) as total FROM financial WHERE Type='$type'"; 
    $result = mysql_fetch_array($query); 
    $subtotal = $result['total']; 
    // Add the subtotal to the spreadsheet 

    $row += 5; // Skip a few rows in between if you want 
} 

// Get the Total for everything 
$query = "SELECT SUM(Amount) as total FROM financial"; 
$result = mysql_fetch_array($query); 
$total = $result['total']; 
// Add the total to the spreadsheet 
+0

yeap 나는 벌써 그것을했다. 소득은 아무 문제도 보여주지 않을 것이다. 그것의 다만 제 2 분대 : 소득의 소계의 밑에 갈 비용, 나는 거기 자료를 두는 너무 확실하지 않다. 이 코드에서 rownumber가 수동으로 선택됩니다. 다음 빈/빈 행을 어떻게 선택합니까? – Tuzki

+0

특정 유형의 작업이 완료되면 부분합 열을 추가하려고합니다. 모든 유형을 처리 할 때 마지막에 합계를 추가하십시오. 아직 내가 완전히 이해하고 있는지 확신 할 수 없다. – immulatin

+0

나중을 위해 소계와 합계를 남겨 둘 수 있습니다. 아프다 질문 편집 – Tuzki

관련 문제