[英]Excel interop - how to stop number (stored as text) being “evaluated”
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. 我想知道是否有人遇到以下问题,并对如何解决它有任何想法:我通过Interop将数据从C#应用程序(.NET 3.5)导出到Excel(2003)。 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 eg 000123
也就是说它是一个以0开头的数字,例如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.
Excel并不热衷于此,所以我认为我可以通过将目标单元格设置为常规来绕过它。 When I export to Excel I find 123 stored instead of 000123.
当我导出到Excel时,我发现123存储而不是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会将该数字视为字符串。
I know it's late, but maybe someone need this in the future. 我知道现在已经很晚了,但也许有人在将来需要这个。 It's not really for the performance but you can store it in a two-dimentional array first.
它不是真正的性能,但你可以先将它存储在一个二维数组中。
object[,] Values = new object[iGrid.Rows.Count, IGrid.Columns.Count];
for (int i = 0; i < alllogentry.Rows.Count; i++)
{
for (int j = 0; j < alllogentry.Columns.Count; j++)
{
if (alllogentry.Rows[i].Cells[j].Value != null)
{
Values[i, j] = alllogentry.Rows[i].Cells[j].Value.ToString();
}
else
{
Values[i, j] = " ";
}
}
}
This way number stays number and string stays string. 这样,数字保持数字,字符串保持字符串。
Also pass the information via bulk insert to excel not cell by cell. 同时通过批量插入将信息传递给excel而不是逐个单元格。 That was my code.
那是我的代码。
// Bulk Transfer
String MaxRow = (alllogentry.Rows.Count+6).ToString();
String MaxColumn = ((String)(Convert.ToChar(alllogentry.Columns.Count / 26 + 64).ToString() + Convert.ToChar(alllogentry.Columns.Count % 26 + 64))).Replace('@', ' ').Trim();
String MaxCell = MaxColumn + MaxRow;
//Format
worksheet.get_Range("A1", MaxColumn + "1").Font.Bold = true;
worksheet.get_Range("A1", MaxColumn + "1").VerticalAlignment = XlVAlign.xlVAlignCenter;
// Insert Statement
worksheet.get_Range("A7", MaxCell).Value2 = Values;
I use this macro. 我用这个宏。 It inserts an apostrophe before each value that is numeric in each cell.
它在每个单元格中的数值之前插入一个撇号。
Sub Macro1()
Dim rwIndex As Integer
Dim colIndex As Integer
For rwIndex = 1 To ActiveSheet.UsedRange.Rows.Count
For colIndex = 1 To ActiveSheet.UsedRange.Columns.Count
If IsNumeric(Cells(rwIndex, colIndex).Value) Then _
Cells(rwIndex, colIndex).Value = "'" _
& Cells(rwIndex, colIndex).Value
Next colIndex
Next rwIndex
End Sub
The correct type is not General, because general will try to guess the correct type, which in this case is a numeric, but you have to specify it as text to not truncate the leading zeros. 正确的类型不是常规类型,因为一般会尝试猜测正确的类型,在这种情况下是数字,但您必须将其指定为文本以不截断前导零。
Try the following code: 请尝试以下代码:
Range cell = ActiveWorksheet.Cells[1,1];
//The @ is the indicator for Excel, that the content should be text
const string textIndicator = "@";
//Change the NumberFormat from 'General' to 'Text'
cell.NumberFormat = textIndicator;
//Set the Value
cell.Value2 = "000123";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.