问题描述
usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Reflection;namespaceExcelExport{classProgram{staticvoidMain(string[]args){ExportDataToExcel("Provider=SQLOLEDB.1;sever=localhost;uid=sa;password=271121;database=master;","select*fromsysobjects",@"d:11-12-1.xls","sysobjects");}///<summary>///直接导出数据到excel///</summary>///<paramname="connectionString">连接字符串</param>///<paramname="sql">查询语句</param>///<paramname="fileName">文件名</param>///<paramname="sheetName">表名</param>staticvoidExportDataToExcel(stringconnectionString,stringsql,stringfileName,stringsheetName){Excel._Applicationapp=newExcel.ApplicationClass();Excel.Workbookwb=(Excel.WorkbookClass)app.Workbooks.Add(Missing.Value);Excel.Worksheetws=wb.Worksheets.Add(Missing.Value,Missing.Value,Missing.Value,Missing.Value)asExcel.Worksheet;ws.Name=sheetName;try{Excel.QueryTableqt=ws.QueryTables.Add("OLEDB;"+connectionString,ws.get_Range("A1",Missing.Value),sql);qt.Refresh(false);//是否异步查询}catch(Exceptionex){stringstr=ex.Message;}finally{wb.Saved=true;wb.SaveCopyAs(fileName);//保存app.Quit();//关闭进程}}}}麻烦给我解释一下那个函数里面的语句,谢谢(菜)
解决方案
解决方案二:
这有啥好解释的啊!OLEDB去查询excel啊