Create Excel workbook in asp.net

I need to create an excel file for the user when a button is clicked. I used Netoffice which worked great for desktop applications. But now I want to do the same with an asp.net application. So my server code doesn't have access to the client copy of excel. Which approach should I take?

+3


source to share


5 answers


Use EPPlus . It allows you to create Excel spreadsheets on the server. I used it and it worked great. It supports advanced features.



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. Print the column names on row 1
    ws.Cells["A1"].LoadFromDataTable(tbl, true);

    //Format the header for column 1-3
    using (ExcelRange rng = ws.Cells["A1:C1"])
    {
        rng.Style.Font.Bold = true;
        rng.Style.Fill.PatternType = ExcelFillStyle.Solid;

        //Set Pattern for the background to Solid
        rng.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(79, 129, 189));

        //Set color to dark blue
        rng.Style.Font.Color.SetColor(Color.White);
    }

    //Example how to Format Column 1 as numeric 
    using (ExcelRange col = ws.Cells[2, 1, 2 + tbl.Rows.Count, 1])
    {
        col.Style.Numberformat.Format = "#,##0.00";
        col.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
    }

    //Write it back to the client
    Response.Clear();
    Response.AddHeader("content-disposition", "attachment;  filename=file.xlsx");
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";                    
    Response.BinaryWrite(pck.GetAsByteArray());
    Response.End();
}

      

+7


source


The most flexible and most likely it will do what you need, it will take some work, but it's free - and it really works. Use the toolkit to view existing documents to learn how to create the functions you want.



Open XML 2.0 SDK

+3


source


You can try a simple HTML table (inlcude html, head and body tags). Just save it with the XLS extension.


enter image description here

0


source


Netoffice requires MS Office to run the machine. Do you have a server?

0


source


You can use DataGrid to create Excel files on the fly. It doesn't require Excel.

public static void ExportDataSetToExcel(DataSet ds, string filename)
{
    HttpResponse response = HttpContext.Current.Response;

    // first let clean up the response.object
    response.Clear();
    response.Charset = "";

    // set the response mime type for excel
    response.ContentType = "application/vnd.ms-excel";
    response.AddHeader(
        "Content-Disposition",
        "attachment;filename=\"" + filename + "\""
    );

   // create a string writer
   using (StringWriter sw = new StringWriter())
   {
       using (HtmlTextWriter htw = new HtmlTextWriter(sw))
       {
            // instantiate a datagrid
            DataGrid dg = new DataGrid();
            dg.DataSource = ds.Tables[0];
            dg.DataBind();
            dg.RenderControl(htw);
            response.Write(sw.ToString());
            dg.Dispose();
            ds.Dispose();
            response.End();
       }
    }
}

      

0


source







All Articles