Domanda

I'm hoping someone can help me here. I'm trying to use phpxls to create an excel sheet. The thing is, I need it to save on the server so it can be emailed, not saved on the user's pc.

I've tried removing header("Content-Disposition: attachment; filename=\"$filename\""); from the send function, but it continues to do try and save it on the pc. I've tried using file_put_contents inside send, but it just saves the filename inside the file instead of the contents.

I'ved looked at other ways of exporting the database query, but I really don't have time to learn a new library. I have downloaded phpexcel, but its rather complicated for me to learn right now. Below is the code i'm using.

function send($filename) {
   header("Content-type: application/vnd.ms-excel");
   header("Content-Disposition: attachment; filename=\"$filename\"");
   header("Expires: 0");
   header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
   header("Pragma: public");
}

And this is the spreadsheet builder

<?php
    require 'config.php';

    $qry = mysql_query("SELECT * FROM itunes_marketing WHERE packageid = '$_POST[paid]' ORDER BY upc");
    $endrow = mysql_num_rows($qry)+2;
    $middle = $endrow+4;
    $end = $middle+2;
    $i = 0;
    define('PATH', '../../admin/itunes_marketing/');

    chdir('../../phpxls');
    require_once 'Writer_save.php';

    $workbook = new Spreadsheet_Excel_Writer();

    //header format
    $format_header =& $workbook->addFormat();
    $format_header->setBold();
    $format_header->setBgcolor('grey');
    $format_header->setFontFamily('Verdana');
    $format_header->setSize(20);
    $format_header->setBorder(2);

    // Title Format
    $format_title =& $workbook->addFormat();
    $format_title->setBold();
    $format_title->setColor('grey');
    $format_title->setFontFamily('Verdana');
    $format_title->setSize(10);
    $format_title->setAlign('center');
    $format_title->setBorder(2);

    // Add bold format
    $format_regular =& $workbook->addFormat();
    $format_regular->setColor('black');
    $format_regular->setBottom(2);
    $format_regular->setFontFamily('Arial');
    $format_regular->setSize(10);

    // worksheet for data?
    $worksheet =& $workbook->addWorksheet();
    // set col width?
    $worksheet->setColumn(0, 0, 10.38);
    $worksheet->setColumn(1, 1, 13);
    $worksheet->setColumn(2, 3, 13.75);
    $worksheet->setColumn(4, 4, 19.63);
    $worksheet->setColumn(5, 5, 18);
    $worksheet->setColumn(6, 6, 10);
    $worksheet->setColumn(7, 7, 8.63);
    $worksheet->setColumn(8, 8, 13.50);
    $worksheet->setColumn(9, 9, 10.50);
    $worksheet->setColumn(10, 10, 9.63);
    $worksheet->setColumn(11, 11, 7.63);
    $worksheet->setColumn(12, 12, 8.63);
    $worksheet->setColumn(13, 13, 11.75);
    $worksheet->setColumn(14, 15, 13.13);
    $worksheet->setColumn(16, 16, 17.38);
    $worksheet->setColumn(17, 17, 16.50);
    $worksheet->setColumn(18, 18, 23.63);
    $worksheet->setColumn(19, 19, 19.75);
    $worksheet->setColumn(20, 20, 46.75);

    // setRow($row, $height, $format = null, $hidden = false, $level = 0)
    $worksheet->setRow(0, 24.75);
    $worksheet->setRow(1, 34.50);
    $worksheet->setRow(3, 64.50);   

    //setMerge($first_row, $first_col, $last_row, $last_col)
    $worksheet->setMerge(0, 0, 0, 14);

    // the title
    $worksheet->write(0, 0, "iTunes Store - Label Priority List", $format_header);
    // add data
    $worksheet->write(1, 0, "Defaults to Playlist if not specified", $format_regular);
    $worksheet->write(1, 6, "Choose from dropdown box", $format_regular);
    $worksheet->write(1, 7, "Choose from dropdown box", $format_regular);
    $worksheet->write(1, 8, "Choose from dropdown box", $format_regular);
    $worksheet->write(1, 9, "(MM/DD/YYYY)", $format_regular);
    $worksheet->write(1, 10, "Choose from dropdown box", $format_regular);
    $worksheet->write(1, 12, "Choose from dropdown box", $format_regular);
    $worksheet->write(1, 13, "*", $format_regular);
    $worksheet->write(1, 17, "**", $format_regular);

    $worksheet->write(2, 0, "CONTENT TYPE", $format_title);
    $worksheet->write(2, 1, "LABEL NAME", $format_title);
    $worksheet->write(2, 2, "UPC/EAN", $format_title);
    $worksheet->write(2, 3, "ARTIST NAME", $format_title);
    $worksheet->write(2, 4, "CONTENT TITLE", $format_title);
    $worksheet->write(2, 5, "MEDIA TYPE", $format_title);
    $worksheet->write(2, 6, "GENRE", $format_title);
    $worksheet->write(2, 7, "FORMAT", $format_title);
    $worksheet->write(2, 8, "STORE DATE", $format_title);
    $worksheet->write(2, 9, "TYPE", $format_title);
    $worksheet->write(2, 10, "INSTANT GRAT", $format_title);
    $worksheet->write(2, 11, "GROUP", $format_title);
    $worksheet->write(2, 12, "WEEK 1 NAT'L FORECAST", $format_title);
    $worksheet->write(2, 13, "PROJECTED  WEEK 1 iTUNES MARKET SHARE", $format_title);
    $worksheet->write(2, 14, "ITUNES WEEK 1 FORECAST", $format_title);
    $worksheet->write(2, 15, "CUMULATIVE NEW RELEASE SINGLE SALES", $format_title);
    $worksheet->write(2, 16, "SALES HISTORY", $format_title);
    $worksheet->write(2, 17, "DEVELOPING ARTIST COST", $format_title);
    $worksheet->write(2, 18, "DESCRIPTION", $format_title);
    $worksheet->write(2, 19, "LP/EXTRAS", $format_title);
    $worksheet->write(2, 20, "MARKETING DRIVERS", $format_title);

    while($rows = mysql_fetch_array($qry)) {
        $worksheet->write($i + 3, 0, $rows['content_type'], $format_regular);
        $worksheet->write($i + 3, 1, $rows['label_name'], $format_regular);
        $worksheet->write($i + 3, 2, $rows['upc'], $format_regular);
        $worksheet->write($i + 3, 3, $rows['artist_name'], $format_regular);
        $worksheet->write($i + 3, 4, $rows['content_title'], $format_regular);
        $worksheet->write($i + 3, 5, $rows['media_type'], $format_regular);
        $worksheet->write($i + 3, 6, $rows['genre'], $format_regular);
        $worksheet->write($i + 3, 7, $rows['format'], $format_regular);
        $worksheet->write($i + 3, 8, date('m/d/Y', strtotime($rows['store_date'])), $format_regular);
        $worksheet->write($i + 3, 9, $rows['type'], $format_regular);
        $worksheet->write($i + 3, 10, $rows['instant_grat'], $format_regular);
        $worksheet->write($i + 3, 11, $rows['groups'], $format_regular);
        $worksheet->write($i + 3, 12, $rows['week1_nat_forecast'], $format_regular);
        $worksheet->write($i + 3, 13, $rows['projected_wk1'], $format_regular);
        $worksheet->write($i + 3, 14, $rows['week1_forecast'], $format_regular);
        $worksheet->write($i + 3, 15, $rows['cumulative_release'], $format_regular);
        $worksheet->write($i + 3, 16, $rows['sales_history'], $format_regular);
        $worksheet->write($i + 3, 17, $rows['develop_cost'], $format_regular);
        $worksheet->write($i + 3, 18, $rows['description'], $format_regular);
        $worksheet->write($i + 3, 19, $rows['lp_extras'], $format_regular);
        $worksheet->write($i + 3, 20, $rows['marketing_drivers'], $format_regular);
        $i++;
    }
    // end of file stuff
    $worksheet->write($endrow, 0, "#end#", $format_title);
    $worksheet->write($middle, 0, "* Forecast & Market Share projections required for any title being pitched for featuring on Main/Music Main", $format_title);
    $worksheet->write($middle+1, 0, "** Sales History should include: First week iTunes sales and market share for last album, as well as LTD iTunes sales, market share, and release dates for the last two releases (when applicable)", $format_title);
    $worksheet->write($end, 0, "In order for your priority titles to be considered for featuring, it is essential that you submit this grid with listening copies (CD's or MP3's).                                  ", $format_title);

    $subdate = date('M').date('Y_d-m');
    //$filename = 'HitlistSubmission_'.$subdate.'.xls';
    //file_put_contents($filename, ob_get_clean());
    $workbook->send('HitlistSubmission_'.$subdate.'.xls');
    $workbook->close();
?>

Any help would be greatly appreciated!!

È stato utile?

Soluzione

If you specify a path and filename in the line where you are creating the spreadsheet, it will save the file to that location.

Current line:

$workbook = new Spreadsheet_Excel_Writer();

Change to:

$workbook = new Spreadsheet_Excel_Writer("HitlistSubmission_".$subdate.".xls");

I would also eliminate the following line:

$workbook->send('HitlistSubmission_'.$subdate.'.xls');

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top