C # operations Excel import and export method one, according to the project requirements, there are multiple methods for Excel operation, there are some class library programming design format, some JS based on the format of the table directly exported.
Now the direct download type is introduced:
Download directly to Excel (equivalent to copying the entire table directly into Excel) based on the HTML format of the table displayed in the page
The HTML format is as follows:
<div id="Ta"> <input type="Button" class="btn btn-primary Btn-mini"onclick="javascript:method1 (' Tableexcel ')"Value="Export Excel"> <table id="Tableexcel" class="Table Table-hover"> <thead> <tr> <th>Id</th> <th colspan="2">UserName</th> <th>Email</th> <th>address</th& Gt <th>Mobile</th> <th>CreateDate</th> <th> Operations < /th> </tr> </thead> <tbody>@foreach (varUinchviewbag.users) {<tr> <td>@u.UserId</td> <td>@u.username</ Td> <td>@u.UserName</td> <td>@u.email</td> ; <td>@u.Address</td> <td>@u.Mobile</td> <t D>@u.createdate</td> <td><a href="@Url. Action ("Delete", "Home") [email protected]"onclick="Javascript:return P_del ()">Delete</a></td> </tr> } </tbody> </table> </div>
Legend:
Export JS:
function Method1 (tableid) {//Copy the entire table to Excel//var len = $ ("Table tr"). Length; //if (len > 1) {//$ ("tr[id=" + (len-1) + "']"). Remove (); //} //$ (' table>th:last '). Hide (); //$ (' table>td:last '). Hide (); //var len = $ ("Table tr"). Length;$("Table TR Th:last-child"). Remove (); $("Table TR Td:last-child"). Remove (); if(GetExplorer () = ='IE') { varCURTBL =document.getElementById (TableID); varOXL =NewActiveXObject ("Excel.Application"); //Create an Ax object Excel varOWB =OXL.Workbooks.Add (); //Get Workbook Object varXlsheet = Owb.worksheets (1); //activating the current sheet varSEL =Document.body.createTextRange (); Sel.movetoelementtext (CURTBL); //Move the contents of the table into the TextRangeSel.Select(); //Select all the contents of TextRangeSel.execcommand ("Copy"); //copy content in TextRangeXlsheet. Paste (); //paste into active ExceloXL.Visible =true; //set Excel Visible Properties Try { varfname = OXL.Application.GetSaveAsFilename ("Excel.xls","Excel Spreadsheets (*.xls), *.xls"); } Catch(e) {print ("Nested Catch caught"+e); } finally{owb.saveas (fname); Owb.close (SaveChanges=false); //xls.visible = false;Oxl.quit (); OXL=NULL; //End Excel process, exit complete//Window.setinterval ("Cleanup ();", 1);IDTMR = Window.setinterval ("Cleanup ();",1); } } Else{tabletoexcel (tableid)}} function Cleanup () {window.clearinterval (IDTMR); CollectGarbage (); } varTabletoexcel =(function () {varURI ='Data:application/vnd.ms-excel;base64,', Template='', base64= function (s) {returnWindow.btoa (unescape (encodeURIComponent (s))}, Format=function (s, c) {returnS.replace (/{(\w+)}/g, function (m, p) {returnc[p];}) } returnfunction (table, name) {if(!table.nodetype) Table =document.getElementById (table)//$ (' table>th:last '). Hide (); //$ (' table>td:last '). Hide (); varCTX = {Worksheet:name | |'Worksheet', Table:table.innerHTML} window.location.href= URI +base64 (Format (template, CTX)) parent.$ ("#ta"). Load (window.location.href +"#ta"); Parent.subwindow_add. Close (); } })()
If the export has garbled problems:
Solution:
Add to
<meta name= "viewport" content= "Width=device-width"/>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<meta charset= "Utf-8"/>
<meta name= "Generator" content= "EditPlus" >
<meta name= "Author" content= "" >
<meta name= "Keywords" content= "" >
<meta name= "Description" content= "" >
Exported Excel:
The data used here is read in the database, if the implementation can modify the HTML itself, add data, self-test.
The source code is in the next section!!!!
C # import and export Excel (i)