ASP.NET 程序讀取Excel文檔

剛剛在項目中完成了一個讀取excel文檔的功能,記下以供交流。
在寫程序的過程中,采取了兩種讀取 excel文檔的方式!
(一)用OleDb讀取Excel文件
此方法和訪問Access文件沒什麼兩樣,但要注意sql查詢的寫法
參考的程序如下,輸入文件名,確定好路徑,返回一個DataView對象
此程序的優點是簡便,模式很熟悉,缺點就是有很多CELL讀取不到!適用於格式很整齊的excel文件。
using  using System.Data;
using  using System.Data.OleDb;
public  DataView ReadDraftXlsFile(string path)
        {           
            string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" /* + Page.Server.MapPath(".") + "//ExcelFolder//CHBDraft.xls" */ + path + ";" + "Extended Properties=Excel 8.0;";
            string strCmd = "select * from [sheet1$]";
            System.Data.OleDb.OleDbConnection  con = new OleDbConnection(strCon);
            System.Data.OleDb.OleDbDataAdapter da  = new OleDbDataAdapter(strCmd, con);

            System.Data.DataSet ds = new System.Data.DataSet();
            da.Fill(ds);
           
            return ds.Tables[0].DefaultView;
        }

(二)用COM組件訪問Excel文件

此種方法操作能力很強,就是有點麻煩。
   首先進行權限設置。在win2000professional環境下,運行dcomcnfg命令,出現DCOM內容設定對話框,在應用程序標簽下選擇  miscrosoft excel ,點下面的“內容”按鈕,到安全性標簽中自定義“起動權限管理”,新增一個everyone用戶,就OK了。否則就會出現權限不夠的異常!
  然後在顯目中引入COM組件:Microsoft Excel 11.0 Object Library(Microsoft Excel2003環境)
參考的程序如下,輸入文件名,確定好路徑,返回一個DataView對象
public DataView ReadInvoiceXlsFile(string excelFilePath)
        {
                       
            Excel.Application myExcel=new Excel.ApplicationClass( ) ;
            object oMissing = System.Reflection.Missing.Value ;
        myExcel.Application.Workbooks.Open(excelFilePath,oMissing,oMissing,oMissing,oMissing,oMissing,    oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing) ;
            Excel.Workbook myBook = myExcel.Workbooks[1] ;
            Excel.Worksheet mySheet = (Excel.Worksheet)myBook.Worksheets[1] ;

            int rowCount = 1; 
            for(; ; rowCount++)
            {
                int j=1;
                for(; j<10 ; j++)
                {
                    Excel.Range r=(Excel.Range)mySheet.Cells[rowCount,j];
                    string strValue=r.Text.ToString();
                    if(strValue != "") break;
                }
                if(j==10) break;
            }

            System.Data.DataTable dt=new System.Data.DataTable("mytable");
            dt.Columns.Add("F1", System.Type.GetType("System.String"));
            dt.Columns.Add("F2", System.Type.GetType("System.String"));
            dt.Columns.Add("F3", System.Type.GetType("System.String"));
            dt.Columns.Add("F4", System.Type.GetType("System.String"));
            dt.Columns.Add("F5", System.Type.GetType("System.String"));
            dt.Columns.Add("F6", System.Type.GetType("System.String"));
            dt.Columns.Add("F7", System.Type.GetType("System.String"));
            dt.Columns.Add("F8", System.Type.GetType("System.String"));
            dt.Columns.Add("F9", System.Type.GetType("System.String"));
           
            DataSet myDs = new DataSet();
            myDs.Tables.Add(dt);
            DataRow myRow;
            myDs.Clear();
            for( int i = 1 ; i <rowCount ; i ++ )
            {
                myRow = myDs.Tables["mytable"].NewRow();
                for( int j = 1 ; j <=9; j ++ )
                {
                    Excel.Range r=(Excel.Range)mySheet.Cells[i,j];
                    string strValue=r.Text.ToString();
                    string aa=strValue;
                    string columnname="F"+j.ToString();
                    myRow[columnname]=strValue;
                }
                myDs.Tables["mytable"].Rows.Add(myRow);
            }

            myExcel.Quit();
            System.GC.Collect();

            return dt.DefaultView;
        }

  因為第一次碰到,我的學習也很表面。
       在解決問題的過程中看參考了如下帖子:
        http://blog.csdn.net/ChengKing/archive/2005/11/29/539514.aspx
  推薦大家看一看,帖子裡還包括例子程序下載!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值