ASP.net连接Excel
首先添加命名空间
using System.Data.OleDb;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
strfile = Request.QueryString["filename"];//从其他页面传过来的文件路径
Excel(strfile);
}
}
private void Excel(string filepath)
{
try
{
Dataset ds = new DataSet();
string ConnStr = "Provider=Microsoft.Jet.OleDb.4.0;data source=" + filepath +
";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";//连接Excel的字符串
string query = "SELECT * from [student$]";//Excel中的表名称
OleDbCommand oleCommand = new OleDbCommand(query, new OleDbConnection(ConnStr));
OleDbDataAdapter oleAdapter = new OleDbDataAdapter(oleCommand);
oleAdapter.Fill(ds, "[student$]");
rowcount = ds.Tables[0].Rows.Count;
gridview1.DataSource = ds;
gridview1.DataBind();
lblmes.Text = "上传成功,数据如下所示,请确认:";//lblmes为label,显示提示信息
}
catch (OleDbException)
{
string filename = filepath.Substring(filepath.LastIndexOf('/') + 1);
lblmes.Text = "错误!请确认上传文件是否正确!当前上传的文件为:" + filename;
lbtnSure.Visible = false;
}
catch(Exception ee)
{
lblmes.Text = ee.Message;
}
}
相关文档:
Asp.Net程序的高级功能,有很多都是基于IHttpHandler接口来实现的,在Winwods Server 2003 下,配置IIS6的 Web服务扩展非常简单。最近用Win7+IIS7.5的人越来越多,在这个环境下配置Asp.Net的Web服务扩展就有些麻烦。以下内容,就是在Win7和IIS7.5环境下配置过程。
& ......
在ASP.NET的SOUCE中,不会存在BGSOUND这个标签,但是,如果我想插入一段背景音乐怎么办?
我这里有两种解决方法:
1、写入一个网页播放器,但是这样,本人认为很复杂,而且,我也不会
2、用IFRAME嵌套一个HTML网页,将IFRAME的的WIDTH和HEIGHT都设置成0PX,即可。 ......
http://zhanglei1286.blog.163.com/blog/static/1895797120091112113019600/
在后台代码里:
SQL 2000:
static string StrConn = "server=.;uid=sa;pwd=sa;database=MyCUDS";
SQL2005:
con = new SqlConnection(@"Server=.\SQLExpress;Database=db_CMS;Persist Security Info=True;User ID=sa;Password=Masslong");
......