access数据库在线生成xls电子表格

时间:2021-10-15 18:27:32

protected void Button1_Click(object sender, EventArgs e)
{

OleDbConnection conn = new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=|datadirectory|***.mdb;");
conn.Open();
string sql = "select count(*) from m";
OleDbCommand cmd = new OleDbCommand(sql, conn);
int rc = (int)cmd.ExecuteScalar();
try
{
sql = @"select top 65534 * into [Excel 8.0;database=" Server.MapPath(".") @"**.xls].[Sheet1] from m";
cmd = new System.Data.OleDb.OleDbCommand(sql, conn);
cmd.ExecuteNonQuery();
conn.Close();
conn.Dispose();
conn = null;
Response.Write("文件已经生成,请下载!");
}
catch (Exception ex)
{

Response.Write(ex.Message.ToString());
}

}