I am exporting dataset to excel using the following code.
public void Export()
{
string fileName = "Non-LaborActuals";
string cmd = "";
if (grid == "NLP")
{
fileName = wrnum + "_Paid Non-Labor.xls";
cmd = "sp_Act_NonLabor_Paid_export";
}
if (grid == "NLC")
{
fileName = wrnum + "_Committed Non-Labor.xls";
cmd = "sp_Act_NonLabor_Commit_export";
}
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.Clear();
response.Charset = "";
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=\"" + fileName);
DataSet ds1;
try
{
using (SqlCommand sqlCmd1 = new SqlCommand(cmd))
{
sqlCmd1.CommandType = CommandType.StoredProcedure;
sqlCmd1.Parameters.Add(new SqlParameter("@c_service_req", SqlDbType.VarChar, 10));
sqlCmd1.Parameters["@c_service_req"].Value = wrnum;
ds1 = db.ExecuteDataSet(sqlCmd1);
}
using (System.IO.StringWriter sw = new System.IO.StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
DataGrid dg = new DataGrid();
dg.DataSource = ds1.Tables[0];
dg.DataBind();
dg.RenderControl(htw);
response.Write(sw.ToString());
response.End();
}
}
}
catch
{
}
}
--Below a subset of result after export to excel (sorry about formatting, i cannot upload image file)
EXPENSE_TYPE_DESC HOURS COST_USD Standard Hours 8 903.2 Standard Hours 16 1,172.80 Standard Hours 40 372 Standard Hours 32 297.6 Standard Hours 5 90 Standard Hours 71 1,278.00 Standard Hours 29 0
--The problem, Excel is truncating zeros in decimal places for numbers less than 1000 and i don't see this issue for numbers greater than 1000. I want to see the decilam places even is the value is 0 (like 0.00). I do not want to apply text style to the column. Any suggestions or help to achieve this is greatly appreciated. Thanks in advance.