我已经使用EPPlus将我的数据表从我的网站/数据库下载到Excel表格,第一张图片是我得到的结果。第二张照片是我想要的。
问题:
显然,标题仍然是字符串格式。
因此,80%的邮件不会被隐藏,您必须将列拖出来阅读整个邮件。
public ActionResult ExportData()
{
DataTable dataTable = GetData();
using (ExcelPackage package = new ExcelPackage())
{
var ws = package.Workbook.Worksheets.Add("My Sheet");
//true generates headers
ws.Cells["1:1"].Style.Font.Bold = true;
ws.Cells["A1"].LoadFromDataTable(dataTable, true);
ws.Cells[ws.Dimension.Address].AutoFitColumns();
var stream = new MemoryStream();
package.SaveAs(stream);
string fileName = "Log.xlsx";
string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
stream.Position = 0;
return File(stream, contentType, fileName);
}
}
public DataTable GetData()
{
DataTable dt = new DataTable();
if (ModelState.IsValid)
{
using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MySqlConnection"].ConnectionString))
{
using (SqlCommand comm = conn.CreateCommand())
{
comm.Parameters.AddWithValue("@val1", Session["myID"]);
comm.Parameters.AddWithValue("@val2", "%" + Session["mySearchString"] + "%");
comm.CommandText = "SELECT * FROM dbo.Log WHERE CustomerId = @val1 AND Message LIKE @val2";
try
{
conn.Open();
dt.Load(comm.ExecuteReader());
}
catch (SqlException e)
{
throw new Exception(e.ToString());
}
}
}
}
return dt;
}
答案 0 :(得分:1)
只需要设置Numberformat.Format
字符串即可。像这样:
ws.Column(2).Style.Numberformat.Format = "hh:mm:ss";
如果您想自定义实际内容,那么网上有大量资源,例如http://www.ozgrid.com/Excel/excel-custom-number-formats.htm。或者您可以在Excel中打开它,将格式设置为Custom
并尝试使用字符串。