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.
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.
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();
}
When exporting SQL Server data to .csv, phrases in Persian don't show up correctly.
string date = Helper.ToPersianDate(DateTime.Now).Replace("/", "");
date += ".csv";
Response.Clear();
Response.AppendHeader("content-disposition", "attachment; filename=" + date);
Response.ContentType = "text/csv";
Response.Charset = Encoding.Unicode.ToString();
foreach (DataRow r in ds.Tables[0].Rows)
{
Response.Write(r[0]);
Response.Write("\r\n");
}
Response.End();
string date = Helper.ToPersianDate(DateTime.Now).Replace("/", "");
date += ".csv";
Response.Clear();
Response.AppendHeader("content-disposition", "attachment; filename=" + date);
Response.ContentType = "text/csv";
Response.Charset = Encoding.Unicode.ToString();
Response.ContentEncoding = System.Text.Encoding.Unicode;
/*
save utf-8 with BOM
GetPreamble
*/
Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
foreach (DataRow r in ds.Tables[0].Rows)
{
Response.Write(r[0]);
Response.Write("\r\n");
}
Response.End();
I'm generating an Excel report in C# and need to add a second tab to the Excel file that will contain reference data. How do I achieve this without using ClosedXML ?
Response.AddHeader("content-disposition", "attachment; filename=" + fname);
Response.ContentType = "application/vnd.ms-excel";
var writer = new System.IO.StringWriter();
var htmlTextWriter = new HtmlTextWriter(writer);
dataGrid.RenderControl(htmlTextWriter);
Response.Write(writer.ToString());
Response.End();
I have created a page which will accept SharePoint list name as query string and generate an excel file
I am trying to export to excel using the below code
ListName = ListName.Replace(" ", "");
string attachment = "attachment; filename=" + ListName + ".xls";
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel 8.0";
HttpContext.Current.Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htmlwriter = new HtmlTextWriter(sw);
gridview.DataSource = _dtFramedTable;
gridview.DataBind();
htmlwriter.AddAttribute("xmlns:x", "urn:schemas-microsoft-com:office:excel");
htmlwriter.RenderBeginTag(HtmlTextWriterTag.Html);
htmlwriter.RenderBeginTag(HtmlTextWriterTag.Head);
htmlwriter.RenderBeginTag(HtmlTextWriterTag.Style);
htmlwriter.Write("br {mso-data-placement:same-cell;}");
htmlwriter.RenderEndTag();
htmlwriter.RenderEndTag();
htmlwriter.RenderBeginTag(HtmlTextWriterTag.Body);
gridview.RenderControl(htmlwriter);
htmlwriter.RenderEndTag();
htmlwriter.RenderEndTag();
HttpContext.Current.Response.Write(HttpUtility.HtmlDecode(sw.ToString()));
HttpContext.Current.Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
HttpContext.Current.Response.Close();
This exports gridview to excel, but when I open the excel file, it says
file format and extension of don't match. the file could be corrupted
or unsafe unless you trust the source, don't open it
How can this be avoided?
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=\""+Export.xls+"\"; filename*=UTF-8''"+Uri.EscapeDataString(Export.xls)); );
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "example.xls"))