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

数字应在excel中格式化为数字,而不是文本C#,c#,excel,number-formatting,C#,Excel,Number Formatting,有人能帮我在excel中导出数字而不是文本吗?还有,我想冻结第一行。这是我用来导出到excel的函数。此外,我希望行采用其他颜色: public FilePathResult ExportToExcel(){ log.Info("Action ExportToExcel for user " + User.Identity.GetUserName()); List<string> dataTypes = new List<string>(); Na

有人能帮我在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
}
public文件路径结果ExportToExcel(){
log.Info(“用户的操作ExportToExcel”+user.Identity.GetUserName());
列表数据类型=新列表();
NameValueCollection参数=Request.Params;
字符串查询=QueryUtility.ConstructionQueryForExcel(参数);
数据集ds=新数据集();
DataTable dt=DaoUtilitySingleton.Instance.ExecuteQueryToGetDataTable(查询);
ds.Tables.Add(dt);
日志信息(“导出查询”+查询);
log.Info(“列名:”+参数[“columnNames”]);
字符串guid=guid.NewGuid().ToString();
字符串文件名=guid;
fileName+=System.DateTime.Now.Ticks.ToString();
文件名+=“.xlsx”;
字符串destinationPath=Path.Combine(
Server.MapPath(“~/App_Data/ExcelExports”);
如果(!Directory.Exists(destinationPath))
{
log.Info(“目录不存在,先创建目录”);
Directory.CreateDirectory(destinationPath);
}
destinationPath=Path.Combine(destinationPath,文件名);
log.Info(“Excel文件将另存为”+destinationPath);
//目前,此函数返回的数据是文本而不是数字
尝试
{
如果(!(新建ExcelUtility()).CreateExcelDocumentFromDataset(ds,
目的地路径)
{
log.Error(“无法创建Excel文件”);
返回null;
}
}
捕获(例外情况除外)
{
GeneralUtility.LogException(日志,
“无法创建Excel文件”,
ex);
投掷;
}
log.Info(“文件创建成功”);
FilePathResult file=file(destinationPath,“application/vnd.openxmlformats of icedocument.spreadsheetml.sheet”,Properties.Instance.FileForExcel);
返回文件;
//返回文件
}

您可以使用OpenXML包装器。现在我用的是Rigth。它处理与excel的大部分交互,您可以深入示例了解它的高级功能