My xlsx file is downloaded but when I open that xlsx file it shows warning as
'We found a problem with some content in'.
Can anyone tell me where I have to fix this? I am using an iFrame to download the file.
using (ClosedXML.Excel.XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(dt, "Sheet1");
Response.Clear();
Response.Charset = "";
Response.Buffer = true;
Response.BinaryWrite(bytes);
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=" + >Model.reportName + "_" + DateTime.Now.ToString("yyyyMMddHH:mm") + "." + extension);
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.Close();
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
Related
I am trying to generate a csv with UTF8 encoding with c#, using ExcelPackage (). Excel is identifying the file as .xlsx and not UTF8. My code:
using (ExcelPackage package = new ExcelPackage())
{
ExcelWorksheet ws = package.Workbook.Worksheets.Add("myName");
int y = 1;
foreach (IDictionary<string, string> elemento in losDatos)
{
ws.Cells["A" + y].Value = "+ Disposición de contacto";
ws.Cells["B" + y].Value = "Llamada detallada";
...
...
ws.Cells["AL" + y].Value = "1";
y++;
}
Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("Content-Disposition", "attachment;filename=myName" + DateTime.Now.ToString("yyyyMMdd_HHmm") + ".csv");
Response.BinaryWrite(package.GetAsByteArray());
Response.End();
}
I have tried some changes, only in the Response piece, and now the csv recognizes me, but the characters are strangers:
Response.Clear();
Response.ContentType = "application/csv";
Response.AddHeader("Content-Disposition", "attachment;filename=myName" + DateTime.Now.ToString("yyyyMMdd_HHmm") + ".csv");
Response.BinaryWrite(Encoding.UTF8.GetPreamble());
Response.BinaryWrite(package.GetAsByteArray());
Response.End();
Replace
Response.ContentType = "application/csv";
Response.AddHeader("Content-Disposition", "attachment;filename=myName" + DateTime.Now.ToString("yyyyMMdd_HHmm") + ".csv");
Response.BinaryWrite(Encoding.UTF8.GetPreamble());
with
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("Content-Disposition", "attachment;filename=myName" + DateTime.Now.ToString("yyyyMMdd_HHmm") + ".xlsx");
and you are done.
If you want a CSV, use a different package but then do consider adding charset to the Content-Type header. If Open XML is not your thing, then OpenDocument Spreadsheet (.odt, LibreOffice) would be equally better than CSV.
string fileName = "test" + DateTime.Now.ToShortDateString();
using (package = new ExcelPackage(newFile, "file"))
{
generateReport();//this function generates a worksheet
package.Workbook.Properties.Title = #"Title of project";
package.Workbook.Properties.Author = "Sri";
package.Workbook.Properties.Subject = #"Report";
byte[] rawData = package.GetAsByteArray();
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("content-disposition", "attachment; filename=" + fileName + ".xlsx");
Response.BinaryWrite(rawData);
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.Flush();
Response.End();
Hi,when i try to export datatable to excel ,columns which has date appears as numbers in the excel file.When i further check these numbers are DATEVALUE(date)(this function is used in excel) results of excel.
the below code works if I try to download a .txt/.msg/.pdf/.png/.jpg but it fails if I try to download exccel or word files. Can anybody please help
Response.ContentType = "APPLICATION/OCTET-STREAM";
//Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
fileUrl = Server.UrlDecode(fileUrl);
System.String disHeader = "attachment; filename=\"" + fileUrl + "\"";
Response.AppendHeader("Content-Disposition", disHeader);
// transfer the file byte-by-byte to the response object
System.IO.FileInfo fileToDownload = new System.IO.FileInfo(fileUrl);
Response.Flush();
Response.WriteFile(fileToDownload.FullName);
I am actually wondering why when I create a zip file, and I download it just after, I can't open it because it's broken ..
This is the code I am actually using .
string url = ((LinkButton)sender).Tag;
var downloadFileName = string.Format(((LinkButton)sender).ID + ".zip");
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo("C://inetpub//wwwroot//Files//Wireframes//" + url);
using (ZipFile zip = new ZipFile())
{
zip.AddEntry("C://inetpub//wwwroot//Files//Wireframes//" + url, zip.Name);
zip.AddDirectory("C://inetpub//wwwroot//Files//Wireframes//" + url);
zip.Save(downloadFileName);
}
Response.ContentType = "application/zip";
Response.AddHeader("Content-Disposition", "filename=" + downloadFileName);
Also, I am adding directly all the directories with their files inside.
You are not sending the actual data.
Response.ContentType = "application/zip";
Response.AddHeader("Content-Disposition", "filename=" + downloadFileName);
Response.TransmitFile(downloadFileName);
Response.End();
I saved the document file/pdf file or txt file in the database. Now to get that File I am using the code below.
JobApplicantResume oApplicantResumne = new JobApplicantResume();
DataSet dsApplicantResume = oApplicantResumne.GetJobApplicantResumeByJobApplicantResumeId(1552);//1552 Long value
Response.ClearContent();
Response.ClearHeaders();
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/xx-xxxx";
Response.AddHeader("Content-Disposition", "attachment;filename=" + dsApplicantResume.Tables[0].Rows[0]["sFileName"].ToString());
//Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Byte[] bytes = (Byte[])dsApplicantResume.Tables[0].Rows[0]["binFile"];
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
It is giving the exact file. No problem is there to open the file.
In some other page I used the same code:
JobApplicantResume oApplicantResumne = new JobApplicantResume();
DataSet dsApplicantResume = oApplicantResumne.GetJobApplicantResumeByJobApplicantResumeId(1552);//1552 Long value
Response.ClearContent();
Response.ClearHeaders();
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/xx-xxxx";
Response.AddHeader("Content-Disposition", "attachment;filename=" + dsApplicantResume.Tables[0].Rows[0]["sFileName"].ToString());
//Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Byte[] bytes = (Byte[])dsApplicantResume.Tables[0].Rows[0]["binFile"];
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
It is also giving a file to download. But when I open it the data are coming in some different format.
Like:
؟½ï؟½ï؟½ï؟½ï؟½ï؟
Junk data. I could not understand why the problem is coming. If any one faced this similar kind of issue or any suggestion / help will be very much helpful to overcome this problem.
Thanks a lot for your attention.
Try to add Unicode Byte-Order-Mark full example.
//add the BOM
byte[] bBOM = new byte[] { 0xEF, 0xBB, 0xBF };
byte[] bContent = ms.ToArray();
byte[] bToWrite = new byte[bBOM.Length + bContent.Length];
//combile the BOM and the content
bBOM.CopyTo( bToWrite, 0 );
bContent.CopyTo( bToWrite, bBOM.Length );
//write to the client
HttpContext.Current.Response.Write( Encoding.UTF8.GetString( bToWrite ) );
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();