The advantage of using Excel to print a template is that the format is easy to adjust, which requires a high degree of consistency in the client system configuration, such as an Excel installation version, where Excel templates on the server must be consistent with the client Excel version and cannot be modified with other versions of Excel.
JavaScript Print Excel code
<script type= "Text/javascript" >functionPrint_click () {alert ("Print Test"); varTemplate = "Http://172.16.18.217/ExcelDoc/CreateCard.xls"; varxlapp =NewActiveXObject ("Excel.Application"); varXlbook =XlApp.Workbooks.add (Template); varXlsheet =Xlbook.activesheet; Xlsheet.printout; Xlapp=NULL; Xlsheet=NULL; }</script>
Asp. NET in the background call JavaScript print Excel code
ProtectedvoidPrinttestbutton_click (object sender, EventArgs e) {string strscript; Strscript= ("<script type= ' Text/javascript ' >"); Strscript+ = ("alert (\" Print test \ ");"); Strscript+ = ("var Template = ' Http://172.16.18.217/ExcelDoc/CreateCard.xls ';")); Strscript+ = ("var xlapp = new ActiveXObject (' Excel.Application ');"); Strscript+ = ("if (xlapp== undefined) {"); Strscript+ = ("alert (\" Excel object creation failed \ ");} else{"); Strscript+ = ("var xlbook = XlApp.Workbooks.add (Template);"); Strscript+ = ("var xlsheet = Xlbook.activesheet;")); Strscript+ = ("xlsheet.printout;"); Strscript+ = ("xlapp = null;"); Strscript+ = ("xlsheet = null;}"); Strscript+ = ("</script>"); This. Clientscript.registerstartupscript ( This. GetType (), "", strscript);}
C # Printing
Public intPrintpatientinfo () {printexcel pexcel=NewPrintexcel ("Createcard.xls"); Worksheet sheet=Pexcel.getworksheet (); Sheet. cells[1,1] ="Build Card information"; Sheet. cells[2,1] ="Patient Card number:"; Sheet. cells[2,2] =Patientcardnum.trim (); Sheet. cells[2,3] ="Patient Name:"; Sheet. cells[2,4] =Patientname.trim (); Sheet. cells[4,1] ="Amount"; Sheet. cells[4,2] ="1 USD"; Sheet. cells[4,3] ="Date"; Sheet. cells[4,4] = DateTime.Now.ToString ("YYYY-MM-DD"); Pexcel.execprint (1); return 0;}
Asp. NET on the server side with C # organization JavaScript printing code and printing in IE client, you can also specify the printer, the most convenient
Public classprintexceljs{Private stringExcelpath; Private stringStrscript; PublicPrintexceljs (stringXslpath) {Excelpath=Xslpath; Strscript="<script type= ' text/javascript ' >"; Strscript+="var Template = '"+excelpath+"';"; Strscript+="var xlapp = new ActiveXObject (' Excel.Application ');"; Strscript+="if (xlapp== undefined) {"; Strscript+="alert (\ "Excel object creation failed \");} else{"; Strscript+="var xlbook = XlApp.Workbooks.add (Template);"; Strscript+="var xlsheet = Xlbook.activesheet;"; } Public voidInsertvaluetoexcelcell (intIintJstringstrcontent) {Strscript+="Xlsheet.cells ("+ i.tostring () +","+ j.tostring () +")"; Strscript+="=\""+ Strcontent +"\";"; } Public voidExecprint (System.Web.UI.Page Page,intNumofprint=1) {Strscript+="xlsheet.printout (1,null,1,false,\ "printguid\", false,false);";//This code took me half a day, because before I heard that JavaScript could not specify the printer, in doubt, then finally found the correct usageStrscript + ="xlapp = null;"; Strscript+="xlsheet = null;}"; Strscript+="</script>"; Page. Clientscript.registerstartupscript ( This. GetType ()," ", Strscript); }} example code:Private voidPrinttestbutton_click () {stringExcelpath ="/ http"+request.url.host +@"/exceldoc/"+"Createcard.xls"; Printexceljs Pexcel=NewPrintexceljs (Excelpath); Pexcel.insertvaluetoexcelcell (1,1,"XX Information"); Pexcel.insertvaluetoexcelcell (2,1,"Patient Card number:"); Pexcel.insertvaluetoexcelcell (2,2, Patientcardnum.trim ()); Pexcel.insertvaluetoexcelcell (2,3,"Patient Name:"); Pexcel.insertvaluetoexcelcell (2,4, Patientname.trim ()); Pexcel.insertvaluetoexcelcell (4,1,"Amount"); Pexcel.insertvaluetoexcelcell (4,2,"1 USD"); Pexcel.insertvaluetoexcelcell (4,3,"Date"); Pexcel.insertvaluetoexcelcell (4,4, DateTime.Now.ToString ("YYYY-MM-DD")); Pexcel.execprint ( This,1);}
JS print with Excel as a template