简体   繁体   English

C#Aspose.Cells将数据设置为具有格式的Excel文件

[英]C# Aspose.Cells set data to Excel file with format

I have search around the web but could not find any solution. 我在网上搜索,但找不到任何解决方案。 I have a Excel file used as a template, with a area to parse data from DB. 我有一个Excel文件用作模板,并具有从DB解析数据的区域。 Among the columns of the Excel file, there is a number column with a custom format. 在Excel文件的各列中,有一个具有自定义格式的数字列。 My code (C#) of setting data to Excel file is as below: 我将数据设置为Excel文件的代码(C#)如下:

Aspose.Cells.Worksheet ws;
Aspose.Cells.Range rg;
...
rg = ws.Cells.CreateRange("A1", "M10");
var setData = genDataArray(dt); //this function convert Datatable to 2D object array
rg.Value = setData;

Another attempt is also a failure: 另一个尝试也是失败的:

ws.Cells.ImportDataTable(dt, false, "A1");

The code works fine. 该代码工作正常。 Data is set correctly to the Excel file, but without any format of the Excel file. 数据已正确设置为Excel文件,但没有任何格式的Excel文件。

When I click the cells of Excel file (after the process), and press Enter, the value is formatted with the style I previously set in the Excel file. 当我单击Excel文件的单元格(此过程之后),然后按Enter键时,该值将使用我先前在Excel文件中设置的样式进行格式化。

How can i make the Aspose to apply the format I set in the Excel file? 如何使Aspose应用我在Excel文件中设置的格式?

Thank in advance. 预先感谢。

After varous attempts, I finally found an solution. 经过各种尝试,我终于找到了解决方案。

The number column of inputted DataTable have data type of String, so i guess Aspose set it to the Excel file as Text value. 输入的DataTable的number列的数据类型为String,所以我猜Aspose将其设置为Excel文件作为Text值。

After changing it to Decimal type, the value is correctly formatted. 将其更改为十进制类型后,该值将正确格式化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM