简体   繁体   English

通过javascript导出表格html以使用文本和图像来表现出色

[英]export table html to excel with text and images by javascript

my problem is images !!!! 我的问题是图像! i export table this table html to excel but the images aren't exported in the table . 我将此表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> 
my problem is images !!!! 我的问题是图像!

Writing images to the excel sheet using phpexcel class is a great feature , using this method we can draw the images inside the excel column it looks good and nice to have images with some descriptions. 使用phpexcel类将图像写入excel工作表是一个很棒的功能,使用此方法,我们可以在excel列内绘制图像,具有描述的图像看起来很好。

While working on an Import/Export system I have noticed this facility of PHPExcel its really good and easy, Ok lets check the code for writing images to the excel sheet using PHPExcel. 在导入/导出系统上工作时,我注意到PHPExcel的这种工具确实非常好用和简单,Ok让我们检查一下使用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.

Writing Images to Excel Using PHPExcel 使用PHPExcel将图像写入Excel 在此处输入图片说明

The above code will create an “xlsx” formatted file because it uses 2007 excel classes If you want “xls” format just try with 2005 class do not for get to change the file format to “xls” while using 2005. 上面的代码将创建一个“ xlsx”格式的文件,因为它使用2007 excel类。如果要“ xls”格式,请仅尝试使用2005类,不要在使用2005时将文件格式更改为“ xls”。

Here is a function I made, it's Javascript as you specified. 这是我做的一个功能,它是您指定的Javascript。

Add "remove" class on elements you do not want to show in the excel. 在不想显示在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();
}

Call it on an event, example: 在事件上调用它,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM