Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using EPPlus I want to Format all cells as TEXT in a spreadsheet

Tags:

c#

excel

epplus

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"));
like image 443
siva Avatar asked Aug 23 '16 05:08

siva


1 Answers

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.

like image 174
par Avatar answered Nov 09 '22 17:11

par