Domanda

I have to export some data to excel.I have done the exporting data, which get from a sql query and bind them to a grid view and after that exporting that grid view to excel. It works fine.

But the issue is when there are some additional information to display in the excel sheet just like below (Report date , style name), How can I add them to the top of the excel sheet before my data list.

Sample Format

Are there any libraries available to do this.. Plzz help...

And this is a ASP.NET application.

È stato utile?

Soluzione

Take a look at Epplus library.

You can easily bind resultset returned by SQL query to worksheet and insert new row at desired position.

private void DumpExcel(DataTable tbl)
{
   using (ExcelPackage pck = new ExcelPackage())
   {
     //Create the worksheet
     ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Demo");


     //Load the datatable into the sheet, starting from cell A1.
     ws.Cells["A1"].LoadFromDataTable(tbl, true);
     ExcelWorksheet.InsertRow(int rowFrom, int rows, intCopyStylesFromRow);

     Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
     Response.AddHeader("content-disposition", "attachment;  filename=ExcelDemo.xlsx");
     Response.BinaryWrite(pck.GetAsByteArray());
    }
 }

Altri suggerimenti

There are several libraries to do this. I have tried this one npoi dll

You can go through this extensive discussion on libraries for writing excel using C#.Write to excel using C#

protected void bt_Express_Click(object sender, EventArgs e)
    {
        string ex = ".xls";
        string FName = "Danh_Sach_Hoc_Sinh";
        FName += ex;
        HtmlForm form = new HtmlForm();
        Literal header = new Literal();
        Response.Clear();
        Response.AddHeader("content-disposition", "attachment; filename=" + FName);
        Response.Charset = "UTF-8";
        Response.ContentType = "application/vnd.ms-excel";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        header.Text = "<table><tr><td colspan='6'>Trường: " + Session["TenTruong"].ToString() + "</td><td colspan='2'>Năm học: " + Session["TenNamHoc"].ToString() + "</td></tr> " +
            "<tr><td ALIGN='CENTER' colspan='8'>DANH SÁCH HỌC SINH LỚP.</td></tr>" +
            "<tr><td colspan='8'>" + ((cb_Khoi.SelectedValue == "" && cb_Lop.SelectedValue == "") ? "Toàn trường" : ((cb_Khoi.SelectedValue != "" && cb_Lop.SelectedValue == "") ? "Toàn khối " + cb_Khoi.SelectedValue : "Lớp: " + cb_Lop.SelectedItem.Text + " - Giáo viên: " + db.Rows[0]["GiaoVien"].ToString())) + "</td></tr>" +
            "</table>";

        form.Attributes["runat"] = "server";
        form.Controls.Add(header);
        form.Controls.Add(gv_dshocsinh);
        Controls.Add(form);
        form.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top