如图所示,怎样直接在asp.net页面中打开Excel
解决方案 »
- 移动控件selectionlist选择值的问题
- ASP.NET(C#)缓存的应用问题。
- web控件JS,css引用的问题.
- 公司的分类信息项目算是结束了.开心散分中...
- asp.net中怎么把response.write输出到表格中?
- 求解:用datagrid或table导出数据到excel文件,本身的网页被关闭?
- 紧急求助,数据绑定到下拉菜单时,如何控制数据不被编码
- [急]TreeView显示问题
- 请大家看看1+1=?这个贴,跪求,我都没分了(我真是想知道是怎么回事)
- 如何动态生成TemplateColumn中的项目?
- Request.QueryString
- 在添加客户信息时,希望编号能自动生成,求高手赐教!!! sql语句中加了个max,就出了问题
1)IFRAME src指向EXCEL文件,但要考虑客户端OFFICE版本问题
2)IFRAME src指向EXCEL生成的mht文件
3)考虑使用第三方控件,如FarPoint等
{
if ((txtFilePath.HasFile))
{
OleDbConnection conn = new OleDbConnection();
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter da = new OleDbDataAdapter();
DataSet ds = new DataSet();
string query = null;
string connString = "";
string strFileName = DateTime.Now.ToString("ddMMyyyy_HHmmss");
string strFileType = System.IO.Path.GetExtension(txtFilePath.FileName).ToString().ToLower();
//Check file type
if (strFileType == ".xls" || strFileType == ".xlsx")
{
txtFilePath.SaveAs(Server.MapPath("~/UploadedExcel/" + strFileName + strFileType));
}
else
{
lblMessage.Text = "Only excel files allowed";
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Visible = true;
return;
}
string strNewPath = Server.MapPath("~/UploadedExcel/" + strFileName + strFileType);
//Connection String to Excel Workbook
if (strFileType.Trim() == ".xls")
{
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
}
else if (strFileType.Trim() == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
}
query = "SELECT * FROM [Sheet1$]";
//query = "SELECT [Country],[Capital] FROM [Sheet1$] WHERE [Currency]=’Rupee’"
//query = "SELECT [Country],[Capital] FROM [Sheet1$]"
//Create the connection object
conn = new OleDbConnection(connString);
//Open connection
if (conn.State == ConnectionState.Closed) conn.Open();
//Create the command object
cmd = new OleDbCommand(query, conn);
da = new OleDbDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
grvExcelData.DataSource = ds.Tables[0];
grvExcelData.DataBind();
lblMessage.Text = "Data retrieved successfully! Total Records:" + ds.Tables[0].Rows.Count;
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Visible = true;
da.Dispose();
conn.Close();
conn.Dispose();
}
else
{
lblMessage.Text = "Please select an excel file first";
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Visible = true;
}
}
3楼的方法不行,这就是把数据直接读取到页面,我需要的是直接在页面显示Excel;
4楼的方法是输出excel
谢谢大家的回复,期待大家继续给出建议...