Better yet, make a MIME Type
so that when users double click ".por" files it triggers an exe to tell users what to do. Rather than open it in Excel. Users wont know that por files are Excel files if you dont tell them.
Since xls's are binary files - if the extension of the files is changed users won't
know the MIME type unless they used something like this:
Determining MIME Type of MAC upload stream file
Regarding taking a DataTable and row by row writing this out to the Excel file.
You could easily take the data into a 2d array:
string[,] TwoDimensional = new string[dt.Rows.Count, dt.Columns.Count];
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; i++)
{
TwoDimensional[i, j] = dt.Rows[i][j].ToString();
}
}
And in one swoop set them in Excel:
using (var targetRangeHeader = _excelApp.Range["A1"].WithComCleanup())
using (var targetRangeFirstDataCell = targetRangeHeader.Resource.Offset[1, 0].WithComCleanup())
using (var targetRange = targetRangeFirstDataCell.Resource.Resize[LengthOfArray, 1].WithComCleanup())
{
....
targetRange.Resource.Value2 = TwoDimensional;
Note I'm using VSTO Contrib for the finalistic determination - ie the using
statements.
Dont forget this (record the current calculation mode and restore it rather than assuming the user is using automatic):
public static void TurnOffApplicationSettings(Excel.Application xlApp)
{
xlApp.ScreenUpdating = false;
xlApp.DisplayAlerts = false;
xlApp.Calculation = XlCalculation.xlCalculationManual;
xlApp.UserControl = false;
xlApp.EnableEvents = false;
}
public static void TurnOnApplicationSettings(Excel.Application xlApp)
{
xlApp.ScreenUpdating = true;
xlApp.DisplayAlerts = true;
xlApp.Calculation = XlCalculation.xlCalculationAutomatic;
xlApp.UserControl = true;
xlApp.EnableEvents = true;
}