Click here to Skip to main content
15,909,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In C# I import Excel into Table through SQL SP.Then I update that table.After that I Export that Table into Excel(During Export to Excel I got Error as below)

HttpException was unhandled by user code


SQL
System.Web.HttpException was unhandled by user code
  Message="DataGrid with id '' could not automatically generate any columns from the selected data source."


My coding

 protected void Button1_Click(object sender, EventArgs e)
 {
     connectionstring();
     SqlConnection con = new SqlConnection(str);
     con.Open();
     SqlCommand cmd = new SqlCommand("SP_Excel", con);
     cmd.CommandType = CommandType.StoredProcedure;
     cmd.Parameters.Add("@ExcelName", SqlDbType.VarChar).Value = FileUpload1.FileName.ToString();
     cmd.ExecuteNonQuery();
     con.Close();
     Response.Write("<script>alert('Table Imported Successfully');</script>");
     Button1.Visible = false;
     Button2.Visible = true;

     SqlDataAdapter da = new SqlDataAdapter(cmd);
     DataTable dt = new DataTable();
     da.Fill(dt);
     GridView1.DataSource = dt;
     GridView1.DataBind();
     ViewState["dtList"] = dt;


protected void Button2_Click(object sender, EventArgs e)
 {
     DataTable dt1 = (DataTable)ViewState["dtList"];
     if (dt1 == null)
     {
         throw new Exception("No Records to Export");
     }
     string Path = "D:\\ImportExcelFromDatabase\\myexcelfile_" + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Month.ToString() + ".xlsb";
     FileInfo FI = new FileInfo(Path);
     StringWriter stringWriter = new StringWriter();
     HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
     DataGrid DataGrd = new DataGrid();
     DataGrd.DataSource = dt1;
     DataGrd.DataBind();
     DataGrd.RenderControl(htmlWrite);
     string directory = Path.Substring(0, Path.LastIndexOf("\\"));// GetDirectory(Path);
     if (!Directory.Exists(directory))
     {
         Directory.CreateDirectory(directory);
     }
     System.IO.StreamWriter vw = new System.IO.StreamWriter(Path, true);
     stringWriter.ToString().Normalize();
     vw.Write(stringWriter.ToString());
     vw.Flush();
     vw.Close();
     WriteAttachment(FI.Name, "application/vnd.ms-excel", stringWriter.ToString());
 }
 public static void WriteAttachment(string FileName, string FileType, string content)
 {
     HttpResponse Response = System.Web.HttpContext.Current.Response;
     Response.ClearHeaders();
     Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
     Response.ContentType = FileType;
     Response.Write(content);
     Response.End();
 }
Posted

instead of
Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
try
Response.AddHeader("Content-Disposition", string.Format("attachment; filename = {0}" + FileName);
 
Share this answer
 
Comments
Saranya Boopathi 25-Apr-13 6:27am    
I got error in this block

string Path = "D:\\ImportExcelFromDatabase\\myexcelfile_" + DateTime.Now.Day.ToString() + "_" + DateTime.Now.Month.ToString() + ".xlsb";
FileInfo FI = new FileInfo(Path);
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
DataGrid DataGrd = new DataGrid();
DataGrd.DataSource = dt1;
DataGrd.DataBind();
DataGrd.RenderControl(htmlWrite);
string directory = Path.Substring(0, Path.LastIndexOf("\\"));// GetDirectory(Path);
if (!Directory.Exists(directory))
Hi,

you have to change the MIME type as
application/vnd.ms-excel.sheet.binary.macroEnabled.12 for generating .xlsb files.

refer What are the Microsoft Office MIME Types?[^] for valid MIME types for various types.

So, change your code as below.
C#
WriteAttachment(FI.Name, "application/vnd.ms-excel.sheet.binary.macroEnabled.12", stringWriter.ToString());


hope it helps.
 
Share this answer
 
Looks like your datagrid is not able to iterate through columns from your datasource (excel, from viewstate) That is not that strange. How on earth can your grid know what columns you have in your woorksheet? In Excel you can put your heading in row number 5 and start your columns at column C if you want. And you can have multiple "table" in each sheet. Excel is not structured the same way as a database.

You could do this almost as you do, but you will have to read the data from your database when you export, not from your viewstate (which is excel)

A little tip. This open source project is superb. It'l export your data as excel and it's very easy to use:
http://closedxml.codeplex.com/[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900