怎么样直接打开一个word/excel/pdf文件我想要的效果是:
点一下"打开"按钮后,直接就能显示文件的内容要求:
1.不是在IE中打开
2.不能出现打开,保存或者下载的对话框不要告诉我上www.baidu.com找
我花了不少时间找这个东西找不到才来问的
都是找不到满足第2点要求的答案
点一下"打开"按钮后,直接就能显示文件的内容要求:
1.不是在IE中打开
2.不能出现打开,保存或者下载的对话框不要告诉我上www.baidu.com找
我花了不少时间找这个东西找不到才来问的
都是找不到满足第2点要求的答案
就想像成都满足客观的条件,在服务器上打开服务器中的一个文件就行
这个要考虑到服务器的目录权限。 (可以把文件放到独立的一个文件夹下,,然后 设置权限,不可下载)至于直接打开,楼主是要什么效果? 像百度百科那样的? 那是FLASH插件。
protected void lbtnExcel_Click(object sender, EventArgs e)
{
PubFunction.GridViewToExcel(GridView1, "application/ms-excel", "MyExcel.xls");
}
//重写该方法 防主导出Excel出错
public override void VerifyRenderingInServerForm(Control control)
{
} /// <summary>
/// 将网格数据导出到Excel
/// </summary>
/// <param name="ctrl">网格名称(如GridView1) </param>
/// <param name="FileType">要导出的文件类型(Excel:application/ms-excel) </param>
/// <param name="FileName">要保存的文件名 </param>
public static void GridViewToExcel(Control ctrl, string FileType, string FileName)
{
//string style = @" <style> .text { mso-number-format:\@; } </script> ";
//string style1 = @" <style> .text { mso-number-format:yyyy/mm/dd; } </script> "; HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;//注意编码
HttpContext.Current.Response.AppendHeader("Content-Disposition",
"attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
HttpContext.Current.Response.ContentType = FileType;//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
ctrl.Page.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctrl.RenderControl(hw);
//HttpContext.Current.Response.Write(style);
//HttpContext.Current.Response.Write(style1); HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
见过问怎么不让客户端打开的,没见过你这样反过来的,呵呵
可以搞定
protected void Button1_Click(object sender, EventArgs e)
{
GridView1.Visible = true;
try
{
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("yyyyMMdd_HHmmss");
string strFileType = System.IO.Path.GetExtension(txtFilePath.FileName).ToString().ToLower();
//Check file type
if (strFileType == ".xls" || strFileType == ".xlsx")
{
txtFilePath.SaveAs(Server.MapPath("~/upfiles/" + strFileName + strFileType));
}
else
{
lblMessage.Text = "只可是EXCEL文件";
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Visible = true;
return;
}
string strNewPath = Server.MapPath("~/upfiles/" + 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=1'";
}
else if (strFileType.Trim() == ".xlsx")
{
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strNewPath + ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1'";
}
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); GridView1.DataSource = ds.Tables[0];
GridView1.DataBind(); lblMessage.Text = "导入成功,总行数为" + ds.Tables[0].Rows.Count;
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Visible = true; da.Dispose();
conn.Close();
conn.Dispose();
//将excel导入到sql的c表里
string strsql = "select * into c from OpenRowset ('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=" + strNewPath + "','select * from [sheet1$]')";
SqlConnection con = new SqlConnection("server=localhost;database=FFI_Product;uid=sa;pwd=sa");
SqlCommand sqlcomm = new SqlCommand(strsql, con);
SqlDataAdapter sdaa = new SqlDataAdapter();
sdaa.SelectCommand = sqlcomm;
con.Open();
DataSet dss = new DataSet();
sdaa.Fill(dss);
con.Close();
}
else
{
lblMessage.Text = "请选择EXCEL文件!";
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Visible = true;
}
}
}
catch
{
lblMessage.Text = "请先清除缓存";
lblMessage.ForeColor = System.Drawing.Color.Blue;
}
}
直接打開server端的文檔
期待高手解決