I create a row of data as follows. I look at each element and see if its a datetime format and then set the style to Number=14. The 1st part works fine until I try to add more styles to it.
string[] AryLines = aItem.ToString().Split('\t');
for (int Cellpos = 0; Cellpos < AryLines.GetUpperBound(0); Cellpos++)
{
wrksheet.Cells[importcounter, Cellpos].PutValue(AryLines[Cellpos], true);
// now test the data to see if its a Datetime. Because we need to change cell to display it.
DateTime dtTest;
if (Regex.IsMatch(AryLines[Cellpos], DateStringMatch))
if (DateTime.TryParse(AryLines[Cellpos], out dtTest))
wrksheet.Cells[importcounter, Cellpos].Style.Number = 14;
}
DrawGrid(wrkBook, wrksheet, 1, importcounter, intLastCol);
----
private static void DrawGrid(Workbook wrkBook, Worksheet wrkSheet, int startRow, int endRow, int lastCol)
{
//Create the style object
Style style = wrkBook.Styles[wrkBook.Styles.Add()];
//Sets font attributes
style.Font.IsBold = false;
style.HorizontalAlignment = TextAlignmentType.Center;
style.Font.Name = "Arial";
style.Font.Size = 10;
//Sets borders
style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;DateTime dtTest;
for (int row = startRow; row <= endRow; row++)
{
for (int y = 0; y <= lastCol; y++)
{
wrkSheet.Cells[row, y].Style = style;
// addtional Note:****
}
}
}
------
Now it changes the Style number to General so now instead of 4/13/2010 I get 40281.
I even tried to replace the "addition Note. ****" with the following code. and I get the same result.
if (wrkSheet.Cells[row, y].Value != null)
if (DateTime.TryParse(wrkSheet.Cells[row, y].Value.ToString(), out dtTest))
{
Style aCellStyle = wrkSheet.Cells[row, y].GetStyle();
aCellStyle.Number = 14;
wrkSheet.Cells[row, y].SetStyle(aCellStyle);
}
How do I fix this? I need all the data in a grid.
Ed.