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, 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();
}
}