Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6115009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:02:31+00:00 2026-05-23T15:02:31+00:00

I need a way to write an excel file with format through Pure PHP.

  • 0

I need a way to write an excel file with format through Pure PHP.
WARNING:Please, this Q not about the libs, plugins and so on- do not disturb.
I’ve googled a lot and didn`t find the Answer, but in some article on StackOverflow, some guy mentioned the HTML/CSS format way
HTML format solutions that didn’t work
and I’ve tried to do this, but in file which was saved columns where filled with tags and nothing happens.
May be there are some professionals who can help me with pure code writing an Excel through PHP, HTML, CSS or something.

And again with example, I’ve already used concept like this wich didn’t help:

    // headers for excel
$headers .= "<div style='font:bold 14px Times;'>Журнал остатков и инкассаций</div>\n";
$headers .= "Начало периода\t ".date('d.m.Y')."0:00 \n";
$headers .= "Начало периода\t ".date('d.m.Y')."23:59 \n\n";

$headers .= "Терминал\t";
$headers .= "Место расположения\t";
$headers .= "Дата\t";
$headers .= "Время\t";
$headers .= "Сумма инкассации\t";
$headers .= "Банкноты\t";

// excel content with overloaded terminals
if (mssql_num_rows($resCollection)>0) {
  while ($rowCollection = mssql_fetch_object($resCollection)) {

    $data .= $rowCollection->Name."\t".$rowCollection->Address."\t"
            .$rowCollection->TerminalNotes."\t".$rowCollection->TerminalAmount
            ."\t".$rowCollection->DayAmountAll."\t"
            .$rowCollection->LastPaymentTime."\n";

  }
}

$fileName = 'collection'.date('d_m_Y_H_i_s').'.xls';


header("Content-type: application/vnd.ms-excel");// application/excel had also been used
header("Content-Disposition: attachment; filename=$fileName");
echo iconv('utf-8', 'cp1251', "$headers\n$data"); 

I’ve used not only divs and tables too – it didn’t work though.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:02:31+00:00Added an answer on May 23, 2026 at 3:02 pm

    The following example would probably help you:

      1 <?php
      2 header('Content-type: application/excel');                                  
      3 header('Content-Disposition: attachment; filename="test.xls"');
      4 ?>
      5 
      6 <table>
      7 <tr><th>Column 1</th><th>Column 2</th></tr>
      8 <tr><td style="font-size:200%">Answer 1</td><td style="color:#f00">Answer 2<    /td></tr>
      9 <tr><td colspan="2" style="font-weight:bold">Answer 3 with 2 columns</td></t    r>
     10 </table>
    

    Save this as a .php file on your server.

    What it does is: add headers to force the browser thing you’re delivering an Excel file. And then returning a table inside of it. My Excel picks this up perfectly.

    Note: the sample HTML comes from the answer that you had already found. I have just added the headers to the beginning.

    Note: You just changed your question and added an example. Your example does NOT use html! You are delivering a TAB-delimited file where each line ends with a newline.

    The proper way to do this is to really generate HTML, ie, use a <table> at the beginning, use <tr> and </tr> for each row, and put the fields in <td> tags, and then finish with the closing </table>, so basically:

    $data='<table>';
    // excel content with overloaded terminals
    if (mssql_num_rows($resCollection)>0) {
      while ($rowCollection = mssql_fetch_object($resCollection)) {
        $data.='<tr>';
        $data .= '<td>'.$rowCollection->Name."</td><td>".$rowCollection->Address."</td><td>"
                .$rowCollection->TerminalNotes."</td><td>".$rowCollection->TerminalAmount
                ."</td><td>".$rowCollection->DayAmountAll."</td><td>"
                .$rowCollection->LastPaymentTime."</td>";
        $data.='</tr>';
    
      }
    }
    $data.='</table>';
    

    Then change your last line to:

    echo iconv('utf-8', 'cp1251', "$data"); 
    

    If that works I suggest you change the output to have <th> tags around your table headers, instead of the div that is in your example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.