c# - Excel._Worksheet.Columns.Autofit() method is hanging -


my sqldatareader returning ~200 rows of data. half of rows have column contains entire xml document. i'm assuming column causing autofit() method hang. how can go getting either throw exception, or complete successfully. don't care one, need [automated] program finish.

excel.application xl = null;             excel._workbook wb;             excel._worksheet ws;              try             {                 xl = new microsoft.office.interop.excel.application();                 xl.visible = false;                  while (reader.hasrows && !done)                 {                     wb = (excel._workbook)(xl.workbooks.add(excel.xlwbatemplate.xlwbatworksheet));                     ws = (excel._worksheet)wb.activesheet;                      // insert column headers                     (int counter = 0; counter < reader.fieldcount; counter++)                         ws.cells[1, counter + 1] = reader.getname(counter);                      // write data excel file                     while (reader.read())                     {                         (int counter = 1; counter <= reader.fieldcount; counter++)                         {                             // need format column excel doesn't change how looks                             if (report.reportname == "rptaamvanetbatch" && reader.getname(counter - 1) == "correlationid")                                 ws.cells[rowcounter, counter] = string.format("''{0}'", reader.getvalue(counter - 1));                             else                                 ws.cells[rowcounter, counter] = reader.getvalue(counter - 1);                         }                         rowcounter++;                     }                      recordsprocessed = rowcounter - 1;                      // format excel file liking                     ws.get_range(ws.cells[1, 1], ws.cells[rowcounter - 1, reader.fieldcount]);                     ws.columns.autofit(); 

further comment above, using particular sample file created couple of days ago in there 100 rows , in col e, have entire body of several emails (i have put red box protect identity , contents of email)

enter image description here

this simple vba code ran check time takes autofit e column

sub test()     dim starttime string     dim endtime string      starttime =      columns("e:e").entirecolumn.autofit      endtime =      debug.print "the process started @ " & starttime & " , got on @ " & endtime end sub 

screenshot:

enter image description here

your program taking more time because there 200 rows , maybe have more data in each excel cell compared mine.

so solution?

the best solution can think of widen column it's max (254.86) before write huge data it. this

        excel.range rng = ws.get_range("e:e",system.type.missing);          rng.entirecolumn.columnwidth = 254; 

note: don't use autofit column or entire worksheet. if need use autofit break up. example, in case, autofit columns 1 4 , 6 last column


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -