简体   繁体   English

如何将值插入单元格而不将其转换为所需的数据类型?

[英]How to insert a value into a cell without converting it to the desired data type?

I insert the values ​​obtained from the database into the cells.我将从数据库中获得的值插入到单元格中。 To numeric values ​​and DataTime values, the “'” character is added.数值和数据时间值,添加了“'”字符。

I do not have the opportunity to check each value and bring it to the desired type.我没有机会检查每个值并将其转换为所需的类型。 The data is always different.数据总是不同的。 The cell format is already defined in the cells of the worksheet.单元格格式已在工作表的单元格中定义。

How to insert data without changes, without placing char “'” ?如何在不更改的情况下插入数据,而不放置字符“'”?

Code example:代码示例:

document.SetCellValue(rowIndex, columnIndex, stringValue);

Result:结果:

enter image description here在此处输入图片说明

Thanks for helping me.谢谢你帮助我。

I'm not familiar with spreadsheetlight , but the general principal is that the display format is independent of how the data is stored.我不熟悉spreadsheetlight ,但一般原则是显示格式与数据的存储方式无关。 You need to convert your input to the correct data type.您需要将输入转换为正确的数据类型。 The leading ' can be used to override automatic data conversion if the cell type is general.如果单元格类型为常规,则前导'可用于覆盖自动数据转换。 Assuming the you are using a time based display format try converting your input to DateTime before storing.假设您使用的是基于时间的显示格式,请在存储之前尝试将您的输入转换为DateTime

There are many ways of doing this.有很多方法可以做到这一点。 see here for more information.请参阅此处了解更多信息。

One method is一种方法是

string stringDate = "05/05/2019 00:31:00";
DateTime realDate = Convert.ToDateTime(stringDate );
document.SetCellValue(rowIndex, columnIndex, realDate);

This will then display the data according to the format setting of the cell.这将根据单元格的格式设置显示数据。

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

相关问题 如何将值插入到 DataGridView 单元格中? - how to insert value into DataGridView Cell? 如何将nvarchar值转换为数据类型int - How can I converting the nvarchar value to data type int 将数据类型varchar转换为数字时出错。 我使用了varchar数据类型,并尝试插入18-24值 - Error converting data type varchar to numeric. I used varchar data type and trying to insert 18-24 value 如何根据单元格的值在运行时设置DataGridViewColumn数据类型? - How to set DataGridViewColumn data Type at RunTime based on the cell's value? 如何在MSI数据库中插入“格式”数据类型的值 - How to insert a value of data type 'Formatted' into the MSI database 将十进制数据类型值转换为格式化的字符串 - Converting a decimal data type value to a formatted string 如何从文本框中将数据插入表*单元格* - How insert data to Table * cell * from textbox 在ASP.NET动态数据中的单元格上插入自定义值 - Insert Custom value on Cell in ASP.NET Dynamic Data datagridview单元格的比较值如何为十进制类型 - how compare value of datagridview cell is of decimal type or not 如何使用WPF C#和Ado.Net数据实体将十进制值插入货币数据类型列 - How to insert a decimal value to money data type column with wpf C# and Ado.Net Data Entity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM