Export dataset to excel and dataset to excel
I found a lot of such code on the Internet. If it weren't for Chinese garbled characters, it would be too complicated. This is the best one I have ever used.
// Ds is the data source, filename is the saved file name publicvoidCreateExcel (DataSet ds, stringFileName) {// sets the information HttpContext. current. response. clear (); HttpContext. current. response. contentType = "application/vnd. ms-excel "; HttpContext. current. response. contentEncoding = System. text. encoding. getEncoding ("GB2312"); HttpContext. current. response. charset = "GB2312"; // save the file name HttpContext. current. response. addHeader ("content-disposition", "attachment; filename =" + HttpUtility. urlEncode (FileName); // Save the excel file stream StringWriter stringWrite = newStringWriter (); HtmlTextWriter htmlWrite = newHtmlTextWriter (stringWrite); // dsDataGrid dg = newDataGrid (); dg. dataSource = ds; dg. dataBind (); dg. renderControl (htmlWrite); // output data HttpContext. current. response. write ("<meta http-equiv = \" content-type \ "content = \" application/ms-excel; charset = gb2312 \ "/>" + stringWrite. toString (); HttpContext. current. response. end ();}