乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      asp.net導(dǎo)出Excel文件之方法比較

       趨明 2012-03-28

       

      web導(dǎo)出Excel也不是什么新鮮的話題了,到網(wǎng)上一查,結(jié)果一大堆??墒且暨x一個(gè)好的、合適的方法也不容易。再加上自己也沒(méi)有真正的做過(guò)這方面的導(dǎo)出工作,到底選那種方法好呢?最近一個(gè)一個(gè)方法地試了一邊,小結(jié)出來(lái)給大家做個(gè)參考。

      測(cè)試題目:通過(guò)一個(gè)模板,導(dǎo)出Excel文件。(模板結(jié)構(gòu)較復(fù)雜,有公式、樣式、合并行等)

      (一)、首先想到的是用服務(wù)器端的Excel軟件com來(lái)生成目標(biāo)Excel。
                      這個(gè)是網(wǎng)上找到介紹最多的方法。這方法不多說(shuō)了,有需要的可以到網(wǎng)上找,這里簡(jiǎn)單講講。這種方法就是調(diào)用服務(wù)器端的Excel軟件,來(lái)讀取模板,填寫模板的。就是后臺(tái)啟動(dòng)Excel來(lái)處理的,在進(jìn)程管理器里可以查看到它的進(jìn)程。
                       方法缺點(diǎn):服務(wù)器端要裝Excel軟件,處理效率低,容易產(chǎn)生進(jìn)程無(wú)法回收。
                       優(yōu)點(diǎn):Excel處理功能豐富,多樣,可以完成一切的Excel文件處理。

                      下面是在網(wǎng)上找到的一個(gè)利用Excel com處理Excel文件的類,自己整理了一下,代碼:

      using System;
      using System.Reflection;

      namespace MyExcelClass{
       /// <summary>
       /// ExcelClass 的摘要說(shuō)明。
       /// </summary>
       public class ExcelClass
       {
        /// <summary>
        /// 構(gòu)建ExcelClass類
        /// </summary>
        public ExcelClass()
        {
         this.m_objExcel=new Excel.Application();
        }
        /// <summary>
        /// 構(gòu)建ExcelClass類
        /// </summary>
        /// <param name="objExcel">Excel.Application</param>
        public ExcelClass(Excel.Application objExcel)
        {
         this.m_objExcel=objExcel;
        }

        /// <summary>
        /// 列標(biāo)號(hào)
        /// </summary>
        private string AList="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        /// <summary>
        /// 獲取描述區(qū)域的字符
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public string GetAix(int x,int y)
        {
         char [] AChars=AList.ToCharArray();
         if(x>=26){return "";}
         string s="";
         s=s+AChars[x-1].ToString();
         s=s+y.ToString();
         return s;
        }

        /// <summary>
        /// 給單元格賦值1
        /// </summary>
        /// <param name="x">行號(hào)</param>
        /// <param name="y">列號(hào)</param>
        /// <param name="align">對(duì)齊(CENTER、LEFT、RIGHT)</param>
        /// <param name="text">值</param>
        public void setValue(int y,int x,string align,string text)
        {
         Excel.Range range=sheet.get_Range(this.GetAix(x,y),miss);
         range.set_Value(miss,text);
         if(align.ToUpper()=="CENTER")
         {
          range.HorizontalAlignment=Excel.XlHAlign.xlHAlignCenter;
         }
         if(align.ToUpper()=="LEFT")
         {
          range.HorizontalAlignment=Excel.XlHAlign.xlHAlignLeft;
         }
         if(align.ToUpper()=="RIGHT")
         {
          range.HorizontalAlignment=Excel.XlHAlign.xlHAlignRight;
         }
        }

        /// <summary>
        /// 給單元格賦值2
        /// </summary>
        /// <param name="x">行號(hào)</param>
        /// <param name="y">列號(hào)</param>
        /// <param name="text">值</param>
        public void setValue(int y,int x,string text)
        {
         Excel.Range range=sheet.get_Range(this.GetAix(x,y),miss);
         range.set_Value(miss,text);
        }

        /// <summary>
        /// 給單元格賦值3
        /// </summary>
        /// <param name="x">行號(hào)</param>
        /// <param name="y">列號(hào)</param>
        /// <param name="text">值</param>
        /// <param name="font">字符格式</param>
        /// <param name="color">顏色</param>
        public void setValue(int y,int x,string text,System.Drawing.Font font,System.Drawing.Color color)
        {
         this.setValue(x,y,text);
         Excel.Range range=sheet.get_Range(this.GetAix(x,y),miss);
         range.Font.Size=font.Size;
         range.Font.Bold=font.Bold;
         range.Font.Color=color;
         range.Font.Name=font.Name;
         range.Font.Italic=font.Italic;
         range.Font.Underline=font.Underline;
        }

        /// <summary>
        /// 插入新行
        /// </summary>
        /// <param name="y">模板行號(hào)</param>
        public void insertRow(int y)
        {
         Excel.Range range=sheet.get_Range(GetAix(1,y),GetAix(25,y));
         range.Copy(miss);
         range.Insert(Excel.XlDirection.xlDown,miss);
         range.get_Range(GetAix(1,y),GetAix(25,y));
         range.Select();
         sheet.Paste(miss,miss);

        }

        /// <summary>
        /// 把剪切內(nèi)容粘貼到當(dāng)前區(qū)域
        /// </summary>
        public void past()
        {
         string s="a,b,c,d,e,f,g";
         sheet.Paste(sheet.get_Range(this.GetAix(10,10),miss),s);
        }
        /// <summary>
        /// 設(shè)置邊框
        /// </summary>
        /// <param name="x1"></param>
        /// <param name="y1"></param>
        /// <param name="x2"></param>
        /// <param name="y2"></param>
        /// <param name="Width"></param>
        public void setBorder(int x1,int y1,int x2,int y2,int Width)
        {
         Excel.Range range=sheet.get_Range(this.GetAix(x1,y1),this.GetAix(x2,y2));
         range.Borders.Weight=Width;
        }
        public void mergeCell(int x1,int y1,int x2,int y2)
        {
         Excel.Range range=sheet.get_Range(this.GetAix(x1,y1),this.GetAix(x2,y2));
         range.Merge(true);
        }

        public Excel.Range getRange(int x1,int y1,int x2,int y2)
        {
         Excel.Range range=sheet.get_Range(this.GetAix(x1,y1),this.GetAix(x2,y2));
         return range;
        }

        private object miss=Missing.Value; //忽略的參數(shù)OLENULL
        private Excel.Application m_objExcel;//Excel應(yīng)用程序?qū)嵗?
        private Excel.Workbooks m_objBooks;//工作表集合
        private Excel.Workbook m_objBook;//當(dāng)前操作的工作表
        private Excel.Worksheet sheet;//當(dāng)前操作的表格

        public Excel.Worksheet CurrentSheet
        {
         get
         {
          return sheet;
         }
         set
         {
          this.sheet=value;
         }
        }

        public Excel.Workbooks CurrentWorkBooks
        {
         get
         {
          return this.m_objBooks;
         }
         set
         {
          this.m_objBooks=value;
         }
        }

        public Excel.Workbook CurrentWorkBook
        {
         get
         {
          return this.m_objBook;
         }
         set
         {
          this.m_objBook=value;
         }
        }

        /// <summary>
        /// 打開Excel文件
        /// </summary>
        /// <param name="filename">路徑</param>
        public void OpenExcelFile(string filename)
        {
         UserControl(false);

         m_objExcel.Workbooks.Open( filename,miss,miss,miss,miss,miss,miss,miss,
                                 miss,miss,miss,miss,miss,miss,miss);

         m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;

         m_objBook = m_objExcel.ActiveWorkbook;
         sheet = (Excel.Worksheet)m_objBook.ActiveSheet;
        }
        public void UserControl(bool usercontrol)
        {
         if(m_objExcel==null){return ;}
         m_objExcel.UserControl=usercontrol;
         m_objExcel.DisplayAlerts=usercontrol;
         m_objExcel.Visible = usercontrol;
        }
        public void CreateExceFile()
        {
         UserControl(false);
         m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
         m_objBook = (Excel.Workbook)(m_objBooks.Add(miss));
         sheet = (Excel.Worksheet)m_objBook.ActiveSheet;
        }
        public void SaveAs(string FileName)
        {
         m_objBook.SaveAs(FileName, miss, miss, miss, miss,
          miss, Excel.XlSaveAsAccessMode.xlNoChange,
          Excel.XlSaveConflictResolution.xlLocalSessionChanges,
          miss,miss, miss, miss);
         //m_objBook.Close(false, miss, miss);
        }
        public void ReleaseExcel()
        {
         m_objExcel.Quit();
         System.Runtime.InteropServices.Marshal.ReleaseComObject((object)m_objExcel);
         System.Runtime.InteropServices.Marshal.ReleaseComObject((object)m_objBooks);
         System.Runtime.InteropServices.Marshal.ReleaseComObject((object)m_objBook);
         System.Runtime.InteropServices.Marshal.ReleaseComObject((object)sheet);
         m_objExcel=null;
         m_objBooks=null;
         m_objBook=null;
         sheet=null;
         GC.Collect();
        }

        /////////////////////////////////
        public bool KillAllExcelApp()
        {
         try
         {
          if(m_objExcel != null) // isRunning是判斷xlApp是怎么啟動(dòng)的flag.
          {
           m_objExcel.Quit();
           System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objExcel);
           //釋放COM組件,其實(shí)就是將其引用計(jì)數(shù)減1
           //System.Diagnostics.Process theProc;
           foreach(System.Diagnostics.Process theProc in System.Diagnostics.Process.GetProcessesByName("EXCEL"))
           {
            //先關(guān)閉圖形窗口。如果關(guān)閉失敗...有的時(shí)候在狀態(tài)里看不到圖形窗口的excel了,
            //但是在進(jìn)程里仍然有EXCEL.EXE的進(jìn)程存在,那么就需要?dú)⒌羲?p
            if( theProc.CloseMainWindow() == false )
            {
             theProc.Kill();
            }
           }
           m_objExcel = null;
           return true;
          }
         }
         catch
         {
          return false;
         }
         return true;
        }
        /////////////////////////////////////////////
       }
      }
                       當(dāng)然,使用的時(shí)候要先在工程引入Excel com,我用的是11版本。

      (二)、利用MSOWC,即是微軟的 office web component。
                     安裝office就有安裝到這個(gè)組件。文件和編程參考在系統(tǒng)盤的Program Files\Common Files\Microsoft Shared\下有Office10或者Office11版本。文件夾中還有幫助文件和例子。
      網(wǎng)上找的一段導(dǎo)出Excel的簡(jiǎn)短例子:

      //請(qǐng)?jiān)陧?xiàng)目中引用OWC11(COM組件)

      OWC11.SpreadsheetClass xlsheet =new OWC11.SpreadsheetClass();
      //合并單元格
      xlsheet.get_Range(xlsheet.Cells[1,1],xlsheet.Cells[1,14]).set_MergeCells(true);
      xlsheet.ActiveSheet.Cells[1,1] ="一級(jí)帳表";
      //字體加粗
      xlsheet.get_Range(xlsheet.Cells[1,1],xlsheet.Cells[1,14]).Font.set_Bold(true);
      //單元格文本水平居中對(duì)齊
      xlsheet.get_Range(xlsheet.Cells[1,1],xlsheet.Cells[1,14]).set_HorizontalAlignment(OWC11.XlHAlign.xlHAlignCenter );
      //設(shè)置字體大小
      xlsheet.get_Range(xlsheet.Cells[1,1],xlsheet.Cells[1,14]).Font.set_Size(14);
      //設(shè)置列寬
      xlsheet.get_Range(xlsheet.Cells[1,3],xlsheet.Cells[1,3]).set_ColumnWidth(50);

      //畫邊框線
      xlsheet.get_Range(xlsheet.Cells[1,1],xlsheet.Cells[10,15]).Borders.set_LineStyle(OWC11.XlLineStyle.xlContinuous );

      //寫入數(shù)據(jù) (這里可根據(jù)需要由DS生成)
      for (int row=2;row<10;row ) //注意寫入數(shù)據(jù)時(shí),必須從第一行開始寫EXCEL里沒(méi)有第"0"行
      {
      for(int i = 0 ;i< 15 ;i )
      {
      xlsheet.ActiveSheet.Cells[row, i 1] =123.456;
      }
      }
      try
      {
      //將數(shù)字格式化為金額(要格式化的單元格內(nèi)的值必須為數(shù)值型)
      xlsheet.get_Range(xlsheet.Cells[2,1],xlsheet.Cells[10,15]).set_NumberFormat("¥#,##0.00");
      xlsheet.Export("D:\\ExportToExcel\\TestOWC.xls",OWC11.SheetExportActionEnum.ssExportActionNone,OWC11.SheetExportFormat.ssExportXMLSpreadsheet);
      Response.Write("Export OK");
      }
      catch
      {
      }


                      這個(gè)方法我沒(méi)有比較全面的試用過(guò)。用的時(shí)候,好像沒(méi)有找到它打開Excel文件的方法,不知道是沒(méi)有還是我沒(méi)留心找。我在試用的時(shí)候,是用它來(lái)設(shè)計(jì)Excel表的,要畫邊框,設(shè)置樣式等,好像很繁。
      (三)、導(dǎo)出XML類型的Excel文件。其實(shí)是一個(gè)XML文件。
                     這個(gè)方法好啊,用起來(lái)放心。因?yàn)閍sp.net里面提供了很多豐富易用的xml類,xml處理起來(lái)明了,簡(jiǎn)單。我把原來(lái)的Excel文件保存為xml文件,用做模板。接著就是讀寫Xml文件了。想這么讀就怎么讀,想怎么寫就怎么寫,查找記錄、改變屬性值方便。
                     優(yōu)點(diǎn):不用安裝Excel等軟件,處理靈活,效率高。
                     缺點(diǎn):Excel2000版本不支持xml,不知道其他版本支不支持。我用的是2003版本。
      (四)、用htm文件。
                    還好Excel2000支持htm,可以把Excel內(nèi)容保存到htm中。其實(shí)就是舊版本的xml嗎。當(dāng)然,樣式和公式也可以用。我正打算用這個(gè)方法來(lái)做。(還沒(méi)有去全面試用,不過(guò)自己對(duì)html還是很熟的,處理起來(lái)應(yīng)該沒(méi)問(wèn)題。)
                     優(yōu)缺點(diǎn)先不說(shuō),不過(guò)它代替xml在低版本的Excel中兼容。

      (五)、網(wǎng)上也有說(shuō)用text做中介的。
                     可是這種文件表示不了樣式,公式等等,格式也很死板。
      (六)、ado.net
                     容易出錯(cuò),而且操作也不靈活,插入等經(jīng)常遇到問(wèn)題,而且不能刪除行。比較適用于結(jié)構(gòu)單一的Excel表。

                   ////連接
                  Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\dd.xls; Extended Properties="Excel 8.0;HDR=YES;"
                  HDR表示是否把第一行當(dāng)作表頭。如果為"NO"則系統(tǒng)或自動(dòng)給行命名為F1,F2,F3.....等
                  ///////獲取默認(rèn)的Excel表
                  DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                  tableName = dt.Rows[0][2].ToString().Trim();
                  ///更新數(shù)據(jù)
                 update [titles$] set F1='dd'
                  ///插入數(shù)據(jù)
                  INSERT INTO DataTable values(1,'e2')
                  ///查詢數(shù)據(jù)
                  select * from [Sheet1$] where F1=5

                 更多可以查看MSDN讀取 Excel  和 MSDN寫入Excel

      (六)、也有方法在客戶端導(dǎo)出Excel。就是用腳本。
      腳本導(dǎo)出: 
        function   run(mytb)  
        {  
            var   mysheet=new   ActiveXObject("OWC.Spreadsheet");  
            with(mysheet)  
            {  
                DataType   =   "HTMLData";  
        HTMLData   =mytb.outerHTML;  
        try{  
            ActiveSheet.Export("c:\\dxx.xls",   0);  
            alert('匯出完畢');  
          };  
        catch   (e){  
              alert('導(dǎo)出Excel表失敗,請(qǐng)確定已安裝Excel2000(或更高版本),并且沒(méi)打開同名xls文件');  
            };  
                }  
          }

      =============================
      補(bǔ)充:
      最近在導(dǎo)出DataGrid/GridView數(shù)據(jù)行到Excel的時(shí)候,如果文件名為中文的話,就會(huì)出現(xiàn)亂碼。暈了一個(gè)下午,終于找到解決方法,現(xiàn)記載如下:

      1Response.Clear();
      2   Response.Buffer = true;
      3   Response.Charset = "gb2312";
      4   Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
      5   Response.AppendHeader("content-disposition","attachment;filename=\"" + System.Web.HttpUtility.UrlEncode("中文名稱",System.Text.Encoding.UTF8) + ".xls\"");
      6   Response.ContentType = "Application/ms-excel";
      3、身份證號(hào)碼的問(wèn)題:存儲(chǔ)為字符串
              if (e.Row.RowType == DataControlRowType.DataRow)
              {
                  e.Row.Cells[2].Attributes.Add("style", "vnd.ms-excel.numberformat:@");//身份證號(hào)列
                  e.Item.Cells[3].Attributes.Add("style","vnd.ms-excel.numberformat:¥#,###.00");//金額
              }

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多