Public bool excelout (string filename, string sql1, string sql2)
{
Try
{
// Start a new workbook in Excel.
M_objexcel = new excel. Application ();
M_objbooks = (Excel. workbooks) m_objexcel.workbooks;
M_objbook = (Excel. _ workbook) (m_objbooks.add (m_objopt ));
// Create a querytable that starts at cell A1.
M_objsheets = (Excel. Sheets) m_objbook.worksheets;
M_objsheet = (Excel. _ worksheet) (m_objsheets.get_item (1 ));
M_objrange = m_objsheet.get_range ("A1", m_objopt );
M_objqrytables = m_objsheet.querytables;
M_objqrytable = (Excel. _ querytable) m_objqrytables.add (
Conn, m_objrange, sql1 );
M_objqrytable.refreshstyle = excel. xlcellinsertionmode. xlinsertentirerows;
M_objqrytable.refresh (false );
M_objsheets = (Excel. Sheets) m_objbook.worksheets;
M_objsheet = (Excel. _ worksheet) (m_objsheets.get_item (2 ));
M_objrange = m_objsheet.get_range ("A1", m_objopt );
M_objqrytables = m_objsheet.querytables;
M_objqrytable = (Excel. _ querytable) m_objqrytables.add (
Conn, m_objrange, sql2 );
M_objqrytable.refreshstyle = excel. xlcellinsertionmode. xlinsertentirerows;
M_objqrytable.refresh (false );
// Save the workbook and quit excel.
M_objbook.saveas (filename, m_objopt, m_objopt,
M_objopt, Excel. xlsaveasaccessmode. xlnochange, m_objopt, m_objopt,
M_objopt, m_objopt );
// M_objbook.close (false, m_objopt, m_objopt );
// M_objexcel.quit ();
Return true;
}
Catch
{
Return false;
}
Finally
{
System. runtime. interopservices. Marshal. releasecomobject (object) m_objqrytable );
M_objqrytable = NULL;
System. runtime. interopservices. Marshal. releasecomobject (object) m_objqrytables );
M_objqrytables = NULL;
System. runtime. interopservices. Marshal. releasecomobject (object) m_objrange );
M_objrange = NULL;
System. runtime. interopservices. Marshal. releasecomobject (object) m_objsheet );
M_objsheet = NULL;
System. runtime. interopservices. Marshal. releasecomobject (object) m_objsheets );
M_objsheets = NULL;
M_objbook.close (false, m_objopt, m_objopt );
System. runtime. interopservices. Marshal. releasecomobject (object) m_objbook );
M_objbook = NULL;
M_objbooks.close ();
System. runtime. interopservices. Marshal. releasecomobject (object) m_objbooks );
M_objbooks = NULL;
M_objexcel.quit ();
System. runtime. interopservices. Marshal. releasecomobject (object) m_objexcel );
M_objexcel = NULL;
GC. Collect ();
GC. waitforpendingfinalizers ();
}
}