I want to convert date to string in excel, i have similiar problem to this :
Convert date field into text in Excel
But I need to implement this in C# project, any idea how to achieve that ?
I had numbers in format like this : "1-2".
I also get strange number like 40530 after formating whole excel file to text, I think that's the number of days from year 1900.
EDIT:
I didn't mention that I'm reading data from .xml file, then fill excel file with this data, I had some columns that I fill with text like "1-2", but when I open excel it's shows as 2 January (excel changes it to date automatically).
SOLUTION
Maybe someone will use it:
if (value != null)
{
if (value.Contains("-") && value.Length == 3) // cause my value = "1-2"
{
value = "'" + value; // addin ' to value
}
} //now value ="'1-2"
Where value is my cell that I'm writing to excel. The " ' " sign will guarantee that the value "1-2" will be displayed as text, not as date.
Related
I am looking to format a value in a datagridview.
These values are a string containing a decimal number. (Like "3000"
I want to display it with a thousand separator (space), like this: "3,000".
I know it can be done by assigning format of defaultcellstyle using format like "N2" for example, this works with decimal column type but
I'm using a string column type in my datagridview to handle some exception(displaying "-" instead of "0" to simplify users view)
I tried differents cell style format and nothing changed in the display.
do i need to change the column type of my datagridview or it can be done without too much code ?
Thanks for all reply,
Tristan
i just made it working as i expected. i did the commented things first and it just showed errors, so i tried the second way (simpliest) that is not commented.
string value = cell.Value.ToString();
//NumberFormatInfo nfi =(NumberFormatInfo)CultureInfo.InvariantCulture.NumberFormat.Clone();
//nfi.NumberGroupSeparator = " ";
//string formatted = double.Parse(value).ToString("n", nfi);
//cell.Value = formatted.Replace(".00","");// 12 345.00
string formatted = double.Parse(value).ToString("# ### ###");
cell.Value = formatted;
I'm trying to read an Excel document with a program that I've written and based off the read information, changes certain fields on my UI and I'm running into an interesting problem.
One of my columns has the potential to hold 4 different types of data. Each cell in the row might contain a number, a number stored as text, text, or nothing.
This is the section of code I'm using to read this column:
try
{
string dtTerritory = dt.Rows[0].Field<string>("Territory");
cmbTerritory.SelectedItem = dtTerritory;
territory = dtTerritory;
}
catch
{
double dtTerritory = dt.Rows[0].Field<double>("Territory");
cmbTerritory.SelectedItem = dtTerritory.ToString();
territory = dtTerritory.ToString();
}
The issue I am running into is reading a number stored as text, and the issue is that, when the numbers are stored as text on the Excel sheet, it is always stored as 3 characters. So if the number is 3, it is stored as "3 " (2 spaces after the 3) if it is 46, it is stored as "46 " (1 space after the 46).
So I'm essentially looking for the best solution on how I can have my program ignore the spaces that occur after the number. Any ideas?
I'd just read the data as text and then do processing on it to see if you can convert it to a number or not.
So in the first instance you need to trim the text:
dtTerritory = dtTerritory.Trim();
Then check to see if it's then an empty string:
if (!string.IsEmptyOrWhileSpace(dtTerritory))
{
// Process further
}
Then your further processing is to try to parse the text:
double value;
if (double.TryParse(dtTerritory, out value))
{
// do your double value handling here.
}
I'm trying to create an Excel spreadsheet in my web application using a tab-delimited text file as the data source. The code that loads my data looks like this:
// Load the data into the cells
Int32 rowIdx = 1;
foreach (String line in tab.Lines)
{
String[] cellTexts = line.Split(TAB);
Int32 colIdx = 1;
foreach (String cellText in cellTexts)
{
sheet.Cells[rowIdx, colIdx].Value = cellText;
colIdx++;
}
rowIdx++;
}
That seems to work fine. Later, however, I add a NumberFormat of "mm/dd/yyyy" to the cells:
range.Style.Numberformat.Format = "mm/dd/yyyy";
However, this doesn't change the display of the data in the cells. (The dates look like 5/1/15 or 12/31/15 in the original text file, and remain that way after the format is applied.
I am pretty sure that this because I've put a text value into the cell. (While it looks like a date, it's still just a string of characters.) But from my reading, I need to put a double into the cell to meet Excel's expectation that dates are stored as a double. Because the cell contains a string and not a double, the format string isn't applied, leaving the original, unformatted text.
I want to add some code to
Check the type of data in each cell in the range to which I apply a
date format.
If it's not a double, attempt to convert it to a date.
If the date conversion is successful, then convert the .NET date to an OADate and put it back into the cell.
My question is: Is this the best (or at least a reasonable) approach, and if so, how do I do that?
This code doesn't work:
foreach (OfficeOpenXml.ExcelRangeBase oneCell in range)
{
if (typeof(oneCell.Value) == "System.String")
{
// date manipulations here
}
}
The red line appears under oneCell in the typeof(oneCell.Value) call with the message "The type or namespace 'oneCell' could not be found. (Are you missing a using directive or an assembly reference?)"
Note that I can't know in advance where the date fields will be because both the data and the cell formats are provided from an external source. (The external cell formats do indicate when the format being applied is for a date format as opposed to a regular number format or a string.)
As #mason suggested, I'm posting the code I used to get around this problem.
(I didn't get an answer to my original question, which is how to iterate cells in a range and check the data type of each cell's content, but with this solution, I no longer need to do that.)
Instead, I modified the loop that loads the data from the tab-delimited text file to use some TryParse() calls to detect dates, numbers, or regular text data, and then load the appropriately typed data into the cell. Note how it checks for a leading single quote character to suppress the data typing if the cell is actually text, but looks like a number or a date:
// Load the data into the cells
Int32 rowIdx = 1;
foreach (String line in tab.Lines)
{
String[] cellTexts = line.Split(TAB);
Int32 colIdx = 1;
foreach (String cellText in cellTexts)
{
DateTime dateValue;
Double doubleValue;
if(cellText.StartsWith("'"))
{
sheet.Cells[rowIdx, colIdx].Value = cellText.Substring(1);
}
else if(DateTime.TryParse(cellText,out dateValue))
{
sheet.Cells[rowIdx, colIdx].Value = dateValue;
}
else if (Double.TryParse(cellText, out doubleValue))
{
sheet.Cells[rowIdx, colIdx].Value = doubleValue;
}
else
{
sheet.Cells[rowIdx, colIdx].Value = cellText;
}
colIdx++;
}
rowIdx++;
}
With the data typed appropriately in the cells, the formats have the desired effect.
I have been using the following code:
private void InsertDate(string dstCoordinates)
{
Range dstRange = worksheet.get_Range(dstCoordinates);
dstRange.Formula = "=TODAY()";
dstRange.Locked = true;
}
Which re-evaluates the date every time the spreadsheet file is opened.
But now I need the date to be evaluated once, when it is inserted in the cell. From that moment on, the value should remain constant.
That value seems to be stored internally as double.
Perhaps what I need is a function as follows:
string today = ExcelEvaluate("=TODAY()");
dstRange.value2 = today;
Is there such facility?
TIA
Like Corak wrote you can probably use ToOADate(). You need to set the cell format to date after it, so that it doesn't display as a double. The TODAY() function does that automatically.
Here's the code:
dstRange.Value2 = DateTime.Now.ToOADate();
dstRange.NumberFormat = "m/d/yyyy"
I'm writing an application that's supposed to export data from a map application.
This application is using Silverlight, and to facilitate export to Excel I am using this library.
All of the data is represented in strings by default. When I write to the spreadsheet, I try to parse each string to see which type it is:
string str = kvp.Value;
int i = 0;
long l = 0;
decimal dec = 0;
DateTime dt;
if (int.TryParse(str, out i))
doc.Workbook.Sheets[0].Sheet.Rows[r].Cells[c].SetValue(i);
else if (decimal.TryParse(str, out dec))
doc.Workbook.Sheets[0].Sheet.Rows[r].Cells[c].SetValue(dec);
else if (long.TryParse(str, out l))
doc.Workbook.Sheets[0].Sheet.Rows[r].Cells[c].SetValue(l);
else if (DateTime.TryParse(str, out dt))
doc.Workbook.Sheets[0].Sheet.Rows[r].Cells[c].SetValue(dt);
else
doc.Workbook.Sheets[0].Sheet.Rows[r].Cells[c].SetValue(str);
This works great, except for DateTime and when I try to parse a social security number, which in my case is 12 characters long.
The social security number is parsed as a decimal number, and is displayed in scientific form in Excel. From what I've gathered through reading it seems like a limitation in Excel. If I mark the cell however, I see the correct number in the top bar where you can write formulas. I've solved this problem so far by simply putting this number as a string and letting the end user handle it for now, but I'd really like for it to be a number in the finished document. Is this possible?
What really boggles my mind though, is the DateTime. The format of the date comes like this from the application: 10/15/2013 1:10:00 AM.
It looks like this in the Excel file: 2455075.
I checked the source code for the date formatting but I don't seem to be adept enough to see if there is anything wrong in it. For anyone intresed, you can check it out here.
The SetValue-function is supposed to identify the following types automatically:
bool
DateTime
decimal
Exception
SharedStringDefinition
string
I apologize for the long post. It boils down to these questions:
Can I make Excel handle long numbers without scientific notation programatically?
Why is the DateTime being outputed to such a weird format?
To be set Cell Value in Date format you have to convert DateTime to OLE Automation Date. Also you can create more clear method for writing cell values. Somthing like this:
public bool UpdateValue(WorkbookPart wbPart, string sheetName, string addressName, string value,
UInt32Value styleIndex, CellValues dataType)
{
// Assume failure.
bool updated = false;
Sheet sheet = wbPart.Workbook.Descendants<Sheet>().Where(
(s) => s.Name == sheetName).FirstOrDefault();
if (sheet != null)
{
Worksheet ws = ((WorksheetPart)(wbPart.GetPartById(sheet.Id))).Worksheet;
Cell cell = InsertCellInWorksheet(ws, addressName);
if (dataType == CellValues.SharedString)
{
// Either retrieve the index of an existing string,
// or insert the string into the shared string table
// and get the index of the new item.
int stringIndex = InsertSharedStringItem(wbPart, value);
cell.CellValue = new CellValue(stringIndex.ToString());
}
else
{
cell.CellValue = new CellValue(value);
}
cell.DataType = new EnumValue<CellValues>(dataType);
if (styleIndex > 0)
cell.StyleIndex = styleIndex;
// Save the worksheet.
ws.Save();
updated = true;
}
return updated;
}
Then call this method like this (first call is for String second is for DateTime):
UpdateValue(workbookPart, wsName, "D14", "Some text", 0, CellValues.String);
UpdateValue(workbookPart, wsName, "H13", DateTime.Parse("2013-11-01").ToOADate().ToString(CultureInfo.InvariantCulture), 0, CellValues.Date);