zoukankan      html  css  js  c++  java
  • (原創)C#使用QueryTables導出到Excel

     

    private void ExortToExcel1(string strSql)

                         {

                                    string ExportPath = Request.PhysicalApplicationPath + "Temp\\";

                                    string strFileName = Session.SessionID+DateTime.Now.ToString("yyyyMMdd hhmmss")+".xls";

                                    //新建一Excel應用程式

                                    Missing missing = Missing.Value;

                                    Excel.ApplicationClass objExcel = null;

                                    Excel.Workbooks objBooks = null;

                                    Excel.Workbook objBook = null;

                                    Excel.Worksheet objSheet = null;

                                    try

                                    {

                                               objExcel = new Excel.ApplicationClass();

                                               objExcel.Visible = false;

                                               objBooks = (Excel.Workbooks)objExcel.Workbooks;

                                               objBook = (Excel.Workbook)(objBooks.Add(missing));

                                               objSheet = (Excel.Worksheet)objBook.ActiveSheet;

                                               string conn = "ODBC;DRIVER={Oracle in OraHome92};SERVER=MCM;UID=SFC;PWD=SFC;DBQ=MCM;";

                                               Excel.QueryTable tb = objSheet.QueryTables.Add(conn,objSheet.get_Range("A1", missing),strSql);

                                               //tb.Name = "來自 FCNP 的查詢";

                                               tb.FieldNames = true;

                                               tb.RowNumbers = false;

                                               tb.FillAdjacentFormulas = false;

                                               tb.PreserveFormatting = true;

                                               tb.RefreshOnFileOpen = false;

                                               tb.BackgroundQuery = true;

                                               tb.RefreshStyle = Excel.XlCellInsertionMode.xlInsertDeleteCells;

                                               tb.SavePassword = false;

                                               tb.SaveData = true;

                                               tb.AdjustColumnWidth = true;

                                               tb.RefreshPeriod = 0;

                                               tb.PreserveColumnInfo = true;

                                               tb.BackgroundQuery = false;

                                               tb.Refresh(tb.BackgroundQuery);

                                                        

                                               try

                                               {

                                                         objSheet.PageSetup.LeftMargin = 20;

                                                         objSheet.PageSetup.RightMargin = 20;

                                                         objSheet.PageSetup.TopMargin = 35;

                                                         objSheet.PageSetup.BottomMargin = 15;

                                                         objSheet.PageSetup.HeaderMargin = 7;

                                                         objSheet.PageSetup.FooterMargin = 10;

                                                         objSheet.PageSetup.CenterHorizontally = true;

                                                         objSheet.PageSetup.CenterVertically = false;

                                                         objSheet.PageSetup.Orientation = Excel.XlPageOrientation.xlPortrait;

                                                         objSheet.PageSetup.PaperSize = Excel.XlPaperSize.xlPaperA4;

                                                         objSheet.PageSetup.Zoom = false;

                                                         objSheet.PageSetup.FitToPagesWide = 1;

                                                         objSheet.PageSetup.FitToPagesTall = false;

                                               }

                                               catch

                                               {

                                               }

                                               //關閉Excel

                                               objBook.SaveAs(ExportPath+strFileName, missing, missing, missing, missing, missing,Excel.XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing,missing);

                                               objBook.Close(false,missing,missing);

                                               objBooks.Close();

                                               objExcel.Quit();

                                    }

                                    finally

                                    {                                        

                                               //System.Runtime.InteropServices.Marshal.ReleaseComObject(objRange);

                                               if (!objSheet.Equals(null))

                                                         System.Runtime.InteropServices.Marshal.ReleaseComObject(objSheet);

                                               if (objBook!=null)

                                                         System.Runtime.InteropServices.Marshal.ReleaseComObject(objBook);

                                               if (objBooks!=null)

                                                         System.Runtime.InteropServices.Marshal.ReleaseComObject(objBooks);

                                               if (objExcel!=null)

                                                         System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcel);

                                               GC.Collect();

                                    }

                                    //保存或打開報表

                                    Response.Clear();

                                    Response.Buffer= true;

                                    Response.ContentType = "application/vnd.ms-excel";

                                    Response.ContentEncoding = System.Text.Encoding.UTF8;                               

                                    Response.AppendHeader("content-disposition","attachment;filename="+strFileName+".xls");

                                    Response.Charset = "";

                                    this.EnableViewState = false;

                                    Response.WriteFile(ExportPath+strFileName);

                                    Response.End();     

                         }


    註:必須安裝odbc for oracle 

  • 相关阅读:
    为什么杜蕾斯的文案工资月薪5万?
    在独立音乐上,网易云音乐是如何甩了其他音乐平台几条街?
    两次大战,为什么德国成不了世界霸主呢?
    在大城市打拼的你,是想留下还是想攒够了钱回家?
    生存在互联网公司是种怎样的体验?
    5G为何采纳华为力挺的Polar码?一个通信工程师的大实话
    放下恩怨,曝小米中兴投关键性一票让华为顺利取得5G短码控制权
    中国唯一的科技城
    互联网圈的6大奇葩大产品经理:张小龙不在乎手机碎屏,马化腾让用户一秒变白痴
    为什么说中国快递分两种:一种叫顺丰,一种叫快递?
  • 原文地址:https://www.cnblogs.com/jimeper/p/776743.html
Copyright © 2011-2022 走看看