am using EPPlus
to export data to excel(from MemoryStream
), below is the code
private static MemoryStream ExportToExcelAsStram(DataSet ds)
{
MemoryStream ms = new MemoryStream();
ExcelPackage package = new ExcelPackage(ms);
try
{
for (int i = 0; i < ds.Tables.Count; i++)
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add((ds.Tables[i].Rows[i]["Date"]).ToString());
using (ExcelRange rng = worksheet.Cells["B1 : B" + (ds.Tables[i].Rows.Count + 1)])
{
rng.Style.Numberformat.Format = "#";
}
//worksheet.Cells["B1 : B" + (ds.Tables[i].Rows.Count + 1)].Style.Numberformat.Format = "@";
worksheet.Cells["A1"].LoadFromDataTable(ds.Tables[i], true);
//Format the header for column 1-9
using (ExcelRange range = worksheet.Cells[1, 1, 1, 12])
{
range.Style.Font.Bold = true;
range.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
range.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.DarkRed);
range.Style.Font.Color.SetColor(System.Drawing.Color.White);
}
worksheet.Cells["A1:L" + (ds.Tables[i].Rows.Count + 1)].Style.Border.BorderAround(OfficeOpenXml.Style.ExcelBorderStyle.Thin, System.Drawing.Color.Black);
worksheet.Cells["A1:L" + (ds.Tables[i].Rows.Count + 1)].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
worksheet.Cells["A1:L" + (ds.Tables[i].Rows.Count + 1)].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
}
}
catch (Exception ex)
{
throw ex;
}
package.Save();
ms.Position = 0;
return ms;
}
i need to set the format of column B as text. i gave the range specifically for column B, but once the excel is generated this formatting is applied for all other columns. Please help me solve this... thanks in advance.