gpt4 book ai didi

C# 在 Excel 文件中将文本序列号作为日期写入

转载 作者:行者123 更新时间:2023-11-30 18:31:03 25 4
gpt4 key购买 nike

我编写了一个解析器,使用 Spire.xls library 从 Excel 工作表中获取一些信息然后将信息写入另一个 Excel 文件。

我遇到了一个奇怪的问题。出于某种原因,该程序正在使用序列号,例如

03-02281 
03-02282
03-01975

并将它们写入Excel工作表中

3/1/2281
3/1/2282
3/1/1975

这只发生在某些值上。

其他如

30-04761
03-00613
03-00614

转录不变。

我检查了 excel 文件,字段设置为 text 格式。因此,它们要么最初以这种方式存储,要么 Excel 将序列号解释为日期。另一种可能性是它不会发生在原始文件中,如果我手动输入正确的值,文本不会自动更正/更改。

有谁知道为什么会发生这种情况以及我如何告诉 Excel 只将这些视为文本而不是其他内容?

虽然我想在每个值的开头附加一个 ',但是这些必须被其他解析器读取,所以这不是最方便的选择。

编辑:这是我为此使用的一些代码,希望它能让你们知道我哪里出错了。

这是添加所有值的代码:

Workbook workbook = new Workbook();
workbook.LoadFromFile(templateExcelFileUri);
Worksheet sheet = workbook.Worksheets[0];

int ColumnIndex = 0; //for the datatable columns iteration
int columnCounter = 1; //for the excel sheet columns iteration
int ColumnsToAdd = 6; //(Seccion, seccion desc, marca, marca desc, **IdArticulo**, articulo desc)

//get the data of the new column
DataColumn DescriptionsDataColumn;
//First, add the suggestions.
for (; ColumnIndex < ColumnsToAdd; ColumnIndex++,
columnCounter++)
{
sheet.InsertColumn(columnCounter);
if(columnCounter==5)
sheet.Columns[5].NumberFormat = "@";// the column with the serial numbers.
DescriptionsDataColumn = AutomatController.DescriptionsTable.Columns[ColumnIndex];
//insert the data into the new column
sheet.InsertDataColumn(DescriptionsDataColumn, true, 2, columnCounter);
}

对于引用,我添加的值的表:

public static void SetDescriptionsTable()
{
DescriptionsTable.Columns.Add("Seccion", typeof(string));
DescriptionsTable.Columns.Add("SeccionDescripcion", typeof(string));
DescriptionsTable.Columns.Add("Marca", typeof(string));
DescriptionsTable.Columns.Add("MarcaDescripcion", typeof(string));
DescriptionsTable.Columns.Add("IdArticulo", typeof(string)); //Serial numbers
DescriptionsTable.Columns.Add("ArticuloDescripcion", typeof(string));
}

感谢您对我的问题和标题的格式进行编辑。我在这里还是有点陌生​​,我正在学习如何做得更好。

最佳答案

某些值未映射到日期的原因是因为它们不属于 MM-dyyyy 格式。例如,没有第 30 个月 (30-02281) 或第 0 天 (03-01975)。

我认为您唯一需要做的就是在通过 API 设置目标列和单元格的值之前设置目标列和单元格的格式。有时,克隆列或单元格会将格式设置默认为“自动”,而 Excel 试图变得过于智能。

如果您可以分享一些代码,社区可能会更准确地诊断问题。

关于C# 在 Excel 文件中将文本序列号作为日期写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20692794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com