asp.net读取Excel表并显示在页面中
来源:互联网 发布:佳能程序软件 编辑:程序博客网 时间:2024/05/16 12:08
1.protected void btnUpload_Click(object sender, EventArgs e)
2.{
3.if ((txtFilePath.HasFile))
4.{
5.
6.OleDbConnection conn = new OleDbConnection();
7.OleDbCommand cmd = new OleDbCommand();
8.OleDbDataAdapter da = new OleDbDataAdapter();
9.DataSet ds = new DataSet();
10.string query = null;
11.string connString = "";
12.string strFileName = DateTime.Now.ToString("ddMMyyyy_HHmmss");
13.string strFileType = System.IO.Path.GetExtension(txtFilePath.FileName).ToString().ToLower();
14.
15.//Check file type
16.if (strFileType == ".xls" || strFileType == ".xlsx")
17.{
18.txtFilePath.SaveAs(Server.MapPath("~/UploadedExcel/" + strFileName + strFileType));
19.}
20.else
21.{
22.lblMessage.Text = "Only excel files allowed";
23.lblMessage.ForeColor = System.Drawing.Color.Red;
24.lblMessage.Visible = true;
25.return;
26.}
27.
28.string strNewPath = Server.MapPath("~/UploadedExcel/" + strFileName + strFileType);
29.
30.//Connection String to Excel Workbook
31.if (strFileType.Trim() == ".xls")
32.{
33.connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strNewPath + ";Extended Properties=/"Excel 8.0;HDR=Yes;IMEX=2/"";
34.}
35.else if (strFileType.Trim() == ".xlsx")
36.{
37.connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath + ";Extended Properties=/"Excel 12.0;HDR=Yes;IMEX=2/"";
38.}
39.
40.query = "SELECT * FROM [Sheet1$]";
41.//query = "SELECT [Country],[Capital] FROM [Sheet1$] WHERE [Currency]=’Rupee’"
42.//query = "SELECT [Country],[Capital] FROM [Sheet1$]"
43.
44.//Create the connection object
45.conn = new OleDbConnection(connString);
46.//Open connection
47.if (conn.State == ConnectionState.Closed) conn.Open();
48.//Create the command object
49.cmd = new OleDbCommand(query, conn);
50.da = new OleDbDataAdapter(cmd);
51.ds = new DataSet();
52.da.Fill(ds);
53.
54.grvExcelData.DataSource = ds.Tables[0];
55.grvExcelData.DataBind();
56.
57.lblMessage.Text = "Data retrieved successfully! Total Records:" + ds.Tables[0].Rows.Count;
58.lblMessage.ForeColor = System.Drawing.Color.Green;
59.lblMessage.Visible = true;
60.
61.da.Dispose();
62.conn.Close();
63.conn.Dispose();
64.}
65.else
66.{
67.lblMessage.Text = "Please select an excel file first";
68.lblMessage.ForeColor = System.Drawing.Color.Red;
69.lblMessage.Visible = true;
70.}
71.}
- asp.net读取Excel表并显示在页面中
- 从ORACLE中读取图片并在asp.net页面中显示
- ASP.net 从数据库中读取html格式的数据并显示在页面中
- ASP.NET读取网络图片并在页面上显示
- ASP.NET读取网络图片并在页面上显示
- ASP.NET中读取excel内容,并显示在界面上
- ASP.net中读取excel内容,并显示在界面上
- ASP.NET中读取Excel内容,并显示在界面上
- ASP.NET中读取Excel内容,并显示在界面上
- ASP.NET中读取Excel内容,并显示在界面上
- asp.net 图片批量上传预览,在Silverlight页面中读取并滚动显示
- asp.net 将本地excel表导入网页 并在gridview中显示
- 使用asp.net读取并显示excel数据
- 使用asp.net读取并显示excel数据
- asp.net 读取并显示excel数据的实现代码
- net读取excel中数据并显示到gridview中
- Asp.net 在页面中显示XML
- ASP.NET中上传并读取Excel文件数据
- ext form 取得字段方法
- 一个字符串拷贝的百万次测试
- velocity基本语法
- MapInfo技术社区
- 读《阴丹士林》
- asp.net读取Excel表并显示在页面中
- 一个典型的C程序存储空间布局
- 发送电子邮件的方法
- 数据库字符串相加...函数...
- memcpy() memmove()和memccpy()
- 你不是真的想要100万
- 虚拟机的几种网络方式详解
- 我的個人特徵
- 项目管理要素:成功取决于三个阶段的努力