数字格式问题,/导致我的数字(具有文本格式)在excel中转​​换为日期 [英] problem with number format , / cause my number (which has text format) convert to date in excel

查看:131
本文介绍了数字格式问题,/导致我的数字(具有文本格式)在excel中转​​换为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Excel中为ex编写一个单元格:3/4,2/4
但是excel将3/4转换为日期4月4日,然后我将其添加到我的代码中
m_objRange.NumberFormat ="@";
然后变成40606这样的东西.
我不知道该怎么做才能将数字精确地保持在excel中的3/4.

I want to write and a cell in excel for ex : 3/4 , 2/4
But excel convert the 3/4 to date 4-Mar then i added this to my code
m_objRange.NumberFormat = "@";
then it became 40606 something like this .
I don''t know what should i do to keep the number exactly 3/4 in the excel.

foreach (DataTable tableToWrite in TablesToWrite.Tables)
           {
               m_objSheet = (EXCEL._Worksheet)(m_objSheets.Add());
               m_objSheet.Name = tableToWrite.TableName;
               m_objRange = m_objSheet.get_Range("A1", m_objOpt);
               m_objRange = m_objRange.get_Resize(tableToWrite.Rows.Count + 1, tableToWrite.Columns.Count);
               m_objRange.Value2 = TableToObject(tableToWrite);
               string _acct = "@";
               m_objRange.NumberFormat = _acct;
               if (FillColorToEnd)
                   m_objRange = m_objRange.get_Resize(1048576, tableToWrite.Columns.Count);

               m_objSheet.ListObjects.AddEx(EXCEL.XlListObjectSourceType.xlSrcRange, m_objRange, Type.Missing, EXCEL.XlYesNoGuess.xlYes, Type.Missing, tableStyle.ToString());
               m_objSheet.Columns.AutoFit();

               m_objExcel.ActiveWindow.Zoom = 100;
               m_objExcel.ActiveWindow.DisplayGridlines = false;
               m_objExcel.ActiveWindow.WindowState = EXCEL.XlWindowState.xlMaximized;
           }



[OP的更新]
我发现了)多数民众赞成在这很有趣,但是值得写.
只需在数字
之前加一个'<-撇号 您要添加到excel
例如:而不是3/4,您应该写''3/4,然后没错.
[/update]



[update from OP]
I find it out ) thats funny but its worth to write .
that just need to add a '' <-- apostrophe before the number which
you want add to excel
ex : instead of 3/4 u should write ''3/4 and then thats right .
[/update]

推荐答案

3/4不是数字.它可以是一个小数,等于0.75,也可以是一个公式,三除四"或仅一个月的日期. Excel看着它,说:这不是数字,这不是公式(因为它不是以"="开头),我对分数一无所知,这必须是一个日期.

使其保持为3/4的唯一方法是让Excel将其视为字符串-但这也不是数字.
3/4 is not a number. It could be a fraction, equivelant to 0.75, it could be a formule, "divide three by four", or a month only date. Excel looks at it, and says: this is not a number, this is not a formula (because it doesn''t start with ''=''), I known nothing about fractions, this must be a date.

The only way to keep it as 3/4 would be to get Excel to treat it as a string - but that isn''t a number either.


我认为您正在构建一个小型的自己的解析器,带有@或/等符号,用于定义自己的操作.
在C#中实现代码以解析这些符号,将传递的数据更改为对您有意义的信息(例如3/4变成.75等),然后将其发送给excel.
I think you are building a mini-parser of your own with symbols such @ or / defining your own operations.
Implement code in C# to parse these symbols, change the data passed along into meaningful information for you (e.g. 3/4 becomes .75 etc), and then send this over to excel.


这篇关于数字格式问题,/导致我的数字(具有文本格式)在excel中转​​换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆