简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM