Convert .aspx page to Microsoft Excel Page - c#

HI i want to use such thing in my web application that on a button click .aspx page converted in Xls page.
I did it but result is not good as I needed. I used this code on button click which is given below.
Response.Clear();
Response.Buffer = true;
string filename = "TicketSecretary.xlxs";
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;

I'm missing the Response.End(); at the end.
http://msdn.microsoft.com/en-us/library/system.web.httpresponse.end.aspx

Related

Byte array not writing to pdf stream c#

I've have a byte array that has all the data needed and can be displayed in a pdf that can be opened if I use this code:
System.IO.File.WriteAllBytes(#"C:\Users\Person\Downloads\results.pdf", bytes)
That let's me know the byte array should be good. The issue is I'm trying to create a pdf on the fly and when I view the call in the network tab in dev tools, it shows a 200 and in the response tab I get a text response. When I copy that response in a text editor and save it as a pdf, it has the correct amount of pages, but no data in it. So I have two issues here. For some reason, it is not being returned as a pdf AND the byte array is not being written to it. Any suggestions on what I can do? I've looked at stack overflow all day and have tried all kinds of things including setting the contentType to application/pdf.
byte[] bytes = ms.ToArray();
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.Buffer = true;
response.AddHeader("Accept-Header", bytes.Length.ToString());
response.AddHeader("Content-Length", bytes.Length.ToString());
response.AddHeader("Content-Disposition", "attachment;filename=Results.pdf");
response.AddHeader("Content-Description", "File Download");
response.ContentType = "application/octet-stream";
response.OutputStream.Write(bytes, 0, onvert.ToInt32(bytes.Length));
//I've tried using this as well
//response.BinaryWrite(bytes);
response.Flush();
response.End();
UPDATED CODE:
byte[] bytes = ms.ToArray();
HttpResponse response = HttpContext.Current.Response;
try
{
response.Clear();
response.ClearHeaders();
response.ClearContent();
response.AddHeader("Accept-Header", bytes.Length.ToString());
response.AddHeader("Content-Length", bytes.Length.ToString());
response.AddHeader("Content-Disposition", "attachment;filename=Results.pdf");
response.ContentType = "application/pdf";
response.BinaryWrite(bytes);
response.Flush();
}
catch { }
finally
{
response.End();
}
I got it working with this:
response.Clear();
response.ClearHeaders();
response.ClearContent();
response.ContentType = "application/pdf";
response.AddHeader("content-disposition", "attachment; filename=Results.pdf");
response.BinaryWrite(bytes);
response.Flush();
response.Close();

Binary to File download - But the downloaded file has no file extension?

protected void getFileAttachment(byte[] bytes)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = fileType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + "test101");
Response.OutputStream.Write(bytes,0,bytes.Length);
Response.BinaryWrite(bytes);
Response.Flush();
}
I'm trying to convert varbinary that is saved in SQL Server and I'm having trouble because the output file has no extension
Response.AppendHeader("Content-Disposition", "attachment; filename=" + "test101.filetype");
My fault - I did not include the file extension in my database
Thanks for the comments!

how to download an excel file from aspx webpage

I am trying to fetch an excel file stored in bytes from database from an aspx webpage and download and open in .xls format, however the file is getting downloaded as .aspx . How to resolve this?
I tried:
private void download(DataTable dt)
{
if (dt.Rows.Count > 0)
{
Byte[] bytes = (Byte[])dt.Rows[0]["xyz"];
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
Response.BinaryWrite(bytes);
// Response.Flush();
Response.End();
}
}
You need to set the Content-Disposition header. For example:
Content-Disposition: attachment; filename=your-excel-file.xlsx
And in code:
Response.AddHeader("Content-Disposition", "attachment; filename=your-excel-file.xlsx");

Downloading a file from a link in a GridView inside an UpdatePanel fails

I am trying to save a file (image, word, pdf or any type) in a database via a FileUpload control in asp.net (i.e. file.Docx etc). I then wish to display the file name as a link in a GridView so when the user clicks it, the file will be downloaded.
I have tried everything. In debugging it shows nothing. It reaches the end of Response.End but downloads nothing.
GridView:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnLinkDownloadTender" runat="server" Text='<%# Eval("UploadedTenderPath") %>'CommandArgument='<%# Eval("UploadedTenderPath") %>' OnClick="DownloadTender"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Upload:
protected void UploadTender()
{
try
{
if (FileUpload1.HasFile)
{
string fileName = Path.GetFileName(FileUpload1.FileName);
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/UploadedTenders/") + fileName);
HdnFieldUploadedTender.Value = fileName;
ResultLabel.ResultLabelAttributes("Tender Uploaded", ProjectUserControls.Enums.ResultLabel_Color.Red);
ResultPanel.Controls.Add(ResultLabel);
}
else
{
ResultLabel.ResultLabelAttributes("No file specified", ProjectUserControls.Enums.ResultLabel_Color.Red);
ResultPanel.Controls.Add(ResultLabel);
}
}
}
Download:
protected void DownloadTender(object sender, EventArgs e)
{
string filePath = (sender as LinkButton).CommandArgument;
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath) );
Response.Write(filePath);
Response.End();
}
Folder structure:
Try the following,
also give us more information about your page. also if you are using chrome check that chrome isn't blocking it
var fileInfo = new FileInfo(filePath);
Response.Clear();
Response.Buffer = true;
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileInfo.Name);
Response.AddHeader("Content-Length", fileInfo.Length.ToString(CultureInfo.InvariantCulture));
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(File.ReadAllBytes(fileInfo.FullName));
Response.Flush();
Response.End();
Edit:
In your gridview Row Databound add the following
LinkButton lb = e.Row.FindControl("btnLinkDownloadTender") as LinkButton;
if (lb != null)
ScriptManager.GetCurrent(this).RegisterPostBackControl(lb);
The above code will register the LinkButton to cause a Full Postback, this will allow the Download to succeed, the same will apply when doing an upload of a file in an Update Panel
try this:
Response.ContentType = "application/octet-stream";
Response.AddHeader("content-disposition", "attachment; filename=" + file_name);
Response.WriteFile(filePath);
Response.Flush();
Response.End();
Replace AppendHeader to AddHeader
string fullFilePath = Server.MapPath("~/UploadedTenders/") + filePath;
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("Content-Disposition", "attachment; filename=" + fullFilePath );
Response.ContentType = ContentType;
Response.TransmitFile(fullFilePath);

export html file to excel using asp.net

Can anybody help me I am a beginner and have no concept how the conversion can be made.
Here is my code:
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");
Response.ContentType = "application/vnd.ms-excel";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
Response.ContentEncoding = System.Text.Encoding.Unicode;
fuFile.RenderControl(hw);
Response.Output.Write(sw.ToString().Replace("&nbsp;", "").Replace("&", "&"));
Response.End();
Is this not enough to export an HTML file to Excel?
I have found this works:
String content = "<html><body><table><tr><td>your table</td></tr></table></body></html>";
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");
Response.ContentType = "application/vnd.xls";
Response.Cache.SetCacheability(HttpCacheability.NoCache); // not necessarily required
Response.Charset = "";
Response.Output.Write(content);
Response.End();
As long as your content is well-formed HTML, this should work.
Also note that comments, VBA code/macros, and multiple work-sheets do not work with this method. Additionally any styling that is not inline will also not render.
And some other considerations on this matter:
How can I export tables to excel from a webpage
http://www.c-sharpcorner.com/UploadFile/ankurmalik123/export-html-to-excel-and-more/

Categories