数字应格式化为excel中的数字,而不是文本C#

时间:2015-06-29 19:09:44

标签: c# excel number-formatting

任何人都可以帮我导出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
}

1 个答案:

答案 0 :(得分:0)

您可以使用OpenXML包装器来帮助自己。 Rigth现在我正在使用SpreadLight。它处理与excel的大多数交互,你可以深入了解它的高级功能。