I have tried to make my cells numeric but I haven't been able to. My data comes from datagridview. I am out of ideas, so can you help me and tell me how can I set cells except first column and first row to numeric format, so excel would be able to calculate
with the numbers.
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Microsoft Excel 2007 XML*.xlsx";
saveFileDialog1.Title = "Save an Excel File";
saveFileDialog1.ShowDialog();
if (saveFileDialog1.FileName != "")
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.TableName = "Prodcuts";
foreach (DataGridViewColumn col in csv_data.Columns)
{
dt.Columns.Add(col.HeaderText);
}
foreach (DataGridViewRow gridRow in csv_data.Rows)
{
if (gridRow.IsNewRow)
continue;
DataRow dtRow = dt.NewRow();
for (int i1 = 0; i1 < csv_data.Columns.Count; i1++)
{
dtRow[i1] = (gridRow.Cells[i1].Value == null ? DBNull.Value : gridRow.Cells[i1].Value);
}
dt.Rows.Add(dtRow);
}
ds.Tables.Add(dt);
var newFile = new FileInfo(saveFileDialog1.FileName);
if (newFile.Exists)
{
newFile.Delete();
}
var table = dt;
using (var package = new ExcelPackage(newFile))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Packages");
worksheet.Cells.LoadFromDataTable(table, true);
package.Save();
}
}
