.NET读取Excel文件的三种方法的区别(2)


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

另外也可以将数据库数据导入到一个txt文件,实例如下:

复制代码 代码如下:


//txt文件名 
string fn = DateTime.Now.ToString("yyyyMMddHHmmss") + "-" + "PO014" + ".txt";   OleDbConnection con = new OleDbConnection(conStr);   con.Open();  string sql = "select  ITEM,REQD_DATE,QTY,PUR_FLG,PO_NUM from TSD_PO014";          OleDbCommand mycom = new OleDbCommand("select * from TSD_PO014", mycon); 
//OleDbDataReader myreader = mycom.ExecuteReader();  //也可以用Reader读取数据
DataSet ds = new DataSet(); 
OleDbDataAdapter oda = new OleDbDataAdapter(sql, con);
oda.Fill(ds, "PO014"); 
DataTable dt = ds.Tables[0]; 
FileStream fs = new FileStream(Server.MapPath("download/" + fn), FileMode.Create, FileAccess.ReadWrite); 
StreamWriter strmWriter = new StreamWriter(fs);    //存入到文本文件中   
//把标题写入.txt文件中 
//for (int i = 0; i <dt.Columns.Count;i++) 
//{ 
//    strmWriter.Write(dt.Columns[i].ColumnName + "  ");
//} 
foreach (DataRow dr in dt.Rows) 
{    
  string str0, str1, str2, str3;     
  string str = "|";  //数据用"|"分隔开   
  str0 = dr[0].ToString();     
  str1 = dr[1].ToString();   
  str2 = dr[2].ToString(); 
  str3 = dr[3].ToString();  
   str4 = dr[4].ToString().Trim();  
   strmWriter.Write(str0);  
   strmWriter.Write(str); 
   strmWriter.Write(str1);  
   strmWriter.Write(str); 
   strmWriter.Write(str2);  
   strmWriter.Write(str);  
   strmWriter.Write(str3); 
   strmWriter.WriteLine();  //换行 

strmWriter.Flush();
strmWriter.Close();
if (con.State == ConnectionState.Open) 
{    
  con.Close();
}

ASP.NET读取Excel文件的方法就向你介绍到这里,希望对你了解ASP.NET读取Excel文件有所帮助。

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjdwyx.html