【问题标题】:Numbers should be formatted as number in excel, not as text C#数字应格式化为 excel 中的数字,而不是文本 C#
【发布时间】:2015-06-29 19:09:43
【问题描述】:

谁能帮我在 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
}

【问题讨论】:

    标签: c# excel number-formatting


    【解决方案1】:

    您可以使用 OpenXML 包装器来帮助自己。现在我正在使用SpreadLight。它处理与 excel 的大部分交互,您可以深入研究其高级功能的示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多