How to convert xml to excel file programmatically

后端 未结 8 709
旧时难觅i
旧时难觅i 2020-12-06 15:20

I have a xml document holding a small data for my project where I want to convert my xml to an excel file (microsoft office excel 2003 and over)

How can I do this pr

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-06 15:29

    1.Fill the xml file into dataset,
    2.convert dataset to excel by below method in asp.net

    These are very simple methods.

      public static void Convert(System.Data.DataSet ds, System.Web.HttpResponse response)
        {
            //first let's clean up the response.object
            response.Clear();
            response.Charset = "";
            //set the response mime type for excel
            response.ContentType = "application/vnd.ms-excel";
            //create a string writer
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
    
    
            //create an htmltextwriter which uses the stringwriter
            System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
            //instantiate a datagrid
            System.Web.UI.WebControls.DataGrid dg = new System.Web.UI.WebControls.DataGrid();
            //set the datagrid datasource to the dataset passed in
            dg.DataSource = ds.Tables[0];
            //bind the datagrid
            dg.DataBind();
    
    
            //tell the datagrid to render itself to our htmltextwriter
            dg.RenderControl(htmlWrite);
            //all that's left is to output the html
            response.Write(stringWrite.ToString());
            response.End();
        }
    

提交回复
热议问题