The efficiency optimization of OWC generation Excel

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.