使用 Aspose 更改 excel 单元格的数据类型?

Change data type of excel Cell using Aspose?

我们将在具有货币类型的 Excel 单元格中显示带有货币符号 ("TRL") 的数据,然后我们使用此数据 "TRL 100.00" 编写 excel 单元格,然后尽管我们使用 (styleFlag's 属性 NumberFormat=true)

更改了特定单元格的格式,但单元格自动转换为通用类型而不是货币类型

请查看以下示例代码、其注释和显示输出 Excel 文件 的屏幕截图。该代码首先使用 TRL 货币格式 格式化 单元格 A1。然后它使用 TRL 货币格式 .

格式化 整个列 C

C#

//Create workbook
Workbook wb = new Workbook();

//Access first worksheet
Worksheet ws = wb.Worksheets[0];

//Add some value in cell A1
Cell cell = ws.Cells["A1"];
cell.PutValue(22);

//Format cell A1 with TRL formatting
Style st = cell.GetStyle();
st.Custom = "[$TRL]\ #,##0.00";
cell.SetStyle(st);

//Make the entire column C with TRL formatting
StyleFlag flg = new StyleFlag();
flg.NumberFormat = true;
ws.Cells.Columns[2].ApplyStyle(st, flg);

//Now enter data in column C cells
ws.Cells["C5"].PutValue(31);
ws.Cells["C6"].PutValue(32);
ws.Cells["C7"].PutValue(33);
ws.Cells["C8"].PutValue(34);

//Save the workbook
wb.Save("output.xlsx");

截图:

更新-我

显示以下行结果的屏幕截图。

st.Custom = "[$฿-41E]#,##0.00";

注意:我在 Aspose 担任开发人员布道师

最简单的方法是为单元格设置样式编号。

//,234.56
ws.Cells[1,1].Value = 1234.56;
var style=ws.Cells[1.1].GetStyle();
style.Number=5; // 5 is build-in style for currency
ws.Cells[1,1].SetStyle(style);

您可以在此处找到所有 build-in 样式:https://docs.aspose.com/cells/net/list-of-supported-number-formats/