Using the FlexCel ASP viewer component (C# / netframework)
Note
This demo is available in your FlexCel installation at <FlexCel Install Folder>\samples\csharp\VS2022\netframework\25.Printing and Exporting\50.ASP.NET HTML Viewer and also at https://github.com/tmssoftware/TMS-FlexCel.NET-demos/tree/master/csharp/VS2022/netframework/Modules/25.Printing and Exporting/50.ASP.NET HTML Viewer
Overview
A simple example on how to use the component FlexCelAspViewer to display xls files as html in a web browser.
Note
This demo does not run from MainDemo and it needs **.NET 2.0 or newer.
In order to run this demo, open the website from Visual Studio 2005 or newer, by doing: Menu->File->Open->Website
and pointing to the Modules\25.Printing and Exporting\50.ASP.NET HTML Viewer folder.
Concepts
In this demo we will use .TemporaryFiles to create the images. This will create all images as GUIDs in the "images" folder. Images older than 15 minutes will be deleted when a new page is requested, and there is an "overflow guard" that prevents having more than 5000 images in the images folder.
If you wanted to use HttpHandlers to return the images, you would need to add the following lines to web.config:
<httpHandlers> <add verb="*" path="flexcelviewer.ashx" type="FlexCel.AspNet.UniqueTemporaryFilesImageHandler,FlexCel.AspNet"/> </httpHandlers>
This has already been done in the example, so you just need to switch the image mode to use UniqueTemporaryFiles.
In order to delete the temporary images once the timeout has happened, we use ImageTimeout and MaxTemporaryImages. Make sure you have rights to delete images in the image folders for this to work. You could also manually delete the images with a script on the server.
FlexCelAspViewer is great for small sites, but due to the way it handles images, (make sure you read the [
) it might not scale enough if you do not provide a custom image handler. ](~/guides/html-exporting-guide.md# -----it-might-not-scale-enough-if-you-do-not-provide --a-custom-image-handler )
Files
Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FlexCel.Core;
using FlexCel.XlsAdapter;
using FlexCel.Render;
using FlexCel.AspNet;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
XlsFile xls = new XlsFile();
string DefaultFile = Server.MapPath("~/default.xls");
if (IsPostBack)
{
if (Uploader.HasFile)
{
xls.Open(Uploader.FileContent);
}
else
{
xls.Open(DefaultFile);
}
}
else
{
xls.Open(DefaultFile);
}
Viewer.HtmlExport.ImageNaming = TImageNaming.Guid;
Viewer.HtmlExport.Workbook = xls;
Viewer.RelativeImagePath = "images";
Viewer.HtmlExport.FixIE6TransparentPngSupport = true; //This is only needed if you are using IE and there are transparent png files.
Viewer.ImageExportMode = TImageExportMode.TemporaryFiles;
}
}