using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
I wrote this code but at the opening time of excel sheet it showing log error.Can any one solve this.
using System.Data.OleDb;
using System.Data;
using System.IO;
using System.Web.UI.HtmlControls;
public partial class Default3 : System.Web.UI.Page
{
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
OleDbConnection cn = new OleDbConnection("user id=sa;password=123;provider=sqloledb.1;database=northwind");
OleDbDataAdapter da1 = new OleDbDataAdapter("select * from employees", cn);
OleDbDataAdapter da2 = new OleDbDataAdapter("select * from orders", cn);
ds = new DataSet();
cn.Open();
da1.Fill(ds, "e");
da2.Fill(ds, "d");
GridView1.DataSource = ds.Tables[0];
GridView2.DataSource = ds.Tables[1];
GridView1.DataBind();
GridView2.DataBind();
ExportToExcel(ds, "e:\\Export.xls");
}
// Response.Write(ds.Tables.Count);
}
protected void Button1_Click(object sender, EventArgs e)
{
ExportToExcel(ds, "e:\\Export.xls");
// ExportFromHtmlForm(GridView1);
// ExportFromHtmlForm(GridView2);
}
/* public void ExportFromHtmlForm(GridView gv)
{
HtmlForm form = new HtmlForm();
string attachment = "attachment; filename=e:\\PrintDetails.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
//namespace (using system.IO)
StringWriter stw = new StringWriter();
HtmlTextWriter htextw = new HtmlTextWriter(stw);
gv.Parent.Controls.Add(form);
form.Attributes["runat"] = "server";
form.Controls.Add(gv);
this.Controls.Add(form);
form.RenderControl(htextw);
Response.Write(stw.ToString());
Response.End();
}*/
public static void ExportToExcel(DataSet dsInput, string ExcelFileName)
{
System.IO.StreamWriter ExportToExcelDoc;
ExportToExcelDoc = new System.IO.StreamWriter(ExcelFileName);
//Header Part of the Excel file is created here.
const string startExcelXML = "
" xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\n " +
"xmlns:x=\"urn:schemas- microsoft-com:office:" +
"excel\"\r\n xmlns:ss=\"urn:schemas-microsoft-com:" +
"office:spreadsheet\">\r\n
"\r\n " +
"\r\n " +
"\r\n "ss:ID=\"Decimal\">\r\n
"\r\n "ss:ID=\"DateLiteral\">\r\n
"
//Footer Part of the Excel is declared here
const string endExcelXML = "";
int rowCount = 0;
int sheetCount = 1;
for (int i = 0; i < dsInput.Tables.Count; i++)
{
ExportToExcelDoc.Write(startExcelXML);
ExportToExcelDoc.Write("
ExportToExcelDoc.Write("
ExportToExcelDoc.Write("
ExportToExcelDoc.Write("
ExportToExcelDoc.Write("
ExportToExcelDoc.Write("
ExportToExcelDoc.Write(endExcelXML);
}
ExportToExcelDoc.Close();
}
}
Suthish NairPosted Oct 28, 2011, 3:30 PM
Sam HobbsPosted Oct 28, 2011, 1:49 PM
Javeed M ShaikhPosted Oct 28, 2011, 10:08 AM