Luckily, based on some suggestions on both 2 site, I solved this problem (phew, God bless me)
Below is all steps to get it (I assumed we can format "$ xxx.xx USD" on grid)
Step 1:
Register the ExcelExportCellFormatting event for grid
protected void grid_ExcelExportCellFormatting(object source, ExcelExportCellFormattingEventArgs e) { var item = e.Cell.Parent as GridDataItem; var hdfCurrency = item.FindControl("
hdfCurrency
") as HiddenField;
var format =
FormatCurrencyWhenExportToExcel(
hdfCurrency.Value);
switch (e.FormattedColumn.UniqueName) { case "TotalPrice": var hdfTotalPrice = item.FindControl("
hdfTotalPrice
") as HiddenField;
// Assumption the
hdfTotalPrice.Value = "25.00"
or "-25.00";
e.Cell.Text =
hdfTotalPrice.Value
; e.Cell.Style["mso-number-format"] = format; break; } }
Step 2:
Write the FormatCurrencyWhenExportToExcel function
public static string FormatCurrencyWhenExportToExcel(string currency)
{
var regionInfo = new System.Globalization.RegionInfo(Globals.GetCurrencyCulture(currency).Name);
var format = @"\0022" + regionInfo.CurrencySymbol + @"\0022\#\,\#\#0\.00\ \0022" + regionInfo.ISOCurrencySymbol + @"\0022";
return format;
}
That's all.
Enjoy your code.
We can reference these sites:
http://niallodoherty.com/post.cfm/basic-html-to-excel-formattinghttp://agoric.com/sources/software/htmltoExcel
http://cosicimiento.blogspot.com/2008/11/styling-excel-cells-with-mso-number.html
http://www.telerik.com/help/aspnet-ajax/grid-html-export.html
exporting gridview to custom excel in c#.net
ReplyDeleteThe post is written in very a good manner and usd to inr it contains many useful information for me.
ReplyDelete