【问题标题】:Open excel file in Client machine after saved on hive.保存在 hive 上后,在客户端计算机中打开 excel 文件。
【发布时间】:2014-04-30 15:20:38
【问题描述】:

我想在 excel 表上写 DataTable 并在客户端机器中打开。让客户将文件保存在他喜欢的位置。我已经创建了文件并保存在 hive 上。但无法在客户端机器上打开它。我将 interop.dll 用于 excel。

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

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

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

请帮忙。

【问题讨论】:

    标签: c# sharepoint-2010 excel-interop


    【解决方案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;
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2017-03-15
      • 1970-01-01
      • 1970-01-01
      • 2013-03-17
      • 2020-12-03
      相关资源
      最近更新 更多