简体   繁体   中英

HTML to Excel / Insert Comments

Is possible to insert a comment on a cell of Excel from an HTML file? Couldn't find information from the internet.

THis is my code an is actually exporting the html table as an excel table perfectly, but I would like to export also comments, no success yet. My actual code:

 <!DOCTYPE html> <html> <head> <title>HTML Form Data to Excel</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> <script type="text/javascript"> function fnExcelReport() { $("th:hidden,td:hidden").remove(); var tab_text = '<html xmlns:x="urn:schemas-microsoft-com:office:excel">'; tab_text = tab_text + '<head><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>'; tab_text = tab_text + '<x:Name>Test Sheet</x:Name>'; tab_text = tab_text + '<x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet>'; tab_text = tab_text + '</x:ExcelWorksheets></x:ExcelWorkbook></xml></head><body>'; tab_text = tab_text + "<table border='1px'>"; tab_text = tab_text + $('#myTable').html(); tab_text = tab_text + '</table></body></html>'; var data_type = 'data:application/vnd.ms-excel'; var ua = window.navigator.userAgent; var msie = ua.indexOf("MSIE "); if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\\:11\\./)) { if (window.navigator.msSaveBlob) { var blob = new Blob([tab_text], { type: "application/csv;charset=utf-8;" }); navigator.msSaveBlob(blob, 'Report.xls'); } } else { $('#test').attr('href', data_type + ', ' + encodeURIComponent(tab_text)); $('#test').attr('download', 'Test file.xls'); } }; </script> <link rel="stylesheet" href="styles/main.css"/> </head> <body> <a href="#" id="test" onClick="fnExcelReport();">download</a> <table id="myTable"> <tr> <td><b>Name</b> </td> <td><b>Age</b> </td> </tr> <tr> <td style="background-color: red;">Tester1</td> <td>30</td> </tr> <tr> <td>Tester2</td> <td>29</td> </tr> <tr> <td>Tester3</td> <td>17</td> <td id="comentA" class="commentA">something</td> </tr> </table> </body> </html>

After you exported to Excel you could use Excel JS API to insert the comments:

await Excel.run(async (context) => {
    const sheet = context.workbook.worksheets.getItem("Comments");

    // Note that an InvalidArgument error will be thrown if multiple cells passed to `comment.add`.
    sheet.comments.add("A2", "TODO: add data.");
    await context.sync();
});

The document can be found at https://docs.microsoft.com/en-us/office/dev/add-ins/excel/excel-add-ins-comments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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