I have a record set (DataTable) that I am saving to Excel and then need to save to a database (as binary).
I have two parts working separately, but I can't figure out how to tie them both together without having to actually save a hard copy of the Excel to the disk.
Here is the "export to Excel" code:
string attachment = "attachment; filename=" + filename;
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gvCompleteCases.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
and the "save file to database" snippet:
byte[] fileData = null;
using (var binaryReader = new BinaryReader(file.InputStream))
{
fileData = binaryReader.ReadBytes(file.ContentLength);
UploadFile.Upload2(GetD(), file.ContentLength, fu.FileBytes, numberOfPages);
}
The above example saves the file to user's machine and then I have to point to its location to save to the database, is there a way I can create the Excel file, without saving to user's hard disk, before saving it to the database?
I guess what I'm asking is how do I get the fileData of the created Excel, without saving it to the computer?
Related
I'm using the following code to generate an Excel file
private void ExportGridView(GridView gv)
{
string attachment = "attachment; filename=Factuurtjes.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
// Create a form to contain the grid
HtmlForm frm = new HtmlForm();
gv.Parent.Controls.Add(frm);
//Response.Write("Opdrachtgever geselecteerd: " + ddlContractor.SelectedItem.ToString());
frm.Attributes["runat"] = "server";
frm.Controls.Add(gv);
frm.RenderControl(htw);
//GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
This all works fine and the excel file gets saved as it should.
I can open the excel file with excel 2013. But when I want to open it with Excel 2016, it gives me a warning message that the file could be damaged.
I would like a solution for this so I can open the files with Excel 2016.
Can someone help me with this?
Thanks in advance.
I think this Save-an-Excel-2016-workbook-for-compatibility-with-earlier-versions-of-Excel can help to solve your problem.
Here is my C# Code :
protected void Button1_Click(object sender, EventArgs e)
{
string attachment = "attachment; filename=" + Session["pdf_name"] + ".pdf";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/pdf";
StringWriter s_tw = new StringWriter();
HtmlTextWriter h_textw = new HtmlTextWriter(s_tw);
Label1.Style.Add("font-size", "16pt");
Label2.Style.Add("font-size", "16pt");
Label3.Style.Add("font-size", "16pt");
h_textw.AddStyleAttribute("font-size", "8pt");
h_textw.AddStyleAttribute("color", "Black");
Panel1.RenderControl(h_textw);
Document doc = new Document();
PdfWriter.GetInstance(doc, Response.OutputStream);
doc.Open();
StringReader s_tr = new StringReader(s_tw.ToString());
HTMLWorker html_worker = new HTMLWorker(doc);
html_worker.Parse(s_tr);
doc.Close();
Response.Write(doc);
}
I want to save this pdf in this Path "H:\new\web\pdf_filename.pdf"
I am using ASP.Net C#.
You cannot specify where on the client machine the downloaded file will be saved.
If you want that type of functionality, you will need to run a plugin in the browser clientside that has access to the local file system.
The browser will take the filename you specify in your attachment header value as a suggestion, but even that can be overridden by the user when he actually saves it.
If you want to save it on the server, you only have to save it somewhere local (to the server) using any of the file-related methods or types.
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(#"H:\new\web\pdf_filename.pdf", FileMode.Create));
try and see if this work, please make sure your authentication type is allow user to access that folder. Not everyone map H drive to same network path,
I am trying to convert the gridview to excel and downloading the excel sheet to my computer's specific folder in my c# application.
The problem is: The file is getting downloaded at both the places.The destination folder and also the download folder.
the code for this is:
private void ExportToExcel(GridView GrdView, string fname)
{
Response.Clear();
Response.AddHeader("content-disposition", "inline;filename=" + fname + ".xls");
Response.Charset = "";
Response.ContentType = "application/OCTET-STREAM";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite =
new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
string renderedGridView = stringWrite.ToString();
File.WriteAllText(#"C:\\Users\abc\\Desktop\" + fname + ".xls", renderedGridView);
Response.Write(stringWrite.ToString());
Response.End();
}
How to avoid the files getting downloaded to the download folder?
Please help!
Thank you
The answer would be to pick one: either render the control to a string and output to Response OR WriteAllText. If you want to use the WriteAllText to save the file to a determined location, then perform the action and pop up a notification to the user that the file was saved.
A friend of mine gave me this task so I can learn advance programming a little bit easier. I am currently doing convertion of an html page to pdf using itext sharp and email the pdf as an attachement. My idea is to save the pdf first to the server machine in the folder name ToBeEmailedPDF folder before using it as an email attachment. The thing that bothers me is that this dialog as what you can see in the picture shows up using the code that I have below.
StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
pnlPDF.RenderControl(hw); // pnlPDF contains the html contents to be converted to pdf
string htmlDisplayText = sb.ToString();
Document document = new Document();
MemoryStream ms = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(document, ms);
StringReader se = new StringReader(htmlDisplayText);
HTMLWorker obj = new HTMLWorker(document);
document.Open();
obj.Parse(se);
// step 5: we close the document
document.Close();
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=report.pdf");
Response.ContentType = "application/pdf";
Response.Buffer = true;
Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
Response.OutputStream.Flush();
Response.End();
I know many of you out there knows a better way or know how to solve my problem. Please help me. Thank you!
There is big difference between client-side and server-side.
Response class can output contents to client machine, if you need to save file on server use something like File.WriteAllBytes (msdn) method
What you are trying to achieve is impossible for security reasons. Only the user can decide where he wants the attachment to be saved. Imagine if this was possible: you would then be able to save any kind of viruses into any kind of folders on the user's computer. As an alternative to showing the Save dialog you could open the PDF inline:
Response.AddHeader("Content-Disposition", "inline; filename=report.pdf");
You can save it using the following code:
PdfWriter.GetInstance(pdfDoc, new FileStream(context.Server.MapPath("~") + "/PDF/" + reportName + ".pdf", FileMode.Create));
i m trying to generate an excel file from my web page but i have some issues with Turkish chars such as "İ" "ğ" when i open the file some chars are incorrect (İ seems Ä°) here is my code
gvExpRequests.DataSource = dsExpRequests;
gvExpRequests.DataBind();
gvExpRequests.GridLines = GridLines.Both;
Page.EnableViewState = false;
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=export.xls");
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = Encoding.UTF8;
Response.BinaryWrite(Encoding.UTF8.GetPreamble());
StringWriter yaz = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(yaz);
gvExpRequests.RenderControl(htw);
i don't know what's wrong with here i tried a lot of encoding but i got same results every time i want to try something different to do this are there any another way to export a excel file from a gridview