Asp.net excel export function, asp. netexcel
The following are my Excel export functions in project development. I hope you can correct them and learn from each other.
Protected void btn_Export_Click (object sender, EventArgs e) {string FileID = ""; if (Request. QueryString ["fujiian"]! = Null & Request. QueryString ["fujitsu"]. ToString ()! = "") {FileID = Request. queryString ["fujiian"]. toString () ;}string title = GetFileTile (FileID); // file title Workbook workbook = new Workbook (); // workbook Worksheet sheet = Workbook. worksheets [0]; // worksheet Cells = sheet. cells; // cell // column width cells. setColumnWidth (0, 20.00); cells. setColumnWidth (1, 30.00); cells. setColumnWidth (2, 30.00); cells. merge (0, 0, 1, 3); // Merge cells [0, 0]. putValue (title + "/List of persons who have learned Files"); cells [0, 1]. putValue (""); cells [0, 2]. putValue (""); cells [1, 0]. putValue ("Serial Number"); cells [1, 1]. putValue ("name"); cells [1, 2]. putValue ("time"); string SQL = "order by StudyTime"; ds = PublishBLL. getCommentCount (FileID, SQL); if (ds. tables [0]. rows. count> 0) {for (int I = 1; I <ds. tables [0]. rows. count + 1; I ++) {// Aspose. cells. style styleTitle = workbook. styles [workbook. styles. add ()]; // Add a style // styleTitle. horizontalAlignment = TextAlignmentType. center; // text Center // styleTitle. font. name = ""; // text font // styleTitle. font. size = 18; // text Size // styleTitle. font. isBold = true; // bold cells [I + 1, 0]. putValue ("" + I. toString (). padLeft (3, '0') + ""); cells [I + 1, 1]. putValue ("" + ds. tables [0]. rows [I-1] ["StudyName"]. toString () + ""); cells [I + 1, 2]. putValue ("" + ds. tables [0]. rows [I-1] ["StudyTime"]. toString () + "") ;}} string filename = "Statistics" + DateTime. now. toString ("yyyyMMddHHmmss") + ". xls "; string path = ConfigurationManager. deleetask[ "StudyFile"] + @ "\" + filename + ""; workbook. save (path); // Save it to the hard disk # region Download System. IO. memoryStream ms1 = workbook. saveToStream (); // generate data stream byte [] bt1 = ms1.ToArray (); string fileName = "Statistics" + DateTime. now. toString ("yyyyMMddHHmmss") + ". xls "; // file name saved by the client // download the file Response in the form of a Response stream. contentType = "application/vnd. ms-excel "; // notify the browser to download the file instead of opening Response. addHeader ("Content-Disposition", "attachment; filename =" + HttpUtility. urlEncode (fileName, System. text. encoding. UTF8); Response. binaryWrite (bt1); Response. flush (); Response. end (); # endregion}