数字在excel中应该格式化为数字,而不是文本C#
本文关键字:数字 文本 格式化 excel | 更新日期: 2023-09-27 18:00:27
有人能帮我在excel中导出数字而不是文本吗?另外,我想冻结第一排。这是我用来导出到excel的函数。此外,我希望行是交替的颜色:
public FilePathResult ExportToExcel(){
log.Info("Action ExportToExcel for user " + User.Identity.GetUserName());
List<string> dataTypes = new List<string>();
NameValueCollection parameters = Request.Params;
string query = QueryUtility.ConstructQueryForExcel(parameters);
DataSet ds = new DataSet();
DataTable dt = DaoUtilitySingleton.Instance.ExecuteQueryToGetDataTable(query);
ds.Tables.Add(dt);
log.Info("Query for export " + query);
log.Info("Column names : " + parameters["columnNames"]);
string guid = Guid.NewGuid().ToString();
string fileName = guid;
fileName += System.DateTime.Now.Ticks.ToString();
fileName += ".xlsx";
string destinationPath = Path.Combine(
Server.MapPath("~/App_Data/ExcelExports"));
if (!Directory.Exists(destinationPath))
{
log.Info("Directory does not exist. Creating Directory first");
Directory.CreateDirectory(destinationPath);
}
destinationPath = Path.Combine(destinationPath, fileName);
log.Info("Excel file will be saved as " + destinationPath);
//currently , this function return data which is in text and not number
try
{
if (!(new ExcelUtility()).CreateExcelDocumentFromDataset(ds,
destinationPath))
{
log.Error("Excel file could not be created");
return null;
}
}
catch (Exception ex)
{
GeneralUtility.LogException(log,
"Excel file could not be created",
ex);
throw;
}
log.Info("FIle created successfully");
FilePathResult file = File(destinationPath, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", Properties.Instance.FileForExcel);
return file;
//return the file
}
您可以使用OpenXML包装器来帮助自己。Rigth现在我正在使用SpreadLight。它处理了与excel的大部分交互,您可以深入了解它的高级功能的示例。