這篇文章主要介紹了asp.net讀取excel文件的三種方法示例,包括采用OleDB讀取Excel文件、引用的com組件讀取Excel文件、用文件流讀取,需要的朋友可以參考下
方法一:采用OleDB讀取Excel文件 把Excel文件當做一個數據源來進行數據的讀取操作,實例如下: 代碼如下:public DataSet ExcelToDS(string Path) { string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source="+ Path +";"+"Extended Properties=Excel 8.0;"; OleDbConnection conn = new OleDbConnection(strConn); conn.Open(); string strExcel = ""; OleDbDataAdapter myCommand = null; DataSet ds = null; strExcel="select * from [sheet1$]"; myCommand = new OleDbDataAdapter(strExcel, strConn); ds = new DataSet(); myCommand.Fill(ds,"table1"); return ds; } 對于Excel中的表即sheet([sheet1$])如果不是固定的可以使用下面的方法得到 代碼如下:string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source="+ Path +";"+"Extended Properties=Excel 8.0;"; OleDbConnection conn = new OleDbConnection(strConn); DataTable schemaTable = objConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables,null); string tableName=schemaTable.Rows[0][2].ToString().Trim(); 另外:也可進行寫入Excel文件,實例如下: 代碼如下:public void DSToExcel(string Path,DataSet oldds) { //先得到匯總Excel的DataSet 主要目的是獲得Excel在DataSet中的結構 string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source ="+path1+";Extended Properties=Excel 8.0" ; OleDbConnection myConn = new OleDbConnection(strCon) ; string strCom="select * from [Sheet1$]"; myConn.Open ( ) ; OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom, myConn ) ; system.Data.OleDb.OleDbCommandBuilder builder=new OleDbCommandBuilder(myCommand); //QuotePrefix和QuoteSuffix主要是對builder生成InsertComment命令時使用。 builder.QuotePrefix="["; //獲取insert語句中保留字符(起始位置) builder.QuoteSuffix="]"; //獲取insert語句中保留字符(結束位置) DataSet newds=new DataSet(); myCommand.Fill(newds ,"Table1") ; for(int i=0;i<oldds.Tables[0].Rows.Count;i++) { //在這里不能使用ImportRow方法將一行導入到news中, //因為ImportRow將保留原來DataRow的所有設置(DataRowState狀態不變)。 //在使用ImportRow后newds內有值,但不能更新到Excel中因為所有導入行的DataRowState!=Added DataRow nrow=aDataSet.Tables["Table1"].NewRow(); for(int j=0;j<newds.Tables[0].Columns.Count;j++) { nrow[j]=oldds.Tables[0].Rows[i][j]; } newds.Tables["Table1"].Rows.Add(nrow); } myCommand.Update(newds,"Table1"); myConn.Close(); } 方法二:引用的com組件:Microsoft.Office.Interop.Excel.dll讀取Excel文件 首先是Excel.dll的獲取,將Office安裝目錄下的Excel.exe文件Copy到DotNet的bin目錄下,cmd到該目錄下,運行 TlbImp EXCEL.EXE Excel.dll 得到Dll文件。 在項目中添加引用該dll文件 代碼如下://讀取EXCEL的方法 (用范圍區域讀取數據) private void OpenExcel(string strFileName) { object missing = System.Reflection.Missing.Value; Application excel = new Application();//lauch excel application if (excel == null) { Response.Write("<script>alert('Can't access excel')</script>"); } else { excel.Visible = false; excel.UserControl = true; // 以只讀的形式打開EXCEL文件 Workbook wb = excel.Application.Workbooks.Open(strFileName, missing, true, missing, missing, missing, missing, missing, missing, true, missing, missing, missing, missing, missing); //取得第一個工作薄Worksheet ws = (Worksheet)wb.Worksheets.get_Item(1);//取得總記錄行數(包括標題列)int rowsint = ws.UsedRange.Cells.Rows.Count; //得到行數//int columnsint = mySheet.UsedRange.Cells.Columns.Count;//得到列數//取得數據范圍區域(不包括標題列)Range rng1 = ws.Cells.get_Range("B2", "B" + rowsint);//itemRange rng2 = ws.Cells.get_Range("K2", "K" + rowsint); //Customerobject[,] arryItem= (object[,])rng1.Value2;//get range's valueobject[,] arryCus = (object[,])rng2.Value2; //將新值賦給一個數組string[,] arry = new string[rowsint-1, 2];for (int i = 1; i <= rowsint-1; i++){//Item_Code列arry[i - 1, 0] =arryItem[i, 1].ToString();//Customer_Name列arry[i - 1, 1] = arryCus[i, 1].ToString();}Response.Write(arry[0, 0] + "/" + arry[0, 1] + "#" + arry[rowsint - 2, 0] + "/" + arry[rowsint - 2, 1]);} excel.Quit();excel = null;Process[] procs = Process.GetProcessesByName("excel");foreach (Process pro in procs){pro.Kill();//沒有更好的方法,只有殺掉進程}GC.Collect();} 方法三:將Excel文件轉化成CSV(逗號分隔)的文件,用文件流讀取(等價就是讀取一個txt文本文件)。 先引用命名空間: 代碼如下:using System.Text;和using System.IO; FileStream fs = new FileStream("d:Customer.csv", FileMode.Open, File新聞熱點
疑難解答