有用过OWC在网页上显示EXCEL文档的吗?
如果用过,可以告诉我方法吗?100分不够还可以加!
谢谢了先!
如果用过,可以告诉我方法吗?100分不够还可以加!
谢谢了先!
解决方案 »
- 在chrome下,ajax不能post数据
- Entity Framework 反序列化的问题
- 利用Repeater如何显示出这样的形式?
- VS2005 使用Jmail收发邮件
- 标识符“sql”超出范围
- 晕了,VS2005不能选择事件
- 有个关于 数据循环显示的问题。。在线等。。。谢谢。。。
- 向各位请教一个难题,关于用户自定义控件,100分
- 请问:数据导入到Excel,能不能实现在Excel中是下拉框?数据是从页面导过来的
- (大虾!你们在哪里??)真头痛!都两周了,还没有正确安装上VSNET2003.(急死了!)
- 有个难题,希望高手指教
- 如何通过OWC的SpreadSheet读取以前通过其导出的文件?
在安装Office2003以后,有一个ActiveX控件被安装到了系统中,这个控件位于“Program Files\Microsoft Office\OFFICE11\owssupp.dll”。通过这个控件,客户端页面上的JavaScript就可以激活本地的Office软件,来实现打开、编辑Office文档。(另,Office XP应该就已经包含这个ActiveX控件了。)
首先,用Script创建一个本地的对象:
openDocObj = new ActiveXObject("SharePoint.OpenDocuments.2"); // 为了兼容Office XP,可以创建“SharePoint.OpenDocuments.1”
然后,调用openDocObj的相应的方法。比如打开服务器上的一个Office文档:
openDocObj.ViewDocument("http://www.abc.com/documents/sample.doc");
openDocObj对象会根据参数中不同的Office文档类型(.doc、.xls、.ppt)来打开不同的程序(Word、Excel、PowerPoint)。ViewDocument()方法还有一个重载签名,可以让我们手工指定激活哪个程序来打开文档:
openDocObj.ViewDocument("http://www.abc.com/documents/sample.doc", 要激活的程序的ProgID);
那么要打开Office程序在线编辑文件又如何?
openDocObj.EditDocument("http://www.abc.com/documents/sample.doc");
就可以直接激活Word,在Word里面编辑文档,然后直接点击Word里面的保存功能,就可以将文件保存会服务器上了。注意:为了让Word能将编辑后的文档直接保存会服务器,访问Web站点的当前上下文的Windows Identity必须对服务器的相应目录(即“http://www.abc.com/documents”这个虚拟目录所对应的服务器上的物理路径)有相应的写权限,否则保存动作会失败。编辑完成后,EditDocument()会返回一个bool值,来反映编辑操作是否成功。
我们还可以通过打开服务器上的一个文档模版,来创建一个新的文档:
openDocObj.CreateNewDocument("http://www.abc.com/documents/sampleTemplate.dot", "http://www.abc.com/documents/");
就可以使用“http://www.abc.com/documents/sampleTemplate.dot”这个模版来创建一个新的文档,默认新文档的保存地点是“http://www.abc.com/documents/”。创建新文档时使用的程序取决于模版文件的类型(比如.dot模版会对应Word)。新文档的保存同样需要注意权限问题。CreateNewDocument()方法同样会返回一个bool值来反映操作是否成功。
CreateNewDocument()方法的第一个参数,除了可以使用一个模版的地址外,还可以直接指定为希望用来创建新文档的客户端程序的ProgID。
//导出到EXCEL的函数
public void ToExcel(System.Web.UI.Control ctl)
{
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.txt");
HttpContext.Current.Response.Charset = "gb2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType = "application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
// HttpContext.Current.Response.ContentType ="application/Microsoft Excel";
// ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
ctl.Visible = false;
}
但是我用这种方法导出的EXCEL表不能导入。导入的方法:
private void BindExcel(string Excel_File_Name)
{
string My_ExeclConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + Excel_File_Name + ";" +
"Extended Properties='Excel 8.0;HDR=YES;'" ;
string My_ExeclComm_Str = "Select * From [Sheet2$]";
OleDbDataAdapter My_ExeclAdapter = new OleDbDataAdapter(My_ExeclComm_Str, My_ExeclConn);
DataSet My_ExeclDataSet = new DataSet(); //Fill方法将数据从数据源复制到DataSet中
My_ExeclAdapter.Fill(My_ExeclDataSet);
Excel_DataGrid.DataSource = My_ExeclDataSet;
// Excel_DataGrid.DataMember = "Excel";
Excel_DataGrid.DataBind();
}
答:
用这个方法做出的Excel表是网页形式,是用MS—EXCEL强制打开的。本身是个网页。所以要打开这个。XLS文件,需要:
C_Act Use_Act = new C_Act();
Excel.Application myExcel = new Excel.Application();
Object oMissing=System.Reflection.Missing.Value;
Excel.Workbook Tw = myExcel.Application.Workbooks.Open(Excel_File_Name,oMissing,true,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing,oMissing);独占的方式打开这个文件。直接导出
this.Page.Response.AddHeader("Content-Disposition", "attachment; filename=Client.xls");
this.Page.Response.Charset = "GB2312";
this.Page.Response.ContentEncoding =System.Text.Encoding.Default;
this.Page.Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter textstring = new System.IO.StringWriter();
HtmlTextWriter htmltext = new HtmlTextWriter(textstring);
this.DataGrid1.RenderControl(htmltext);
this.Page.Response.Write(textstring);
this.Page.Response.End();
不错