使用 Aspose.cells 导入时无法使用 Excel 中的公式

Can't able to use Formula in Excel while using Aspose.cells to import

我一直在使用 Aspose.cells 导入 Excel sheet 数据。 Excel sheet 包含一个 Salary 列,我为其分配 Decimal 值。即使我从数据库中分配十进制值,列也被分配为字符串格式。 一旦我双击每个单元格,它就会转换为数字格式。 因此,我无法使用像“=SUM(M1:M20)”这样的公式。

我正在使用以下函数使用 Aspose.cells 下载 excel 受保护的无效下载 Excel(字符串 psPlanNo,字符串 psSuffix) { 尝试 { DataTable dtExcelData = GetDataTableValue(); dtExcelData.TableName = psPlanNo + "Template";

        var workbook = new Workbook();
        var worksheet = workbook.Worksheets[0];
        worksheet.Cells.ImportDataTable(dtExcelData, true, "A1");
        worksheet.AutoFilter.Range = worksheet.Cells.FirstCell.Name + ":" + worksheet.Cells.LastCell.Name;
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "";
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.AddHeader("content-disposition", "attachment;filename=" + psPlanNo + psSuffix + ".xlsx");
        worksheet.AutoFitColumns();

        Aspose.Cells.Style style = worksheet.Cells["A1"].GetStyle();
        style.ForegroundThemeColor = new ThemeColor(ThemeColorType.Accent1, 0);
        style.Font.Color = Color.White;
        style.Pattern = BackgroundType.Solid;

        for (int lnColumn = 0; lnColumn <= worksheet.Cells.MaxColumn; lnColumn++)
            worksheet.Cells[0, lnColumn].SetStyle(style);

        Cells cells = worksheet.Cells;
        Aspose.Cells.Style fontStyle = new Aspose.Cells.Style();
        Aspose.Cells.Style stylefont = workbook.Styles[workbook.Styles.Add()];
        stylefont.Font.Name = "Calibri";
        stylefont.Font.Size = 12;
        StyleFlag flag = new StyleFlag();
        flag.FontName = true;
        flag.FontSize = true;
        cells.ApplyStyle(stylefont, flag);

        using (MemoryStream memoryStream = new MemoryStream())
        {
            workbook.Save(memoryStream, SaveFormat.Xlsx);
            memoryStream.WriteTo(Response.OutputStream);
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.SuppressContent = true;
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }
    }
    catch (SqlException sql)
    {

        DbException(sql, MethodBase.GetCurrentMethod().Name);
    }
    catch (Exception ex)
    {
        GenericException(ex, MethodBase.GetCurrentMethod().Name);
    }
}

有人能解决这个问题吗?

提前致谢

我找到了解决方案,更改这行代码

worksheet.Cells.ImportDataTable(dtExcelData, true, "A1");

至:

worksheet.Cells.ImportDataTable(dtExcelData, true, 0, 0, true, true); 

(注意:最后一个布尔参数"convertStringToNumber"要设置为true)

现在一切正常..:)