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[][].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=;i<oldds.Tables[].Rows.Count;i++)
{
//在这里不能使用ImportRow方法将一行导入到news中,
//因为ImportRow将保留原来DataRow的所有设置(DataRowState状态不变)。
//在使用ImportRow后newds内有值,但不能更新到Excel中因为所有导入行的DataRowState!=Added
DataRow nrow=aDataSet.Tables["Table1"].NewRow();
for(int j=;j<newds.Tables[].Columns.Count;j++)
{
nrow[j]=oldds.Tables[].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
{ www.jbxue.com
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();
//取得总记录行数(包括标题列)
int rowsint = ws.UsedRange.Cells.Rows.Count; //得到行数
//int columnsint = mySheet.UsedRange.Cells.Columns.Count;//得到列数
//取得数据范围区域(不包括标题列)
Range rng1 = ws.Cells.get_Range("B2", "B" + rowsint);//item
Range rng2 = ws.Cells.get_Range("K2", "K" + rowsint); //Customer
object[,] arryItem= (object[,])rng1.Value2;//get range's value
object[,] arryCus = (object[,])rng2.Value2;
//将新值赋给一个数组
string[,] arry = new string[rowsint-, ];
for (int i = ; i <= rowsint-; i++)
{
//Item_Code列
arry[i - , ] =arryItem[i, ].ToString();
//Customer_Name列
arry[i - , ] = arryCus[i, ].ToString();
}
Response.Write(arry[, ] + "/" + arry[, ] + "#" + arry[rowsint - , ] + "/" + arry[rowsint - , ]);
}
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, FileAccess.Read, FileShare.None);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.GetEncoding());
string str = "";
string s = Console.ReadLine();
while (str != null)
{ www.jbxue.com
str = sr.ReadLine();
string[] xu = new String[];
xu = str.Split(',');
string ser = xu[];
string dse = xu[];
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[];
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[].ToString();
str1=dr[].ToString();
str2=dr[].ToString();
str3=dr[].ToString();
str4=dr[].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();
}