Use the Spreadsheet Document API to convert a document to various formats. Click Select File and load a document in XLSX, XLSB, XLS, XLSM, XLTX, XLTM, XLT, CSV, or TXT format. Select an output format from the list and click Download to save the result. The maximum file size you can upload is 1MB.
@model AspNetCoreDemos.OfficeFileAPI.SpreadsheetDocumentConversionModel
@using DevExtreme.AspNet.Mvc
@using (Html.BeginForm("SpreadsheetConversion", "DocumentConversion", FormMethod.Post)) {
@Html.HiddenFor(model => model.DocumentUrl)
<div class="uploader-panel">
@(Html.DevExtreme().FileUploader()
.ID("file-uploader")
.Name("myFile")
.Multiple(false)
.ShowFileList(true)
.Accept(".xlsx, .xlsm, .xlsb, .xls, .xltx, .xltm, .xlt, .xml, .csv, .txt")
.MaxFileSize(1048576)
.UploadMode(FileUploadMode.Instantly)
.UploadUrl(Url.Action("DocumentUpload", "SpreadsheetDocumentConversion"))
.OnValueChanged("fileUploader_valueChanged")
)
</div>
<div class="document-downloader">
<div class="document-downloader-item">Convert file to:</div>
<div class="document-downloader-format-selector">
@(Html.DevExtreme().SelectBoxFor(m => m.FileFormat)
.DataSource(Html.GetEnumSelectList<AspNetCoreDemos.OfficeFileAPI.SpreadsheetFileFormat>()
.Select(i => new { Value = int.Parse(i.Value), Text = i.Text }))
.ValueExpr("Value")
.DisplayExpr("Text")
)
</div>
<div class="document-downloader-item">
@(Html.DevExtreme().Button()
.ID("downloadButton")
.Text("Download")
.Disabled(true)
.Type(ButtonType.Default)
.StylingMode(ButtonStylingMode.Contained)
.UseSubmitBehavior(true)
.OnClick("DownloadClick")
)
</div>
<div id="error" class="error-message-right">
<span>@ViewBag.ErrorMessage</span>
</div>
</div>
<script>
function fileUploader_valueChanged(e) {
var files = e.value;
if (files.length > 0) {
$("#selected-files .selected-item").remove();
$.each(files, function (i, file) {
var $selectedItem = $("<div />").addClass("selected-item");
$selectedItem.append(file.name);
$selectedItem.appendTo($("#selected-files"));
});
$("#selected-files").show();
$('#DocumentUrl').val(files[0].name);
var button = $("#downloadButton").dxButton("instance");
button.option("disabled", false);
}
else
$("#selected-files").hide();
}
function DownloadClick() {
document.getElementById("error").hidden = true;
}
</script>
}
using System;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
namespace AspNetCoreDemos.OfficeFileAPI {
public partial class DocumentConversionController : DocumentProcessingController {
public DocumentConversionController(ILogger<DocumentConversionController> logger, IWebHostEnvironment hostingEnvironment)
: base(logger, hostingEnvironment) {
}
protected const string LoadDocumentExceptionString = "ERROR: Cannot convert this document.";
protected override string SessionKey => throw new NotImplementedException();
}
}
using System;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using DevExpress.Spreadsheet;
using DevExpress.XtraPrinting;
using DevExpress.XtraSpreadsheet.Export;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
namespace AspNetCoreDemos.OfficeFileAPI {
public class SpreadsheetDocumentConversionController : DocumentConversionController {
public SpreadsheetDocumentConversionController(ILogger<SpreadsheetDocumentConversionController> logger, IWebHostEnvironment hostingEnvironment)
: base(logger, hostingEnvironment) {
}
public static string SpreadsheetSessionKey => "SpreadsheetFileKey";
protected override string SessionKey => SpreadsheetSessionKey;
}
public partial class DocumentConversionController {
public IActionResult SpreadsheetConversion() {
SpreadsheetDocumentConversionModel model = new SpreadsheetDocumentConversionModel();
return View(model);
}
[HttpPost]
public IActionResult SpreadsheetConversion(SpreadsheetDocumentConversionModel model) {
Workbook workbook = new Workbook();
byte[] data;
try {
if(HttpContext.Session.TryGetValue(SpreadsheetDocumentConversionController.SpreadsheetSessionKey, out data))
workbook.LoadDocument(data);
MemoryStream stream = new MemoryStream();
SpreadsheetFileFormat fileFormat = model.FileFormat;
if(fileFormat == SpreadsheetFileFormat.Pdf) {
PdfExportOptions options = new PdfExportOptions();
options.ConvertImagesToJpeg = false;
workbook.ExportToPdf(stream, options);
}
else if(fileFormat == SpreadsheetFileFormat.Html) {
HtmlDocumentExporterOptions options = new HtmlDocumentExporterOptions();
options.EmbedImages = true;
options.AnchorImagesToPage = true;
workbook.ExportToHtml(stream, options);
}
else {
DocumentFormat format = SpreadsheetUtils.ConvertToFormat(fileFormat);
workbook.SaveDocument(stream, format);
}
string contentType = SpreadsheetUtils.ConvertToContentType(fileFormat);
string fileExtension = SpreadsheetUtils.ConvertToFileExtension(fileFormat);
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(Uri.UnescapeDataString(model.DocumentUrl));
return CreateFileStreamResult(stream, contentType, fileExtension, fileNameWithoutExtension);
} catch(Exception ex) {
Logger.LogError(ex, LoadDocumentExceptionString);
ViewBag.ErrorMessage = LoadDocumentExceptionString;
return View(model);
}
}
}
}
namespace AspNetCoreDemos.OfficeFileAPI {
public class SpreadsheetDocumentConversionModel : SpreadsheetModelBase {
public SpreadsheetDocumentConversionModel() {
FileFormat = SpreadsheetFileFormat.Pdf;
}
public string DocumentUrl { get; set; }
}
}
namespace AspNetCoreDemos.OfficeFileAPI {
public class SpreadsheetModelBase {
public SpreadsheetModelBase() {
PreviewModel = new SpreadsheetPreviewModel();
PreviewModel.OwnerPropertyName = "PreviewModel";
FileFormat = SpreadsheetFileFormat.Xlsx;
}
public SpreadsheetFileFormat FileFormat { get; set; }
public SpreadsheetPreviewModel PreviewModel { get; internal set; }
}
}