将 Excel 转换为 Pdf、图像及其他格式
将 Excel 工作簿转换为 PDF
PDF文件被广泛用于组织、政府部门和个人之间交换文档。它是一种标准文档格式,软件开发人员经常被要求找到一种方法将Microsoft Excel文件转换为PDF文档。
Aspose.Cells支持将Excel文件转换为PDF,并在转换过程中保持高度的视觉保真度。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Save As PDF Example</title>
</head>
<body>
<h1>Save Excel as PDF Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Convert to PDF</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
const resultDiv = document.getElementById('result');
if (!fileInput.files.length) {
resultDiv.innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object by opening the Excel file through the file stream
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Save the document in PDF format
const outputData = workbook.save(SaveFormat.Pdf);
const blob = new Blob([outputData], { type: 'application/pdf' });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.pdf';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download PDF File';
resultDiv.innerHTML = '<p style="color: green;">Conversion completed successfully! Click the download link to get the PDF file.</p>';
});
</script>
</html>
将 Excel 工作簿转换为 JPG
Aspose.Cells 支持将 Excel 文件转换为 JPG。以下代码示例展示如何将工作簿保存为 JPG。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Convert Workbook to JPG Example</title>
</head>
<body>
<h1>Convert Workbook to JPG Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Convert to JPG</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiate Workbook from uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Convert workbook to JPG image
const outputData = workbook.save(SaveFormat.Jpeg);
const blob = new Blob([outputData], { type: 'image/jpeg' });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'Image1.jpg';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download JPG Image';
document.getElementById('result').innerHTML = '<p style="color: green;">Conversion completed. Click the download link to get the JPG image.</p>';
});
</script>
</html>
将Excel工作簿转换为图像
Aspose.Cells 支持将 Excel 文件转换为图片。以下代码示例展示如何将工作簿保存为图片。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells: Convert Workbook to Images</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Convert to Images</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
<div id="downloads"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
document.getElementById('result').innerHTML = '<p>Converting workbook to images...</p>';
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object from uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Define desired image formats
const formats = [
{ fmt: SaveFormat.Bmp, name: 'Image1.bmp', mime: 'image/bmp' },
{ fmt: SaveFormat.Jpeg, name: 'Image1.jpg', mime: 'image/jpeg' },
{ fmt: SaveFormat.Png, name: 'Image1.png', mime: 'image/png' },
{ fmt: SaveFormat.Emf, name: 'Image1.emf', mime: 'image/emf' },
{ fmt: SaveFormat.Gif, name: 'Image1.gif', mime: 'image/gif' }
];
const downloadsDiv = document.getElementById('downloads');
downloadsDiv.innerHTML = '';
// Convert and create download links for each image format
for (const f of formats) {
const outputData = workbook.save(f.fmt);
const blob = new Blob([outputData], { type: f.mime });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = f.name;
a.textContent = 'Download ' + f.name;
a.style.display = 'block';
downloadsDiv.appendChild(a);
}
document.getElementById('result').innerHTML = '<p style="color: green;">Conversion completed. Click the links below to download the images.</p>';
});
</script>
</html>
将Excel工作簿转换为XPS
XPS文档格式由结构化的XML标记组成,用于定义文档的布局和每个页面的视觉外观,同时还包括用于分发、归档、渲染、处理和打印文档的渲染规则。
XPS的标记语言是XAML的子集,允许在文档中包含矢量图形元素,使用XAML来标记Windows Presentation Foundation(WPF)的基元。所使用的元素是根据路径和其他几何原语来描述的。
实际上,XPS文件是一个使用开放打包约定的Unicode ZIP存档,包含构成文档的文件。这些文件包括每个页面的XML标记文件、文本、嵌入字体、光栅图像、2D矢量图形,以及数字版权管理信息。可以通过在支持ZIP文件的应用程序中打开XPS文件来查看其内容。
从Aspose.Cells 6.0.0开始,支持将Microsoft Excel转换为XPS。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Render to XPS</title>
</head>
<body>
<h1>Render Worksheet / Workbook to XPS</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx" />
<button id="runExample">Run Example</button>
<div>
<a id="downloadLinkSheet" style="display: none; margin-right: 10px;">Download Sheet XPS</a>
<a id="downloadLinkWorkbook" style="display: none;">Download Workbook XPS</a>
</div>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, XpsSaveOptions, SheetSet } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
const resultDiv = document.getElementById('result');
const downloadLinkSheet = document.getElementById('downloadLinkSheet');
const downloadLinkWorkbook = document.getElementById('downloadLinkWorkbook');
if (!fileInput.files.length) {
resultDiv.innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
// Read file from input
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Open workbook from uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Get the first worksheet
const sheet = workbook.worksheets.get(0);
// Render the sheet to XPS
const options = new XpsSaveOptions();
const sheetSet = new SheetSet([sheet.index]);
options.sheetSet = sheetSet;
const outputDataSheet = workbook.save(SaveFormat.Xps, options);
const blobSheet = new Blob([outputDataSheet], { type: 'application/vnd.ms-xps' });
downloadLinkSheet.href = URL.createObjectURL(blobSheet);
downloadLinkSheet.download = 'out_printingxps.out.xps';
downloadLinkSheet.style.display = 'inline-block';
downloadLinkSheet.textContent = 'Download Sheet XPS';
// Export the whole workbook to XPS
const outputDataWorkbook = workbook.save(SaveFormat.Xps, new XpsSaveOptions());
const blobWorkbook = new Blob([outputDataWorkbook], { type: 'application/vnd.ms-xps' });
downloadLinkWorkbook.href = URL.createObjectURL(blobWorkbook);
downloadLinkWorkbook.download = 'out_whole_printingxps.out.xps';
downloadLinkWorkbook.style.display = 'inline-block';
downloadLinkWorkbook.textContent = 'Download Workbook XPS';
resultDiv.innerHTML = '<p style="color: green;">XPS files generated. Use the links above to download the sheet and workbook XPS files.</p>';
});
</script>
</html>
将 Excel 转换为 Ods、Sxc 和 Fods(OpenOffice / LibreOffice Calc)
Aspose.Cells 支持将 Excel 文件转换为 Ods、Sxc 和 Fods 格式。以下代码示例展示如何将 模板 转换为 Ods、Sxc 和 Fods 文件。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Save As Multiple Formats Example</title>
<meta charset="utf-8" />
</head>
<body>
<h1>Save As ODS / SXC / FODS Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Convert and Download</button>
<div style="margin-top: 10px;">
<a id="downloadLinkOds" style="display: none; margin-right: 10px;">Download ODS</a>
<a id="downloadLinkSxc" style="display: none; margin-right: 10px;">Download SXC</a>
<a id="downloadLinkFods" style="display: none; margin-right: 10px;">Download FODS</a>
</div>
<div id="result" style="margin-top: 10px;"></div>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
const result = document.getElementById('result');
if (!fileInput.files.length) {
result.innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object from the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Save as ods file
const outputOds = workbook.save(SaveFormat.Ods);
const blobOds = new Blob([outputOds]);
const downloadLinkOds = document.getElementById('downloadLinkOds');
downloadLinkOds.href = URL.createObjectURL(blobOds);
downloadLinkOds.download = 'Out.ods';
downloadLinkOds.style.display = 'inline-block';
downloadLinkOds.textContent = 'Download ODS File';
// Save as sxc file
const outputSxc = workbook.save(SaveFormat.Sxc);
const blobSxc = new Blob([outputSxc]);
const downloadLinkSxc = document.getElementById('downloadLinkSxc');
downloadLinkSxc.href = URL.createObjectURL(blobSxc);
downloadLinkSxc.download = 'Out.sxc';
downloadLinkSxc.style.display = 'inline-block';
downloadLinkSxc.textContent = 'Download SXC File';
// Save as fods file
const outputFods = workbook.save(SaveFormat.Fods);
const blobFods = new Blob([outputFods]);
const downloadLinkFods = document.getElementById('downloadLinkFods');
downloadLinkFods.href = URL.createObjectURL(blobFods);
downloadLinkFods.download = 'Out.fods';
downloadLinkFods.style.display = 'inline-block';
downloadLinkFods.textContent = 'Download FODS File';
result.innerHTML = '<p style="color: green;">Files converted successfully! Click the download links to get the converted files.</p>';
});
</script>
</html>
将Excel工作簿转换为MHTML文件
MHTML结合了普通HTML以及外部资源(通常是链接的内容,如图像、动画、音频等)到一个文件中。它们通常用于以.mht文件扩展名的电子邮件。
Aspose.Cells支持读取和写入MHTML文件。
下面的代码示例显示了如何将工作簿保存为MHTML文件。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Convert Excel to MHT Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, HtmlSaveOptions } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
window.asposeCellsReady = true;
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
const resultDiv = document.getElementById('result');
if (!fileInput.files.length) {
resultDiv.innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
if (!window.asposeCellsReady) {
resultDiv.innerHTML = '<p style="color: red;">Aspose.Cells is not initialized yet. Please wait and try again.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiate a workbook and open the uploaded XLSX file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Specify the HTML Saving Options
const sv = new HtmlSaveOptions(SaveFormat.MHtml);
// Save the MHT file (returns binary data)
const outputData = workbook.save(SaveFormat.MHtml, sv);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = `${file.name}.out.mht`;
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download MHT File';
resultDiv.innerHTML = '<p style="color: green;">MHT file generated successfully. Click the download link to get the file.</p>';
});
</script>
</html>
将Excel工作簿转换为HTML
Aspose.Cells API 提供了导出电子表格为 HTML 格式的支持。为此,Aspose.Cells 使用 HtmlSaveOptions 类,提供控制输出 HTML 各个方面的灵活性。
下面的代码示例显示了如何将工作簿保存为HTML文件。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Converting Excel to HTML</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Convert to HTML</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
const resultDiv = document.getElementById('result');
if (!fileInput.files.length) {
resultDiv.innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Load your sample excel file in a workbook object
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Save it in HTML format
const outputData = workbook.save(SaveFormat.Html);
const blob = new Blob([outputData], { type: 'text/html' });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'ConvertingToHTMLFiles_out.html';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download HTML File';
resultDiv.innerHTML = '<p style="color: green;">Conversion completed successfully! Click the download link to get the HTML file.</p>';
});
</script>
</html>
为HTML设置图像首选项
从 8.0.2 版本起,Aspose.Cells 已将 imageOptions 公开给 HtmlSaveOptions 类,使开发者在将电子表格保存为 HTML 格式时可以指定图片偏好。
以下是可以应用的一些图像设置的详细信息。
- ImageType:指定图像类型。请注意,所有形状,包括图表,在输出HTML中呈现为图像。
- quality:指定图片的质量(0 到 100),当 ImageType 被指定为 Jpeg 时。
- verticalResolution:获取或设置图像的垂直分辨率(每英寸点数)。
- horizontalResolution:获取或设置图像的水平分辨率(每英寸点数)。
- TiffCompression:在 ImageType 被指定为 Tiff 时,获取或设置图片的压缩类型。
- transparent:指示当ImageFormat指定为Png时,图像的背景是否应该是透明的。
将Excel工作簿转换为Markdown
Aspose.Cells API 支持将电子表格导出为 Markdown 格式。要将活动工作表导出为 Markdown,请将 SaveFormat.Markdown 作为参数传递给 Workbook.save(string, SaveOptions) 方法的第二个参数。也可以使用 MarkdownSaveOptions 类指定导出工作表到 Markdown 的其他设置。
以下代码示例演示如何使用 SaveFormat.Markdown 枚举成员将活动工作表导出为 Markdown。请参考由代码生成的 输出 Markdown 文件。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Save as Markdown</title>
</head>
<body>
<h1>Save Excel as Markdown Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Convert to Markdown</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object by opening the uploaded Excel file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Saving as Markdown
const outputData = workbook.save(SaveFormat.Markdown);
const blob = new Blob([outputData], { type: 'text/markdown' });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'Book1.md';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Markdown File';
document.getElementById('result').innerHTML = '<p style="color: green;">Conversion completed successfully! Click the download link to get the Markdown file.</p>';
});
</script>
</html>
将Excel工作簿转换为JSON
Aspose.Cells 支持将工作簿转换为 Json(JavaScript 对象表示法)文件。
以下代码示例演示如何使用 SaveFormat.Json 枚举成员将活动工作表导出为 Json。请参考代码将 源文件 转换为 输出 Json 文件。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Convert Workbook to JSON</title>
</head>
<body>
<h1>Convert Workbook to JSON</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Convert to JSON</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, Worksheet, Cell } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object from the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Convert the workbook to JSON format
const outputData = workbook.save(SaveFormat.Json);
// Create a downloadable blob for the JSON result
const blob = new Blob([outputData], { type: 'application/json' });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'book1.json';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download JSON File';
document.getElementById('result').innerHTML = '<p style="color: green;">Workbook converted to JSON successfully! Click the download link to get the JSON file.</p>';
});
</script>
</html>
将Excel转换为XML
Aspose.Cells 支持将工作簿转换为 Excel 2003 电子表格 XML 和普通 XML 数据。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Convert Workbook to XML Examples</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<br/><br/>
<a id="downloadLink1" style="display: none; margin-right: 10px;">Download Spreadsheet XML</a>
<a id="downloadLink2" style="display: none;">Download Data XML</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, XmlSaveOptions } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object from uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Save as Excel 2003 Spreadsheet XML
const spreadsheetData = workbook.save(SaveFormat.Excel2003Xml);
const blob1 = new Blob([spreadsheetData]);
const downloadLink1 = document.getElementById('downloadLink1');
downloadLink1.href = URL.createObjectURL(blob1);
downloadLink1.download = 'Spreadsheet.xml';
downloadLink1.style.display = 'inline-block';
downloadLink1.textContent = 'Download Spreadsheet XML';
// Save as plain XML data with XmlSaveOptions
const xmlSaveOptions = new XmlSaveOptions();
const dataXml = workbook.save(SaveFormat.SpreadsheetML, xmlSaveOptions);
const blob2 = new Blob([dataXml]);
const downloadLink2 = document.getElementById('downloadLink2');
downloadLink2.href = URL.createObjectURL(blob2);
downloadLink2.download = 'data.xml';
downloadLink2.style.display = 'inline-block';
downloadLink2.textContent = 'Download Data XML';
document.getElementById('result').innerHTML = '<p style="color: green;">Conversion completed successfully! Use the links above to download the generated XML files.</p>';
});
</script>
</html>
将Excel工作簿转换为TIFF
Aspose.Cells 支持将工作簿转换为 TIFF 文件。
下面的代码片段显示了如何将Excel转换为TIFF:
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Convert Excel to TIFF</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
const resultDiv = document.getElementById('result');
if (!fileInput.files.length) {
resultDiv.innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object by opening the Excel file from the file input
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Save workbook to TIFF format
const outputData = workbook.save(SaveFormat.Tiff);
const blob = new Blob([outputData], { type: 'image/tiff' });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'out.tiff';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download TIFF File';
resultDiv.innerHTML = '<p style="color: green;">File converted to TIFF successfully! Click the download link to get the TIFF file.</p>';
});
</script>
</html>
将Excel工作簿转换为DOCX
Aspose.Cells API 支持将电子表格转换为 DOCX 格式。将工作簿导出为 DOCX 时,请将 SaveFormat.Docx 作为参数传递给 Workbook.save(string, SaveOptions) 方法的第二个参数。也可以使用 DocxSaveOptions 类指定导出工作表到 DOCX 的其他设置。
以下代码示例演示如何使用 SaveFormat.Docx 枚举成员将活动工作表导出为 DOCX。请参考由代码生成的 输出 DOCX 文件。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiate Workbook from the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Save as DOCX (Markdown/Docx conversion per original code)
const outputData = workbook.save(SaveFormat.Docx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'Book1.docx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Docx File';
document.getElementById('result').innerHTML = '<p style="color: green;">File converted successfully! Click the download link to get the DOCX file.</p>';
});
</script>
</html>
将Excel工作簿转换为PPTX
Aspose.Cells API 支持将电子表格转换为 PPTX 格式。将工作簿导出为 PPTX 时,请将 SaveFormat.Pptx 作为参数传递给 Workbook.save(string, SaveOptions) 方法的第二个参数。也可以使用 PptxSaveOptions 类指定导出工作表到 PPTX 的其他设置。
以下代码示例演示如何使用 SaveFormat.Pptx 枚举成员将活动工作表导出为 PPTX。请参考由代码生成的 输出 PPTX 文件。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Save as PPTX Example</title>
</head>
<body>
<h1>Save Excel as PPTX Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Convert to PPTX</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, Worksheet, Cell } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object by opening the uploaded Excel file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Save as PPTX
const outputData = workbook.save(SaveFormat.Pptx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
const baseName = file.name.replace(/\.[^/.]+$/, "");
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = baseName + '.pptx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Converted PPTX File';
document.getElementById('result').innerHTML = '<p style="color: green;">File converted successfully! Click the download link to get the PPTX file.</p>';
});
</script>
</html>
将 Excel 工作簿转换为 EPUB
Aspose.Cells API 支持将电子表格转换为 EPUB 格式。将工作簿导出为 EPUB 时,请将 SaveFormat.Epub 作为参数传递给 Workbook.save(string, SaveOptions) 方法的第二个参数。也可以使用 EBookSaveOptions 类指定导出工作表到 Epub 的其他设置。
以下代码示例演示如何使用 SaveFormat.Epub 枚举成员将活动工作表导出为 EPUB。
<!DOCTYPE html>
<html>
<head>
<title>Converting To EPUB Files</title>
</head>
<body>
<h1>Converting To EPUB Files</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Convert to EPUB</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
const result = document.getElementById('result');
if (!fileInput.files.length) {
result.innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Load your sample excel file in a workbook object
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Save it in EPUB format
const outputData = workbook.save(SaveFormat.Epub);
const blob = new Blob([outputData], { type: 'application/epub+zip' });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'ConvertingToEPUBFiles_out.epub';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download EPUB File';
result.innerHTML = '<p style="color: green;">File converted to EPUB successfully! Click the download link to get the EPUB file.</p>';
});
</script>
</html>
将 Excel 工作簿转换为 AZW3
Aspose.Cells API 支持将电子表格转换为 AZW3 格式。将工作簿导出为 AZW3 时,请将 SaveFormat.Azw3 作为参数传递给 Workbook.save(string, SaveOptions) 方法的第二个参数。也可以使用 EBookSaveOptions 类指定导出工作表到 AZW3 的其他设置。
以下代码示例演示如何利用 SaveFormat.Azw3 枚举成员将活动工作表导出为 AZW3。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Convert to AZW3 Example</title>
</head>
<body>
<h1>Convert Excel to AZW3 (EPUB) Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx" />
<button id="runExample">Convert to AZW3</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Load your sample excel file in a workbook object
const wb = new Workbook(new Uint8Array(arrayBuffer));
// Save it in AZW3 format
const outputData = wb.save(SaveFormat.Azw3);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'ConvertingToEPUBFiles_out.azw3';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download AZW3 File';
document.getElementById('result').innerHTML = '<p style="color: green;">Conversion completed successfully! Click the download link to get the AZW3 file.</p>';
});
</script>
</html>