国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
ASP.NET讀取Excel文件的三大方法淺析

最近研究了如何使得ASP.NET讀取Excel文件更加高效呢,現(xiàn)總結(jié)如下:

ASP.NET讀取Excel文件方法一:采用OleDB讀取Excel文件:

把Excel文件當(dāng)做一個(gè)數(shù)據(jù)源來(lái)進(jìn)行數(shù)據(jù)的讀取操作,實(shí)例如下:

  1. public DataSet ExcelToDS(string Path)   
  2. {   
  3.  string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source="+ Path +";"+"Extended Properties=Excel 8.0;";   
  4.  OleDbConnection conn = new OleDbConnection(strConn);   
  5.  conn.Open();     
  6.  string strExcel = "";      
  7.  OleDbDataAdapter myCommand = null;   
  8.  DataSet ds = null;   
  9.  strExcel="select * from [sheet1$]";   
  10.  myCommand = new OleDbDataAdapter(strExcel, strConn);   
  11.  ds = new DataSet();   
  12.  myCommand.Fill(ds,"table1");      
  13.  return ds;   
  14. }  

對(duì)于Excel中的表即sheet([sheet1$])如果不是固定的可以使用下面的方法得到

  1. string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +"Data Source="+ Path +";"+"Extended Properties=Excel 8.0;";   
  2. OleDbConnection conn = new OleDbConnection(strConn);   
  3. DataTable schemaTable = objConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables,null);   
  4. string tableName=schemaTable.Rows[0][2].ToString().Trim();    

另外:也可進(jìn)行寫(xiě)入Excel文件,實(shí)例如下:

  1. public void DSToExcel(string Path,DataSet oldds)   
  2. {   
  3.  //先得到匯總Excel的DataSet 主要目的是獲得Excel在DataSet中的結(jié)構(gòu)   
  4.  string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source ="+path1+";Extended Properties=Excel 8.0" ;   
  5.  OleDbConnection myConn = new OleDbConnection(strCon) ;   
  6.  string strCom="select * from [Sheet1$]";   
  7.  myConn.Open ( ) ;   
  8.  OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom, myConn ) ;   
  9.  ystem.Data.OleDb.OleDbCommandBuilder builder=new OleDbCommandBuilder(myCommand);   
  10.  //QuotePrefix和QuoteSuffix主要是對(duì)builder生成InsertComment命令時(shí)使用。   
  11.  builder.QuotePrefix="[";     //獲取insert語(yǔ)句中保留字符(起始位置)   
  12.  builder.QuoteSuffix="]"//獲取insert語(yǔ)句中保留字符(結(jié)束位置)   
  13.  DataSet newds=new DataSet();   
  14.  myCommand.Fill(newds ,"Table1") ;   
  15.  for(int i=0;i<oldds.Tables[0].Rows.Count;i++)   
  16.  {   
  17.   //在這里不能使用ImportRow方法將一行導(dǎo)入到news中,
  18. //因?yàn)镮mportRow將保留原來(lái)DataRow的所有設(shè)置(DataRowState狀態(tài)不變)。
  19. //在使用ImportRow后newds內(nèi)有值,但不能更新到Excel中因?yàn)樗袑?dǎo)入行的DataRowState!=Added   
  20.   DataRow nrow=aDataSet.Tables["Table1"].NewRow();   
  21.   for(int j=0;j<newds.Tables[0].Columns.Count;j++)   
  22.   {   
  23.    nrow[j]=oldds.Tables[0].Rows[i][j];   
  24.   }   
  25.   newds.Tables["Table1"].Rows.Add(nrow);   
  26.  }   
  27.  myCommand.Update(newds,"Table1");   
  28.  myConn.Close();   
  29. }  

ASP.NET讀取Excel文件方法二:引用的com組件:Microsoft.Office.Interop.Excel.dll讀取Excel文件

首先是Excel.dll的獲取,將Office安裝目錄下的Excel.exe文件Copy到DotNet的bin目錄下,cmd到該目錄下,運(yùn)行 TlbImp EXCEL.EXE Excel.dll 得到Dll文件。

在項(xiàng)目中添加引用該dll文件.

  1. //讀取EXCEL的方法   (用范圍區(qū)域讀取數(shù)據(jù))  
  2. private void OpenExcel(string strFileName)  
  3. {  
  4.     object missing = System.Reflection.Missing.Value;  
  5.     Application excel = new Application();//lauch excel application  
  6.     if (excel == null)  
  7.     {  
  8.         Response.Write("<script>alert('Can't access excel')</script>");  
  9.     }  
  10.     else 
  11.     {  
  12.         excel.Visible = false;  excel.UserControl = true;  
  13.         // 以只讀的形式打開(kāi)EXCEL文件  
  14.         Workbook wb = excel.Application.Workbooks.Open(strFileName, missing, true, missing, missing, missing,  
  15.          missing, missing, missing, true, missing, missing, missing, missing, missing);  
  16.         //取得第一個(gè)工作薄  
  17.         Worksheet ws = (Worksheet)wb.Worksheets.get_Item(1);  
  18.  
  19.         //取得總記錄行數(shù)   (包括標(biāo)題列)  
  20.         int rowsint = ws.UsedRange.Cells.Rows.Count; //得到行數(shù)  
  21.         //int columnsint = mySheet.UsedRange.Cells.Columns.Count;//得到列數(shù)  
  22.  
  23.         //取得數(shù)據(jù)范圍區(qū)域  (不包括標(biāo)題列)    
  24.         Range rng1 = ws.Cells.get_Range("B2""B" + rowsint);   //item  
  25.  
  26.         Range rng2 = ws.Cells.get_Range("K2""K" + rowsint);  //Customer  
  27.         object[,] arryItem= (object[,])rng1.Value2;   //get range's value  
  28.         object[,] arryCus = (object[,])rng2.Value2;     
  29.         //將新值賦給一個(gè)數(shù)組  
  30.         string[,] arry = new string[rowsint-1, 2];  
  31.         for (int i = 1; i <= rowsint-1; i++)  
  32.         {  
  33.             //Item_Code列  
  34.             arry[i - 1, 0] =arryItem[i, 1].ToString();  
  35.             //Customer_Name列  
  36.             arry[i - 1, 1] = arryCus[i, 1].ToString();  
  37.         }  
  38.         Response.Write(arry[0, 0] + "  /  " + arry[0, 1] + "#" + arry[rowsint - 2, 0] + "  /  " + arry[rowsint - 2, 1]);  
  39.     }  
  40.      excel.Quit();  excel = null;  
  41.     Process[] procs = Process.GetProcessesByName("excel");  
  42.  
  43.     foreach (Process pro in procs)  
  44.     {  
  45.         pro.Kill();//沒(méi)有更好的方法,只有殺掉進(jìn)程  
  46.     }  
  47.     GC.Collect();  

ASP.NET讀取Excel文件方法三:將Excel文件轉(zhuǎn)化成CSV(逗號(hào)分隔)的文件,用文件流讀取(等價(jià)就是讀取一個(gè)txt文本文件)。

先引用命名空間:

  1. using System.Text;和using System.IO;  
  2.           FileStream fs = new FileStream("d:\\Customer.csv", FileMode.Open, FileAccess.Read, FileShare.None);  
  3.           StreamReader sr = new StreamReader(fs, System.Text.Encoding.GetEncoding(936));  
  4.  
  5.           string str = "";  
  6.           string s = Console.ReadLine();  
  7.           while (str != null)  
  8.           {    str = sr.ReadLine();  
  9.                string[] xu = new String[2];  
  10.                xu = str.Split(',');  
  11.                string ser = xu[0];   
  12.                string dse = xu[1];                if (ser == s)  
  13.                {  Console.WriteLine(dse);break;  
  14.                }  
  15.           }   sr.Close(); 

另外也可以將數(shù)據(jù)庫(kù)數(shù)據(jù)導(dǎo)入到一個(gè)txt文件,實(shí)例如下:

  1. //txt文件名  
  2. string fn = DateTime.Now.ToString("yyyyMMddHHmmss") + "-" + "PO014" + ".txt";  
  3.  
  4. OleDbConnection con = new OleDbConnection(conStr);   
  5. con.Open();  
  6. string sql = "select  ITEM,REQD_DATE,QTY,PUR_FLG,PO_NUM from TSD_PO014";          
  7. /OleDbCommand mycom = new OleDbCommand("select * from TSD_PO014", mycon);  
  8. //OleDbDataReader myreader = mycom.ExecuteReader();  //也可以用Reader讀取數(shù)據(jù)  
  9. DataSet ds = new DataSet();  
  10. OleDbDataAdapter oda = new OleDbDataAdapter(sql, con);  
  11. oda.Fill(ds, "PO014");  
  12. DataTable dt = ds.Tables[0];  
  13.  
  14. FileStream fs = new FileStream(Server.MapPath("download/" + fn), FileMode.Create, FileAccess.ReadWrite);  
  15. StreamWriter strmWriter = new StreamWriter(fs);    //存入到文本文件中   
  16.  
  17. //把標(biāo)題寫(xiě)入.txt文件中   
  18. //for (int i = 0; i <dt.Columns.Count;i++)  
  19. //{  
  20. //    strmWriter.Write(dt.Columns[i].ColumnName + "  ");  
  21. //}  
  22.  
  23. foreach (DataRow dr in dt.Rows)  
  24. {  
  25.     string str0, str1, str2, str3;  
  26.     string str = "|";  //數(shù)據(jù)用"|"分隔開(kāi)  
  27.     str0 = dr[0].ToString();  
  28.     str1 = dr[1].ToString();  
  29.     str2 = dr[2].ToString();  
  30.     str3 = dr[3].ToString();  
  31.     str4 = dr[4].ToString().Trim();  
  32.     strmWriter.Write(str0);  
  33.     strmWriter.Write(str);  
  34.     strmWriter.Write(str1);  
  35.     strmWriter.Write(str);  
  36.     strmWriter.Write(str2);  
  37.     strmWriter.Write(str);  
  38.     strmWriter.Write(str3);  
  39.     strmWriter.WriteLine();  //換行  
  40. }  
  41. strmWriter.Flush();  
  42. strmWriter.Close();  
  43. if (con.State == ConnectionState.Open)  
  44. {  
  45.     con.Close();  

ASP.NET讀取Excel文件的方法就向你介紹到這里,希望對(duì)你了解ASP.NET讀取Excel文件有所幫助。

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶(hù)發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
asp
asp.net(c#)操作office
2012,我的C#全能Excel操作(無(wú)需Office,不使用XML)
.net實(shí)現(xiàn)將Excel中的數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫(kù)
C#中tostring怎么保留小數(shù)點(diǎn)后面3位,準(zhǔn)備數(shù)字,不四舍五入,第四位才四舍五入 愛(ài)說(shuō)篇
C# 將數(shù)據(jù)導(dǎo)出到Excel匯總
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服