Reprinted from: Http://www.cnblogs.com/jbps/p/3549671.html?utm_source=tuicool&utm_medium=referral
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7 using Aspose.cells; 8 using System.Text; 9 using system.data;10 one namespace Oceansoft.Net.CeSWeb.Module. Export Excel_ via template _12 {public partial class Daochu:syst Em. WEB.UI.PAGE14 {protected void Page_Load (object sender, EventArgs e) 16 {17 18}19// /<summary>20///Click export///</SUMMARY>22//<param name= "Sender" ></param> ;//<param name= "E" ></param>24 public void Btndaochu_serverclick (object sender, EventArgs E) {A string file = Server.MapPath ("~/excel/zwxxtj.xls"); the DataSet ds = new DataSet (); The datatable dtout = new DataTable (); Dtout. Columns.Add ("unit", typeof (String)); Dtout. Columns.Add ("Quantity", typeof (String)); Dtout. Columns.Add ("Score", typeof (String)); Dtout. Columns.Add ("Look", typeof (String)), list<datatable> lst = null;35 for (int i = 0; i < 3; i++) (PNS) = new list<datatable> (); a. DataRow dr = Dtout. NewRow (); dr["unit"] = "AAAA" + i;41 dr["Quantity"] = "BBBB" + i;42 dr["score"] = " CCCC "+ i;43 dr[" See "] =" DDDD "+ i;44 dtout. Rows.Add (DR);}47 ds. Tables.add (dtout); Add (ds. Tables[0]); Exportexcelmodel (this. Response, LST, file, 4);//Fill data from line fourth}54-public void Exportexcelmodel (HttpResponse res, list<syste M.data.datatable> Datas, string exceltemplatepath, int firstrow)-The path to the Excel template is 58 Workbookdesigner designer = new Workbookdesigner (); Open (Exceltemplatepath); 60 61 Worksheet sheet = Designer. workbook.worksheets[0];62 sheet. Cells.importdatatable (Datas[0], false, FirstRow, 0, true); var C11 = sheet. Cells[0, 0];//the first row of the first column of C11. Putvalue ("I am the title, everyone Happy New Year ... "); saveoptions s = new xlssaveoptions (saveformat.excel97to2003); string str =" "; 70 71 str = Httputility.urlencode ("Test. xls", Encoding.UTF8). ToString (); Workbook.save (res, str, contentdisposition.attachment, s); 75 76}77}78}
Net custom Excel template Export data