Pre-optimized code, importing 1000 of data from a DataTable takes almost 10 minutes and sometimes times out or errors:
public void WriteDataToSpreadsheet2 (Spreadsheetclass p_spreadsheet, DataTable p_dt,
int p_irow, int p_icol , bool p_bwithcaption, int p_istaredrecord,
int p_ilimited, int[] p_itextcolumns)
{
Worksheet f_sheet = P_ Spreadsheet. ActiveSheet;
int f_irowcount = 0;
int f_itotalrow = 0;
int f_imaxrow = 65535;
Int F_irow, F_icol, F_idtcolumn;
F_irow = P_irow;
F_icol = P_icol;
//Use title
if (p_bwithcaption = = True)
{
foreach (DataColumn f_col in P_dt. Columns)
{
F_sheet. Cells[f_irow, F_icol] = F_col. Caption;
f_icol++;
}
f_irow++;
f_itotalrow++;
F_icol = p_icol;
foreach (DataRow f_row in P_dt. Rows)
{
F_idtcolumn = 0;
foreach (DataColumn f_col in P_dt. Columns)
{
if (F_row[f_col]!= dbnull.value)
{
//Note that this is a column-by-column assignment
if (Arr Ay. IndexOf (P_itextcolumns,F_idtcolumn) >-1)
F_sheet. Cells[f_irow, F_icol] = "'" + f_row[f_col]. ToString (); Cast to String
else
F_sheet. Cells[f_irow, F_icol] = F_row[f_col];
}
f_idtcolumn++;
f_icol++;
}
f_irowcount++;
f_itotalrow++;
if (f_itotalrow >= f_imaxrow)
break;
if (p_ilimited > 0)
{
if (f_irowcount >= p_ilimited)
break;
}
f_irow++;
F_icol = P_icol;
}
return;
}