I have insertted two inputs (e.g. February and March data), however the output only shows the last record which is data in March.
Input:
Date, Market Value
2015/2/28, $123,456,789
2015/3/31, $456,789
Output:
Date, Market Value
2015/3/31, $456,789
How should I show all the input data instead?
int count = 0;
foreach (DataTable dt in ds.Tables)
{
var ws = wb.Worksheets.Add(dt.TableName);
if (count == 0)
{
string[] columnNames = dt.Columns.Cast<DataColumn>().Select(x => x.ColumnName).ToArray();
string[] rowValue = new string[dt.Rows.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
rowValue = dt.Rows[i].ItemArray.Select(x => x.ToString()).ToArray();
}
DateTime dttryparse;
for (int i = 0; i < columnNames.Count(); i++)
{
ws.Cell(1, i + 1).SetValue(columnNames[i]);
}
for (int i = 0; i < rowValue.Count(); i++)
{
if (rowValue[i].Contains("$"))
{
ws.Cell(2, i + 1).SetValue(rowValue[i]);
ws.Cell(2, i + 1).Style.NumberFormat.Format = "$ #,##0.00";
ws.Cell(2, i + 1).DataType = XLCellValues.Text;
}
else if (rowValue[i].Contains("%"))
{
ws.Cell(2, i + 1).SetValue(rowValue[i]);
ws.Cell(2, i + 1).DataType = XLCellValues.Text;
ws.Cell(2, i + 1).Style.NumberFormat.Format = "0.0%";
}
else if (DateTime.TryParse(rowValue[i], out dttryparse))
{
ws.Cell(2, i + 1).SetValue(DateTime.Parse(rowValue[i]));
ws.Cell(2, i + 1).DataType = XLCellValues.DateTime;
}
}
ws.Columns().AdjustToContents();
count += 1;
}
else
{
var rowValue = dt.Rows[0].ItemArray.Select(x => x.ToString()).ToArray();
double dbltryparse = 0;
DateTime dttryparse;
for (int i = 0; i < dt.Rows.Count; i++)
{
if (rowValue[i].Contains("$"))
{
ws.Cell(2, i + 1).SetValue(rowValue[i]);
ws.Cell(2, i + 1).Style.NumberFormat.Format = "$ #,##0.00";
ws.Cell(2, i + 1).DataType = XLCellValues.Text;
}
else if (rowValue[i].Contains("%"))
{
ws.Cell(2, i + 1).SetValue(double.Parse(rowValue[i]));
ws.Cell(2, i + 1).DataType = XLCellValues.Text;
ws.Cell(2, i + 1).Style.NumberFormat.Format = "0.0%";
}
else if (DateTime.TryParse(rowValue[i], out dttryparse))
{
ws.Cell(2, i + 1).SetValue(DateTime.Parse(rowValue[i]));
ws.Cell(2, i + 1).DataType = XLCellValues.DateTime;
}
}
ws.Columns().AdjustToContents();
}
}