Exporting to excel loses the date format - asp.net

I am exporting the contents of SP to excel. One of the columns brings the date format as 08/2015 but when exporting to excel, the format gets changed to Aug-2015.
I did a google on the same and found that including the below code does the trick;
string style = #"<style> .text { mso-number-format:\#; } </style> ";
The exporting to excel (dataset to excel) works below;
/// <summary>
/// This method can be used for exporting data to excel from dataset
/// </summary>
/// <param name="dgrExport">System.Data.DataSet</param>
/// <param name="response">System.Web.Httpresponse</param>
public static void DataSetToExcel(System.Data.DataSet dtExport, System.Web.HttpResponse response, string strFileName)
{
string style = #"<style> .text { mso-number-format:\#; } </style> ";
//Clean up the response Object
response.Clear();
response.Charset = "";
//Set the respomse MIME type to excel
response.ContentType = "application/vnd.ms-excel";
//Opens the attachment in new window
response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName.ToString() + ".xls;");
response.ContentEncoding = Encoding.Unicode;
response.BinaryWrite(Encoding.Unicode.GetPreamble());
//Create a string writer
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
//Create an htmltextwriter which uses the stringwriter
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
//Instantiate the datagrid
System.Web.UI.WebControls.GridView dgrExport = new System.Web.UI.WebControls.GridView();
//Set input datagrid to dataset table
dgrExport.DataSource = dtExport.Tables[0];
//bind the data with datagrid
dgrExport.DataBind();
//Make header text bold
dgrExport.HeaderStyle.Font.Bold = true;
//bind the modified datagrid
dgrExport.DataBind();
//Tell the datagrid to render itself to our htmltextwriter
dgrExport.RenderControl(htmlWrite);
response.Write(style);
//Output the HTML
response.Write(stringWrite.ToString());
response.End();
}
Where am i making a mistake? please guide!
Thanks!

Problem is not with the Date format, Excel converts the data as per the DataType (Default is GENERAL) of CELL. To prevent the data conversion you have to provide the data type (TEXT) along with the data.
you have used the correct code, but style sheet .text is not applied on your data. Apply the style sheet on ALL the <TD> tags. It will 100% work and will retain your data as is you will provide (Date- 08/2015, 0001 or any data).
string style = #"<style> TD { mso-number-format:\#; } </style> ";

Here is some sample code.
Response.AddHeader("content-disposition", "attachment; filename=Report.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
DataGrid g = new DataGrid();
DataTable d = new System.Data.DataTable();
d = (DataTable)Session["ReportData"];
g.DataSource = d;
g.DataBind();
foreach (DataGridItem i in g.Items)
{
foreach (TableCell tc in i.Cells)
tc.Attributes.Add("class", "text");
}
g.RenderControl(htmlWrite);
string style = #"<style> .text { mso-number-format:\#; } </style> ";
Response.Write(style);
Response.Write(stringWrite.ToString());
Response.End();

I don't really understand a fair bit of the code (not fluent in asp.net) but I will say that if you want to force text in an excel sheet you need to define the target area as text before putting your data in there.
If my understanding of the code is correct this:
response.Write(style);
Needs to be before this.
dgrExport.RenderControl(htmlWrite);
Edit: Perhaps an alternate solution
The bit of google code you have found sets the format of the cells as text. In all likelyhood you want excel to treat the date as a date which has a display format of MM/YYYY.
maybe try replacing this:
string style = #"<style> .text { mso-number-format:\#; } </style> "
with
string style = #"<style> .text { mso-number-format:\mm/yyyy; } </style> "
I am not sure if / or \ is an escape character in ASP.net so the exact snytax might be different. In excel terms number format # means text and mm/yyyy will mean a date with the display format that you want.

Related

Export data with images to Excel

I am exporting data with images to Excel by using the following code.
Code
protected void ExportToExcel(object sender, EventArgs e)
{
//Get the data from database into datatable
string strQuery = "select CustomerID, ContactName, City, PostalCode, display_picture" +
" from customers";
SqlCommand cmd = new SqlCommand(strQuery);
DataTable dt = GetData(cmd);
//Create a dummy GridView
GridView GridView1 = new GridView();
GridView1.AllowPaging = false;
GridView1.DataSource = dt;
GridView1.DataBind();
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition",
"attachment;filename=DataTable.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
for (int i = 0; i .textmode { mso-number-format:\#; } ";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
The Excel is downloading properly. But the problem is when I am filtering the data in the Excel. The images in the Excel are in Move but don't size with cells property. How to make the images with the property, Move and size with cells?
Your code doesn't create an Excel file at all, it creates an HTML table and sends it with a fake content type, that of the old binary Excel format (xls). Excel isn't fooled, it detects that this is an HTML table and tries to import it using default settings. This can break for any number of reasons.
It's far easier and cheaper to create a real Excel file with a library like EPPlus. For starters, you can fill a sheet directly from a DataTable  :
protected void ExportToExcel(object sender, EventArgs e)
{
///...
DataTable dt = GetData(cmd);
using (ExcelPackage pck = new ExcelPackage())
{
//Create the worksheet
var ws = pck.Workbook.Worksheets.Add("Demo");
//Load the datatable into the sheet, starting from cell A1.
//Print the column names on row 1
ws.Cells["A1"].LoadFromDataTable(dt, true);
//That's it!
//Write it back to the client
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=ExcelDemo.xlsx");
Response.BinaryWrite(pck.GetAsByteArray());
}
}
You can add pictures with the Drawings.AddPicture method :
ExcelPicture pic = ws.Drawings.AddPicture("pic1", new FileInfo("PathToMyImage.png"));
The result is an xlsx file which is a package of compressed XML files. This means it's actually smaller than the HTML table or CSV files that are often generated instead of actual Excel files.
EasyXLS is a library that also exports xlsx and xls files with images.
//Create a workbook
ExcelDocument workbook = new ExcelDocument();
//Add a worksheet
ExcelWorksheet worksheet = new ExcelWorksheet("Gridview");
workbook.easy_addWorksheet(worksheet);
//Add the gridview to the worksheet
DataSet dataSet = new DataSet();
dataSet.Tables.Add((DataTable)GridView1.DataSource);
worksheet.easy_insertDataSet(dataSet);
//Add an image
worksheet.easy_addImage("image.jpg", "A10");
//Exporting gridview with image
workbook.easy_WriteXLSXFile("DataTable.xlsx");
More about inserting images, you can find at:
http://www.easyxls.com/manual/basics/excel-image-import-export.html
If the image bytes are loaded from database, you will need to temporary save the image locally on machine.
You can also check how to export gridview to excel to see more about formatting the data.

Export gridview to excel having numeric column

I want to export my gridview to excel. One column contains a numeric value, like:
12345678998765432112345678899
but when I am exporting to excel, it is showing like:
1234+E11
I don't want like this,I want entire value.
I used following code
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=ReportOutput.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
hw.WriteLine("<h3>Output Form</h3>");
GridView1.RenderControl(hw);
string style = "<style> .textmode { mso-number-format:\\#; } </style>";
Response.Write(style);
Response.Output.Write(tw.ToString());
Response.Flush();
Response.End();
Still it is not working.

ASP.Net Open office XML Set the values of a cell

I am using ASP.Net and Open office XML and I have been able to set the headers of the excel file.
However, I want to set the value to cells from say D2 to D1000 in a drop down fashion i.e. that the user can only select from a predefined list of values as in a drop down list.
How do I accomplish this?
The code for creating the excel is
List<ExcelExport> mpList = new List<ExcelExport>();
DataTable dt = ListToDataTable(mpList);
string attachment = string.Format("attachment;filename={0}-{1}.xlsx", ddlHealthFacility.SelectedItem.Text + " Excel export ", " ");
using (ExcelPackage pck = new ExcelPackage())
{
//Create the worksheet
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Monthly Plan");
ws.Cells["A1"].LoadFromDataTable(dt, true);
Byte[] fileBytes = pck.GetAsByteArray();
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", attachment);
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
Response.BinaryWrite(fileBytes);
Response.End();
}

Passing a grid to a function used to export to excel

I have a method that is used to export the data to excel.Till now I have been passing table to the method.But now I wish to pass the grid data so that I do not have to call the procedures for different instances for getting different filtered data sets.
is there a way to do so?
public void ExportToExcel(DataSet ds)
{
if (ds.Tables[0].Rows.Count > 0)
{
string filename = ds.Tables[1].Rows[0]["filename"].ToString() + ".xls";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = ds.Tables[0];
dgGrid.DataBind();
//Get the HTML for the control.
dgGrid.RenderControl(hw);
//Write the HTML back to the browser.
//Response.ContentType = application/vnd.ms-excel;
Response.Clear();
Response.ClearHeaders();
Response.Charset = "";
Response.AddHeader("content-disposition", String.Concat("attachment;filename=", filename));
Response.AddHeader("Cache-Control", "max-age=0");
Response.ContentType = "application/vnd.xls";
// this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
}
}
This is the method that I am using to export to excel.

Exporting from repeater to excel

I have been able to populate a repeater but the only problem I'm having is to export it to an excel sheet.
There are no data displayed in the excel file .
What I'm thinking is that because of some postback when I click the export button, the data on the repeater gets deleted or something.
Here is the code :
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
Table tb = new Table();
TableRow tr1 = new TableRow();
TableCell cell1 = new TableCell();
cell1.Controls.Add(Repeater1);
tr1.Cells.Add(cell1);
tb.Rows.Add(tr1);
tb.RenderControl(hw);
//style to format numbers to string
string style = #"<style> .textmode { mso-number-format:\#; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
For exporting data in Excel format I would recommend using EPPlus library instead of writing strings.
EPPlus is a .net library that reads and writes Excel 2007/2010 files
using the Open Office Xml format (xlsx). 
EPPlus supports: Cell Ranges, Cell styling (Border, Color, Fill, Font,
Number, Alignments), Charts, Pictures, Shapes, Comments, Tables,
Protection, Encryption, Pivot tables, Data validation
I think "Repeater1" and table "tb" must be attached to Page (Page.Controls.Add(tb)).
Try to:
render controls to string var rendered=RenderControlToString(tb);
then clear response Response.Clear();
then write rendered string Response.Write(rendered);
public static string RenderControlToString(Control c)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htmlWriter = new HtmlTextWriter(sw);
c.RenderControl(htmlWriter);
sw.Close();
htmlWriter.Close();
return sb.ToString();
}

Resources