继续前篇,第三种方法是利用OELDB数据访问对象操作Excel文件,达到将数据导出到Excel的目的,这种方法可能用的人不太多,缺点是必须有一个存在的Excel文件。优点是服务器不需要安装Excel,且生成的Excel是标准的Excel格式可以当作数据源使用。
准备工作:
1)先用准备一个Excel文件,这个Excel文件可以是空的,或是有一定格式的当做数据模板使用。
2)将操作的目录给ASP.NET用户(ASPNET 或NETWORK SERVICE)写入权限。
以下是示例:
说明一下:如果这个模板文件是个空文件,则需要使用Create Table创建一个工作表,否则只需使用已有的表进操作就可以了。
DataTable dt
=
new
DataTable();
dt.Columns.Add( " name " );
dt.Columns.Add( " age " , typeof ( int ));
dt.Columns.Add( " phone " );
dt.Rows.Add( " 老张 " , 40 , " 99213812 " );
dt.Rows.Add( " 小李 " , 28 , " a21313 " );
dt.Rows.Add( " 小王 " , 22 , " 2131434 " );
string FileName = Guid.NewGuid().ToString() + " .xls " ;
string sNewFullFile = Server.MapPath(FileName);
try
{
File.Copy(Server.MapPath( " format.xls " ), sNewFullFile);
}
catch (Exception er)
{
Response.Write(er.Message);
return ;
}
string strConn = " Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=True;Data Source= " + sNewFullFile + " ;Extended Properties=Excel 8.0; " ;
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(strConn);
OleDbCommand cmd = null ;
bool bRet = false ;
try
{
conn.Open();
cmd = new OleDbCommand( " create table [sheet4]([姓名] Text,[年龄] int,[电话] Text) " ,conn);
cmd.ExecuteNonQuery();
string strSQL = " INSERT INTO [Sheet4$] ([姓名], [年龄],[电话]) VALUES (?, ?, ?) " ;
cmd = new OleDbCommand(strSQL, conn);
for ( int i = 0 ; i < 3 ; i ++ )
{
cmd.Parameters.Add(i.ToString(), OleDbType.VarChar);
}
DataView dv = dt.DefaultView;
foreach (DataRowView row in dv)
{
cmd.Parameters[ 0 ].Value = row[ " name " ].ToString();
cmd.Parameters[ 1 ].Value = ( int ) row[ " age " ];
cmd.Parameters[ 2 ].Value = row[ " phone " ].ToString();
cmd.ExecuteNonQuery();
}
bRet = true ;
}
catch (Exception er)
{
Response.Write(er.Message);
}
finally
{
if (cmd != null )
{
cmd.Dispose();
}
conn.Dispose();
}
if (bRet)
Response.Redirect(FileName);
dt.Columns.Add( " name " );
dt.Columns.Add( " age " , typeof ( int ));
dt.Columns.Add( " phone " );
dt.Rows.Add( " 老张 " , 40 , " 99213812 " );
dt.Rows.Add( " 小李 " , 28 , " a21313 " );
dt.Rows.Add( " 小王 " , 22 , " 2131434 " );
string FileName = Guid.NewGuid().ToString() + " .xls " ;
string sNewFullFile = Server.MapPath(FileName);
try
{
File.Copy(Server.MapPath( " format.xls " ), sNewFullFile);
}
catch (Exception er)
{
Response.Write(er.Message);
return ;
}
string strConn = " Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=True;Data Source= " + sNewFullFile + " ;Extended Properties=Excel 8.0; " ;
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(strConn);
OleDbCommand cmd = null ;
bool bRet = false ;
try
{
conn.Open();
cmd = new OleDbCommand( " create table [sheet4]([姓名] Text,[年龄] int,[电话] Text) " ,conn);
cmd.ExecuteNonQuery();
string strSQL = " INSERT INTO [Sheet4$] ([姓名], [年龄],[电话]) VALUES (?, ?, ?) " ;
cmd = new OleDbCommand(strSQL, conn);
for ( int i = 0 ; i < 3 ; i ++ )
{
cmd.Parameters.Add(i.ToString(), OleDbType.VarChar);
}
DataView dv = dt.DefaultView;
foreach (DataRowView row in dv)
{
cmd.Parameters[ 0 ].Value = row[ " name " ].ToString();
cmd.Parameters[ 1 ].Value = ( int ) row[ " age " ];
cmd.Parameters[ 2 ].Value = row[ " phone " ].ToString();
cmd.ExecuteNonQuery();
}
bRet = true ;
}
catch (Exception er)
{
Response.Write(er.Message);
}
finally
{
if (cmd != null )
{
cmd.Dispose();
}
conn.Dispose();
}
if (bRet)
Response.Redirect(FileName);