hi
I use below code to export excell file form database:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataLayerB;
using System.IO;
using System.Data;
using ClosedXML.Excel;
using System.Configuration;
using System.Data.SqlClient;
protected void Imgsave_Click(object sender, ImageClickEventArgs e)
{
using (SqlConnection conn = General.GetConnection())
{
using (SqlCommand _cmd = General.GetCommand("Estate_ViewInfo", conn))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
_cmd.Connection = conn;
sda.SelectCommand = _cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(dt, "Customers");
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
//ذخیره فایل به تاریخ
Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", miladitoshamsi(DateTime.Now.ToString("yyyy-MM-dd"))));
//Response.AddHeader("content-disposition", "attachment;filename=SqlExport.xlsx");
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
}
}
}
}
}
but below error happen:
Server Error in '/' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1674: 'XLWorkbook': type used in a using statement must be implicitly convertible to 'System.IDisposable'
Source Error:
|
Line 155: {
Line 156: sda.Fill(dt);
Line 157: using (XLWorkbook wb = new XLWorkbook())
Line 158: {
Line 159:
|
best regards
neda