export of webform

    Uh, how do I say this is written by the boss of the company, I directly call it and it is not the same as what I wrote on Baidu before. I feel very nice. Maybe I have to come back for reference later. The template is what the data you want to export should look like in Excal. For example, I need to export it now 

ok Then find a place to put it and then write a configuration file ExportConfig.xml The contents are as follows

Then an ExcalHelper helper class

    public  static  class ExcelHelper
    { 
     //The three parameters here are a data result set DataSet, the name of the configuration file template you just wrote, and then the name of a exported Excal
public static void ExprortExcel(DataSet set , string typeName, string fileName) { string outputFile = HttpContext.Current.Server.MapPath(string.Format("~/Temp/Temp/{0}", fileName)); var tempDirectory = HttpContext.Current.Server.MapPath("~/Temp/Temp/"); var files = Directory.GetFiles(tempDirectory); try { foreach (string item in files) { File.Delete(item); } } catch (Exception) { } ExportMain.GetProductDescription(); set.DataSetName = typeName; ExportMain.Export(set, outputFile); ToClient(outputFile); } public static void ToClient(string filePath) { try { FileInfo info = new FileInfo(filePath); System.Web.HttpContext.Current.Response.Clear(); System.Web.HttpContext.Current.Response.Charset = "UTF-8"; System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; // Add header information to specify the default file name for the "File Download/Save As" dialog System.Web.HttpContext.Current.Response. AddHeader( " Content-Disposition " , " attachment; filename= " + HttpUtility.UrlEncode(info.Name, System.Text.Encoding.UTF8)); // Add header information, specify the file size, so that the browser can display the download progress System.Web.HttpContext.Current.Response.AddHeader( " Content-Length " , info.Length.ToString()); // Specifies that the returned stream is a stream that cannot be read by the client and must be downloaded System.Web.HttpContext.Current.Response.ContentType = " application/ms-excel " ; // Send the file stream to the client System.Web.HttpContext.Current.Response.WriteFile(info.FullName); // File. Delete(file.FullName); // Stop the execution of the page HttpContext.Current.Response.End(); } catch (Exception ex) { throw ex; } finally { if (File.Exists(filePath)) { File.Delete(filePath); } } } }
//Call
//Create a DataSet
  DataSet set = new DataSet();
//Create a DataTable
  DataTable table = new DataTable("Body");//The name here is the name of the source in your configuration file
  //After Create columns for this Table
  

   table.Columns.Add("Year", typeof(int));//Year
     table.Columns.Add("ProvinceName", typeof(string));//Province
     table.Columns.Add("CourseName", typeof( string));//
     Artistic table.Columns.Add("MinScore", typeof(int));//Score start
     table.Columns.Add("MaxScore", typeof(int));//Score cutoff
     table.Columns .Add("SameNumber", typeof(int));//Number of people with the same score
     table.Columns.Add("LowestRank", typeof(int));//Lowest rank
     table.Columns.Add("HighestRank", typeof (int));//Highest order
     table.Columns.Add("istrue", typeof(string));//Is the value correct

   //Append data in a loop as you like 
table.Rows.Add(Column 1, Column 2, Column 3, Column 4, Column 5, Column 6, Column 7, Column 8, Column 9);
//Then put this table into the DataSet Medium
set.Tables.Add(table);
//Finally call the ExcalHelper helper class to pass in the required parameters and it's OK
ExcalHelper(set,"OneParagraph","Excal file name to export");

will add more worksheets or Thank you for reading the cross-line header

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325209588&siteId=291194637