通过javascript

时间:2015-05-25 08:47:43

标签: javascript php html

我的问题是图片!!!! 我将此表格html导出到excel,但图像不会导出到表格中。

   <table class="order-table table sortable" id="tableadmin">
    <thead>
        <tr>
            <th style="width:8px; min-width:8px; max-width:8px;">ID</th>
            <th>Télépro</th>
            <th>Date RDV</th>
            <th>Heure RDV</th>
            <th>Fiche</th>
            <th style="width:170px; min-width:170px; max-width:170px;">Compterendu</th>
            <th style="width:250px; min-width:250px;">Commentaire commercial</th>
        </tr>
    </thead>
    <tbody id="changetable" class="new">
        <tr>
            <td>
                <?php echo $id ?>
            </td>
            <td>
                <?php echo $login ?>
            </td>
            <td>
                <?php echo $date ?>
            </td>
            <td>
                <?php echo $heure ?>
            </td>
            <td><img src="../img/fiche.png" />
            </td>
            <td>
                <?php echo $compterendu ?>
            </td>
            <td>
                <?php echo $comment ?>
            </td>
        </tr>
    </tbody>
</table>
我的问题是图片!!!!

2 个答案:

答案 0 :(得分:4)

使用phpexcel类将图像写入Excel工作表是一个很棒的功能,使用这种方法我们可以在excel列中绘制图像,看起来很漂亮,可以获得带有描述的图像。

在使用导入/导出系统时,我注意到PHPExcel的这个功能非常好用,好了,让我们检查使用PHPExcel将图像写入excel表的代码。

include 'PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set properties
$objPHPExcel->getProperties()->setCreator("Jobin Jose");
$objPHPExcel->getProperties()->setLastModifiedBy("Jobin Jose");
$objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHPExcel classes.");
// Add some data
// echo date('H:i:s') . " Add some data\n";
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('B2', 'world!');
//$objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('D2', 'world!');
$objPHPExcel->getActiveSheet()->setTitle('Simple');
$gdImage = imagecreatefromjpeg('uploads/t12.jpg');
// Add a drawing to the worksheetecho date('H:i:s') . " Add a drawing to the worksheet\n";
$objDrawing = new PHPExcel_Worksheet_MemoryDrawing();
$objDrawing->setName('Sample image');
$objDrawing->setDescription('Sample image');
$objDrawing->setImageResource($gdImage);
$objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG);
$objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
$objDrawing->setHeight(150);
$objDrawing->setCoordinates('C1');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
// Echo done
echo date('H:i:s') . " Done writing file.\r\n";
The out put will be like as follows.

使用PHPExcel将图像写入Excel enter image description here

上面的代码将创建一个“xlsx”格式的文件,因为它使用2007 excel类如果你想要“xls”格式只是尝试使用2005类,不要在使用2005时将文件格式更改为“xls”。 / p>

答案 1 :(得分:0)

这是我制作的一个函数,它是你指定的Javascript。

在您不希望在Excel中显示的元素上添加“删除”类。

function exportExcel(id,name){ //<table> id and filename
    var today = new Date();
    var date = ('0'+today.getDate()).slice(-2)+"-"+('0'+(today.getMonth()+1)).slice(-2)+"-"+today.getFullYear();

    var file_name = name+"_"+date+".xls"; //filename with current date, change if needed
    var meta = '<meta http-equiv="content-type" content="text/html; charset=UTF-8" />';
    var html = $("#"+id).clone();

    html.find('.remove').remove(); //add the 'remove' class on elements you do not want to show in the excel
    html.find('a').each(function() { //remove links, leave text only
        var txt = $(this).text();
        $(this).after(txt).remove();
    });
    html.find('input, textarea').each(function() { //replace inputs for their respectives texts
        var txt = $(this).val().replace(/\r\n|\r|\n/g,"<br>");
        $(this).after(txt).remove();
    });
    html.find('select').each(function() { //replace selects for their selected option text
        var txt = $(this).find('option:selected').text();
        $(this).after(txt).remove();
    });
    html.find('br').attr('style', "mso-data-placement:same-cell"); //make line breaks show in single cell
    html = "<table>"+html.html()+"</table>";

    var uri = 'data:application/vnd.ms-excel,'+encodeURIComponent(meta+html);
    var a = $("<a>", {href: uri, download: file_name});
    $(a)[0].click();
}

在事件上调用它,例如:

$("#export_button").click(function(e){
    exportExcel("table_id", "filename");
});