保存在配置单元后,在客户端计算机中打开excel文件。

时间:2014-04-29 10:25:47

标签: c# sharepoint-2010 excel-interop

我想在Excel工作表上编写 DataTable 并在客户端计算机中打开。让客户端将文件保存在他的首选位置。我创建了该文件并保存在配置单元中。但无法在客户端计算机上打开它。我正在使用 interop.dll for excel。

excelWorkBook.Saved = true;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
    excelWorkBook.SaveCopyAs(filename);
});

excelWorkBook.Close(Missing.Value, Missing.Value, Missing.Value);
excelWorkBook = null;
excelApp.Quit();

我想在客户端计算机上打开保存的Excel工作表,并在客户端将其保存到其位置后从配置单元中删除该文件。

请帮忙。

1 个答案:

答案 0 :(得分:1)

试试这个。这将打开一个保存文件对话框。因此,用户可以将文件保存在他想要的地方..

try
        {
            string XlsPath = Server.MapPath(@"~/Resources/test.xls");// give ur file path here (where it is stored, in ur case ur Hive path)
            FileInfo fileDet = new System.IO.FileInfo(XlsPath);
            Response.Clear();
            Response.Charset = "UTF-8";
            Response.ContentEncoding = Encoding.UTF8;
            Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fileDet.Name));
            Response.AddHeader("Content-Length", fileDet.Length.ToString());
            Response.ContentType = "application/ms-excel";
            Response.WriteFile(fileDet.FullName);
            Response.End();
        }
        catch (Exception ex)
        {
          throw ex; 
        }

这是使用SharePoint Web部件

的方法
try
        {
            using (SPSite site = new SPSite(SPContext.Current.Web.Url))
            {
                SPWeb currentWeb = site.RootWeb;
                currentWeb.ParserEnabled = false;
                SPFile spFile = currentWeb.GetFile(@"/Shared%20Documents/test.xls"); // ur documet url saved in document library
                string localFileName = Path.Combine(@"c:\Users\anbuj\Documents\Backup", string.Format("{0}.xls","tempfile")); // tenpFilePath is where u wanna save ur file
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    FileStream outStream = new FileStream(localFileName, FileMode.Create);
                    byte[] fileData = spFile.OpenBinary();
                    outStream.Write(fileData, 0, fileData.Length);
                    outStream.Close();
                }
                    );

            }
        }
        catch(Exception ex)
        {
            throw ex;
        }