简体   繁体   English

以编程方式打开与表格模型连接的 Excel 文档

[英]Programmatically open an Excel document with connection to a Tabular Model

I would like to create a spreadsheet programmatically with a connection to my Tabular Model that exists in an Azure Analysis Server when the user clicks on some button.当用户单击某个按钮时,我想以编程方式创建一个电子表格,并连接到 Azure 分析服务器中存在的表格模型。 Similar to the option available in Azure to View Tabular Model which creates an odc file which you can open in Excel.类似于 Azure 中可用于查看表格模型的选项,该选项创建一个可在 Excel 中打开的odc文件。 My application is hosted on Azure and I am using .NET Core 2.2 as back-end.我的应用程序托管在 Azure 上,我使用 .NET Core 2.2 作为后端。

I am quite clueless how can I achieve this.我很不知道我怎么能做到这一点。 Has anyone managed to implement such functionality?有没有人设法实现这样的功能?

The simplest thing is to just generate and deliver an odc file, which will open with Excel.最简单的方法是生成并交付一个odc文件,该文件将用 Excel 打开。

First create an .odc file pointing to your AAS server, per docs: Create an Office Data Connection file .首先根据文档创建一个指向 AAS 服务器的 .odc 文件: 创建 Office 数据连接文件

Then add that .odc file to your web app, and serve it through a controller like this:然后将该 .odc 文件添加到您的网络应用程序,并通过这样的控制器提供它:

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using System.IO;
using System.Text;

namespace WebApplication4.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class OdcController : ControllerBase
    {
        private IHostingEnvironment hostingEnvironment;
        public OdcController(IHostingEnvironment env)
        {
            hostingEnvironment = env;
        }
        // GET api/values
        [HttpGet]
        public ActionResult Get()
        {
            var fp = Path.Combine(_env.ContentRootPath, "model.odc");
            var odcText = System.IO.File.ReadAllText(fp);
            //optionally modify odcText
            return File(Encoding.ASCII.GetBytes(odcText), "application/octet-stream", "model.odc");
        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM