其他分享
首页 > 其他分享> > Aspose.Cells实现excel预览

Aspose.Cells实现excel预览

作者:互联网

aspose.cells是收费组件,支持java、.net、.net core,免费使用时候转换出的html页面会有水印“Evaluation Only. Created with Aspose.Cells”如果excel存在多个sheet,aspose转换出来的网页会带选项卡,点击选项卡会展示对应的sheet页面内容,展示效果比POI转换出的html效果的好。

首先在后台使用aspose读取excel文件并返回转换好的html文件目录返回给前台

private readonly ILogger _logger;
private readonly IWebHostEnvironment _webHostEnvironment;

    public HomeController(ILogger<HomeController> logger, IWebHostEnvironment webHostEnvironment)
    {
        _logger = logger;
        _webHostEnvironment = webHostEnvironment;
    }
    public IActionResult Index()
    {
        return View();
    }

    public IActionResult Privacy()
    {
        return View();
    }

    [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
    public IActionResult Error()
    {
        return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
    }
    /// <summary>
    /// 返回html地址
    /// </summary>
    /// <returns></returns>
    public string ExcelToHtml()
    {
        //程序根目录
        string rootpath = _webHostEnvironment.ContentRootPath;
        //程序下webroot根目录
        string webRootPath = _webHostEnvironment.WebRootPath;
        string filepath = webRootPath + "\\excelFile\\test.xlsx";
        //读取模板路径
        Workbook book = new Workbook(filepath);
        //filePath为保存文件的地址,需要服务端底下可以正常访问的路径
        book.Save(webRootPath+ "\\excelFile\\test.html", SaveFormat.Html);
        return  "\\excelFile\\test.html";
    }

标签:Aspose,Cells,收费组件,excel,POI
来源: