ActionResult:
var strLawTable = new StringBuilder();
strLawTable.Append("<thead>");
strLawTable.Append("<tr>");
strLawTable.Append("<th style=\"text-align: right\">Dollar</th>");
strLawTable.Append("</tr>");
strLawTable.Append("</thead>");
strLawTable.Append("<tbody>");
foreach (Currency currency in Model.List)
{
strLawTable.Append("<tr>");
strLawTable.Append("<th style=\"text-align: right\">=\"" + GetExcellFormatString(Currency.USD) + "\"</th>");
strLawTable.Append("</tr>");
}
strLawTable.Append("</tbody>");
string headerTable = "<table>" + strLawTable + "</table>";
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=TestFile.xls");
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = System.Text.Encoding.Unicode;
Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
System.IO.StringWriter sw = new System.IO.StringWriter();
sw.Write(headerTable);
System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(sw);
Response.Write(sw.ToString());
Response.End();
GetExcellFormatString method:
public string GetExcellFormatString(double doubleAmount)
{
if (doubleAmount < 1000)
return doubleAmount.ToString("0.00");
else if (doubleAmount < 1000000)
return doubleAmount.ToString("0000.00");
else if (doubleAmount < 1000000000)
return doubleAmount.ToString("0000,000.00");
else if (doubleAmount < 1000000000000)
return doubleAmount.ToString("0000000000.00");
else return doubleAmount.ToString();
}
My question:
After I exported the data into an Excel file, I have values in my Excel table below,
Exported Excel table
Dollar
50.0
35.0
40.0
60.0
etc..
If I press to "ctrl" and click with mouse 35.0 and 40.0 Excel can not sum 35.0 and 40.0 because 35.0 is string displays "35.0" and 40.0 is string displays "40.0".
The problem is about sum selected rows however I can not sum because values are string not numeric
How can I remove " " out of string number in Excel table by C# code ?
Thanks.
,instead of.?strLawTable.Append("<th style=\"text-align: right\">=\"" + GetExcellFormatString(Currency.USD) + "\"</th>");to ->strLawTable.Append("<th style=\"text-align: right\">=" + GetExcellFormatString(Currency.USD) + "</th>");