该博客演示了一个使用SheetJS库和canvas-datagrid组件来处理Excel文件的实时预览和导出功能。用户可以拖放Excel文件到网页上,数据将被解析并显示在canvas-datagrid中。此外,还提供了导出为XLSX文件的功能。源代码和问题报告链接也在页面中给出。
摘要由CSDN通过智能技术生成
<!DOCTYPE html>
<!-- xlsx.js (C) 2013-present SheetJS http://sheetjs.com -->
<!-- vim: set ts=2: -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>SheetJS + canvas-datagrid Live Demo</title>
<style>
#drop{
border:2px dashed #bbb;
-moz-border-radius:5px;
-webkit-border-radius:5px;
border-radius:5px;
padding:25px;
text-align:center;
font:20pt bold,"Vollkorn";color:#bbb
#b64data{
width:100%;
a { text-decoration: none }
</style>
</head>
<b><a href="http://sheetjs.com">SheetJS Data Preview Live Demo</a></b>
<a href="https://github.com/TonyGermaneri/canvas-datagrid">canvas-datagrid component library</a>
<a href="https://github.com/SheetJS/js-xlsx">Source Code Repo</a>
<a href="https://github.com/SheetJS/js-xlsx/issues">Issues? Something look weird? Click here and report an issue</a>
<div id="drop">Drop a spreadsheet file here to see sheet data</div>
<input type="file" name="xlfile" id="xlf" /> ... or click here to select a file
<textarea id="b64data">... or paste a base64-encoding here</textarea>
<b>Advanced Demo Options:</b>
<p><input type="submit" value="Export to XLSX!" id="xport" onclick="export_xlsx();" disabled="true"></p>
<div id="htmlout"></div>
<script src="https://unpkg.com/canvas-datagrid/dist/canvas-datagrid.js"></script>
<script src="shim.js"></script>
<script src="xlsx.full.min.js"></script>
<script>
/*jshint browser:true */
/* eslint-env browser */
/* eslint no-use-before-define:0 */
/*global Uint8Array, Uint16Array, ArrayBuffer */
/*global XLSX */
var cDg;
var process_wb = (function() {
var XPORT = document.getElementById('xport');
var HTMLOUT = document.getElementById('htmlout');
return function process_wb(wb) {
/* get data */
var ws = wb.Sheets[wb.SheetNames[0]];
var data = XLSX.utils.sheet_to_json(ws, {header:1});
/* update canvas-datagrid */
if(!cDg) cDg = canvasDatagrid({ parentNode:HTMLOUT, data:data });
cDg.style.height = '100%';
cDg.style.width = '100%';
cDg.data = data;
XPORT.disabled = false;
/* create schema (for A,B,C column headings) */
var range = XLSX.utils.decode_range(ws['!ref']);
for(var i = range.s.c; i <= range.e.c; ++i) cDg.schema[i - range.s.c].title = XLSX.utils.encode_col(i);
HTMLOUT.style.height = (window.innerHeight - 400) + "px";
HTMLOUT.style.width = (window.innerWidth - 50) + "px";
if(typeof console !== 'undefined') console.log("output", new Date());
})();
var do_file = (function() {
return function do_file(files) {
var f = files[0];
var reader = new FileReader();
reader.onload = function(e) {
if(typeof console !== 'undefined') console.log("onload", new Date());
var data = e.target.result;
data = new Uint8Array(data);
process_wb(XLSX.read(data, {type: 'array'}));
reader.readAsArrayBuffer(f);
})();
(function() {
var drop = document.getElementById('drop');
if(!drop.addEventListener) return;
function handleDrop(e) {
e.stopPropagation();
e.preventDefault();
do_file(e.dataTransfer.files);
function handleDragover(e) {
e.stopPropagation();
e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
drop.addEventListener('dragenter', handleDragover, false);
drop.addEventListener('dragover', handleDragover, false);
drop.addEventListener('drop', handleDrop, false);
})();
(function() {
var xlf = document.getElementById('xlf');
if(!xlf.addEventListener) return;
function handleFile(e) { do_file(e.target.files); }
xlf.addEventListener('change', handleFile, false);
})();
var export_xlsx = (function() {
function prep(arr) {
var out = [];
for(var i = 0; i < arr.length; ++i) {
if(!arr[i]) continue;
if(Array.isArray(arr[i])) { out[i] = arr[i]; continue };
var o = new Array();
Object.keys(arr[i]).forEach(function(k) { o[+k] = arr[i][k] });
out[i] = o;
return out;
return function export_xlsx() {
if(!cDg) return;
/* convert canvas-datagrid data to worksheet */
var new_ws = XLSX.utils.aoa_to_sheet(prep(cDg.data));
/* build workbook */
var new_wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(new_wb, new_ws, 'SheetJS');
/* write file and trigger a download */
XLSX.writeFile(new_wb, 'sheetjs.xlsx', {bookSST:true});
})();
</script>
<script type="text/javascript">
/* eslint no-use-before-define:0 */
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-36810333-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>
<!DOCTYPE html><!-- xlsx.js (C) 2013-present SheetJS http://sheetjs.com --><!-- vim: set ts=2: --><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>SheetJS + canvas-dat.
SheetJS js-xlsx
git地址:https://github.com/SheetJS/sheetjs#sheetjs-js-xlsx
可以查看更详细的官方文档
如何导入读取Excel文件
先在页面中写一个导入文件的地方,以功能为目的,先不要在意样式
再写个文件处理完点击输出的小button
然后再引入xlsx,想用jquery再引一个这个库
<input type="file" id="file1"><br>
<button style="margin-left:
<mx:Canvas width="25%" height="100%" borderStyle="solid" borderColor="black" shadowDistance="0" cornerRadius="8" dropShadowEnabled="true">
<mx:Canvas id="_mask
// 获取canvas元素和绘图上下文
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
// 创建一个Image对象并加载图片
var img = new Image();
img.src = "path/to/image.jpg";
// 等待图片加载完成后执行切割操作
img.onload = function() {
// 设置切割区域的起始坐标和宽高
var startX = 100;
var startY = 100;
var cropWidth = 200;
var cropHeight = 200;
// 绘制原始图片
ctx.drawImage(img, 0, 0);
// 获取切割后的图片数据
var imageData = ctx.getImageData(startX, startY, cropWidth, cropHeight);
// 创建一个新的Canvas用于显示切割后的图片
var cropCanvas = document.createElement("canvas");
cropCanvas.width = cropWidth;
cropCanvas.height = cropHeight;
var cropCtx = cropCanvas.getContext("2d");
// 将切割后的图片数据绘制到新的Canvas上
cropCtx.putImageData(imageData, 0, 0);
// 将切割后的图片显示在页面上
document.body.appendChild(cropCanvas);
在上述代码中,首先获取了Canvas元素和绘图上下文,然后创建一个Image对象并指定要加载的图片路径。在图片加载完成后,使用`drawImage()`方法将原始图片绘制到Canvas上。接下来,使用`getImageData()`方法获取指定区域的图片数据。然后,创建一个新的Canvas元素并设置其宽度和高度为切割区域的宽度和高度,并获取其绘图上下文。最后,使用`putImageData()`方法将切割后的图片数据绘制到新的Canvas上,并将该Canvas添加到页面上以显示切割后的图片。
请注意,上述代码中的`"myCanvas"`应该替换为你实际使用的Canvas元素的id,而`"path/to/image.jpg"`应该替换为你要加载的图片的路径。切割区域的起始坐标和宽高可以根据你的需求进行调整。
Enforcing a policy for regular password changes is no longer recommended, as it can result in more h
使用 PowerSell 脚本从 Windows PE 解锁 BitLocker 驱动器
什么是交换分区以及如何创建一个交换分区
Enforcing a policy for regular password changes is no longer recommended, as it can result in more h
使用 PowerSell 脚本从 Windows PE 解锁 BitLocker 驱动器
什么是交换分区以及如何创建一个交换分区