using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OfficeOpenXml;
using System.Drawing;
using OfficeOpenXml.Style;
using System.Data;
namespace WebApplication5
{
public partial class SampleExcel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("EmpNo");
dt.Columns.Add("EmpName");
dt.Columns.Add("EmpDept");
dt.Rows.Add("111", "aaa", "bbb");
dt.Rows.Add("222", "aaa", "bbb");
dt.Rows.Add("333", "aaa", "bbb");
dt.Rows.Add("444", "aaa", "bbb");
dt.Rows.Add("555", "aaa", "bbb");
dt.AcceptChanges();
DumpExcel(dt);
}
private void DumpExcel(DataTable tbl)
{
try
{
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.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
//Response.AddHeader("content-disposition", "attachment; filename=ExcelDemo.xlsx");
//Response.BinaryWrite(pck.GetAsByteArray());
pck.Workbook.Properties.Title = "Excel Export";
pck.Workbook.Properties.Author = "Me";
pck.Workbook.Properties.Comments = "Sample Record Details";
pck.Workbook.Properties.Company = "ABC Tech";
byte[] fileBytes = pck.GetAsByteArray();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Cookies.Clear();
//Add the header & other information
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Private);
HttpContext.Current.Response.CacheControl = "private";
HttpContext.Current.Response.Charset = System.Text.UTF8Encoding.UTF8.WebName;
HttpContext.Current.Response.ContentEncoding = System.Text.UTF8Encoding.UTF8;
HttpContext.Current.Response.AppendHeader("Content-Length", fileBytes.Length.ToString());
HttpContext.Current.Response.AppendHeader("Pragma", "cache");
HttpContext.Current.Response.AppendHeader("Expires", "60");
string DateTimes = DateTime.Now.TimeOfDay.ToString().Replace(":", "_").Replace(".", "_");
//Default Download Filename
string FileName = "ExportBiblio_" + DateTimes + ".xlsx";
HttpContext.Current.Response.AppendHeader("Content-Disposition",
"attachment; " +
"filename=\"" + FileName + "\"; " +
"size=" + fileBytes.Length.ToString() + "; " +
"creation-date=" + DateTime.Now.ToString("R") + "; " +
"modification-date=" + DateTime.Now.ToString("R") + "; " +
"read-date=" + DateTime.Now.ToString("R"));
HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.sampleDemo.sheet";
//Write it back to the client
HttpContext.Current.Response.BinaryWrite(fileBytes);
HttpContext.Current.Response.End();
}
}
catch (Exception ex)
{
ex.ToString();
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OfficeOpenXml;
using System.Drawing;
using OfficeOpenXml.Style;
using System.Data;
namespace WebApplication5
{
public partial class SampleExcel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("EmpNo");
dt.Columns.Add("EmpName");
dt.Columns.Add("EmpDept");
dt.Rows.Add("111", "aaa", "bbb");
dt.Rows.Add("222", "aaa", "bbb");
dt.Rows.Add("333", "aaa", "bbb");
dt.Rows.Add("444", "aaa", "bbb");
dt.Rows.Add("555", "aaa", "bbb");
dt.AcceptChanges();
DumpExcel(dt);
}
private void DumpExcel(DataTable tbl)
{
try
{
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.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
//Response.AddHeader("content-disposition", "attachment; filename=ExcelDemo.xlsx");
//Response.BinaryWrite(pck.GetAsByteArray());
pck.Workbook.Properties.Title = "Excel Export";
pck.Workbook.Properties.Author = "Me";
pck.Workbook.Properties.Comments = "Sample Record Details";
pck.Workbook.Properties.Company = "ABC Tech";
byte[] fileBytes = pck.GetAsByteArray();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Cookies.Clear();
//Add the header & other information
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Private);
HttpContext.Current.Response.CacheControl = "private";
HttpContext.Current.Response.Charset = System.Text.UTF8Encoding.UTF8.WebName;
HttpContext.Current.Response.ContentEncoding = System.Text.UTF8Encoding.UTF8;
HttpContext.Current.Response.AppendHeader("Content-Length", fileBytes.Length.ToString());
HttpContext.Current.Response.AppendHeader("Pragma", "cache");
HttpContext.Current.Response.AppendHeader("Expires", "60");
string DateTimes = DateTime.Now.TimeOfDay.ToString().Replace(":", "_").Replace(".", "_");
//Default Download Filename
string FileName = "ExportBiblio_" + DateTimes + ".xlsx";
HttpContext.Current.Response.AppendHeader("Content-Disposition",
"attachment; " +
"filename=\"" + FileName + "\"; " +
"size=" + fileBytes.Length.ToString() + "; " +
"creation-date=" + DateTime.Now.ToString("R") + "; " +
"modification-date=" + DateTime.Now.ToString("R") + "; " +
"read-date=" + DateTime.Now.ToString("R"));
HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.sampleDemo.sheet";
//Write it back to the client
HttpContext.Current.Response.BinaryWrite(fileBytes);
HttpContext.Current.Response.End();
}
}
catch (Exception ex)
{
ex.ToString();
}
}
}
}
No comments:
Post a Comment