本文介绍了Excel interop - 如何停止数字(存储为文本)被“评估”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人遇到了以下问题,并有任何想法如何解决它:我将数据从C#应用程序(.NET 3.5)导出到Excel(2003)通过Interop。其中一列存储显示为数字的字符串值。也就是说,它是以0开头的数字。 000123



我需要存储完整号码,因为它可能是序列号或类似的东西。 Excel不热衷于这一点,所以我想我可以通过将目标单元格设置为一般来解决它。当我导出到Excel时,我发现123存储而不是000123。



我已经在调试中运行了应用程序,并从监视列表中发现(对于Range范围:

  range.NumberFormat =General`
this.Rows [iGridRow] .Cells [iGridCol] .Value =000123'/ *(datagrid不截断它)* /
range.Value2 = 123.0

尽管我在此之前设置了数字格式,似乎正在处理一个数字:

  range .NumberFormat = sNumberFormat; 
range =(Range)sheet.Cells [iExcelRow,iExcelCol];
range.Value2 = this.Rows [iGridRow] .Cells [iGridCol] .Value.ToString();

任何人都可以帮忙?

解决方案

在数字前添加单个单引号'。然后Excel会将该数字视为字符串。


I was wondering if anyone had come across the following problem and had any ideas on how to resolve it: I'm exporting data from a C# application (.NET 3.5) to Excel (2003) via Interop. One of the columns stores a string value that appears to be numeric. That is to say it is a number that begins with a 0 e.g. 000123

I need the full number to be stored as it may be a serial number or something similar. Excel is not keen on this so I thought I could get around it by setting the destination cell to general. When I export to Excel I find 123 stored instead of 000123.

I've run through the app in debug and from the watchlist I've found that (for "Range range":

range.NumberFormat = "General"`
this.Rows[iGridRow].Cells[iGridCol].Value = "000123" '/* (datagrid is not truncating it)*/
range.Value2 = 123.0

It seems to be getting handled as a number even though I set the numberformat before this point:

range.NumberFormat = sNumberFormat;
range = (Range)sheet.Cells[iExcelRow, iExcelCol];
range.Value2 = this.Rows[iGridRow].Cells[iGridCol].Value.ToString();

Can anyone please help?

解决方案

Add a single apostrophe ' before the number. Excel will then treat the number as a string.

这篇关于Excel interop - 如何停止数字(存储为文本)被“评估”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 11:59