I want to format all cells of the spreadsheet as text before loading it with the datatable.
Here is the sample code I am using
StringBuilder sbitems = new StringBuilder();
sbitems.Append(@"select * from Items");
SqlDataAdapter daitems = null;
DataSet dsitems = null;
daitems = new SqlDataAdapter(sbitems.ToString(), constate);
daitems.SelectCommand.CommandTimeout = 0;
dsitems = new DataSet("Items");
daitems.Fill(dsitems);
app.Workbook.Worksheets.Add("Items").Cells["A1"].LoadFromDataTable(dsitems.Tables[0], true);
Excel.ExcelWorksheet worksheet2 = workBook.Worksheets["Items"];
using (var rngitems = worksheet2.Cells["A1:BH1"])//Giving colour to header
{
rngitems.Style.Font.Bold = true;
rngitems.Style.Fill.PatternType = ExcelFillStyle.Solid;
rngitems.Style.Fill.BackgroundColor.SetColor(Color.Yellow);
rngitems.Style.Font.Size = 11;
rngitems.AutoFitColumns();
}
worksheet2.Cells["A1:BH1"].AutoFitColumns();
worksheet2.Cells["A1:BH1"].Style.Font.Bold = true;
app.SaveAs(new System.IO.FileInfo(@"D:\ItemsData\testfileexcelnew.xlsx"));
Try setting Number format as @
ex: rngitems.Style.Numberformat.Format = "@";
@ formats cell as Text.
Reference : Force EPPLUS to read as text
possibly duplicate of above thread.