
xlsxcoreminjs是一款用于读取和处理Excel文件的JavaScript库,如果你在使用该库时遇到了“文件类型不正确”的异常,可能是以下原因导致的:
1 文件格式不正确:xlsxcoreminjs只能读取和处理Excel文件,如果你尝试读取其他类型的文件(如PDF、Word文档等),就会出现“文件类型不正确”的异常。因此,你需要确保你的文件是Excel格式的文件(xlsx、xls等)。
2 文件损坏:如果你的Excel文件损坏或者格式不正确,也会导致xlsxcoreminjs无法读取文件并抛出异常。你可以尝试打开文件并检查文件是否正常,或者尝试使用其他软件打开文件。
3 库版本不兼容:如果你使用的xlsxcoreminjs库版本过低或者过高,可能会导致无法正确读取Excel文件。你可以尝试更新或者降低库的版本,以确保与你的应用程序兼容。
4 其他问题:如果以上方法都无法解决问题,可能是其他未知问题导致的。你可以尝试查看错误日志或者联系xlsxcoreminjs的开发者或者社区,以获取更多帮助。
总之,如果你在使用xlsxcoreminjs时遇到了“文件类型不正确”的异常,需要检查文件格式、文件是否损坏、库版本是否兼容等方面的问题,找到具体原因后再采取相应的解决方法。
JavaScript is a versatile platform that allows easy customization of client-side scripting tools In some applications, it's useful to have some sort of spreadsheet interface that is easy to code and maintain The SpreadJS client-side JavaScript spreadsheet component, part of the SpreadJS package, is perfect for this
A JavaScript export to Excel
You can import and export Excel files, and provide users with an interface to interact with those files -- all in pure JavaScript In this tutorial, I'll show you how easy it is to add a SpreadJS component to an HTML page and import an Excel file into it
Set Up the JavaScript Spreadsheet Project
Create a new HTML page and add references to the SpreadSheets script and the CSS files that are included in your SpreadJS download:
<!DOCTYPE html> <html> <head>
<title>SpreadJS ExcelIO</title>
<script src=">
Then add a script to the page that initializes the SpreadSheets component, and a div element to contain it (since the SpreadJS spreadsheet component utilizes a canvas, this is necessary to initialize the component):
<script type="text/javascript">
$(document)ready(function () {
var workbook = new GCSpreadSheetsWorkbook(documentgetElementById("ss"));
});
</script> </head> <body>
<div id="ss" style="height:600px ; width :100%; "></div> </body>
Add Excel Import Code
We need to create an instance of the client-side ExcelIO component that we can use to actually open the file:
var excelIO = new GCSpreadExcelIO();
Then we need to add a function to import a file In this example, we import a local file, but you can do the same thing with a file on a server If you’re importing a file from a server, you need to reference the location The following is an example of an input element where the user can enter the location of the file:
<input type="text" id="importUrl" value=">
Once you have that, you can directly access that value in script code:
var excelUrl = $("#importUrl")val();
The following code for the import function just uses a local file for the "excelUrl" variable:
function ImportFile() {
var excelUrl = "/testxlsx";
var oReq = new XML>
Regardless of whether you're referencing a file on a server or locally, you'll need to add the following to your script inside the $(document)ready function:
$(document)ready(function () {
$supportcors = true;
workbook = new GCSpreadSheetsWorkbook(documentgetElementById("ss"));
// });
你可以搜寻 黑体字 , 如果需要更详细的介绍
一。导出Excel有两种:
1
function ExportExcel(tableid){//读取表格中每个单元到EXCEL中
try
{
var curTbl = documentgetElementById(tableid);
var oXL = new ActiveXObject("ExcelApplication"); //创建AX对象excel
var oWB = oXLWorkbooksAdd(); //获取workbook对象
var oSheet = oWBActiveSheet; //激活当前sheet
var Lenr = curTblrowslength; //取得表格行数
for (i = 0; i < Lenr; i++){
var Lenc = curTblrows(i)cellslength; //取得每行的列数
for (j = 0; j < Lenc; j++){
oSheetCells(i + 1, j + 1)value = curTblrows(i)cells(j)innerText; //赋值
}
}
oXLVisible = true; //设置excel可见属性
}
catch(e)
{
alert(emessage);
}
}
2:
function dataToExcel(tableid) {//整个表格拷贝到EXCEL中
var curTbl = documentgetElementById(tableid);
var oXL;
try {
oXL = GetObject("", "ExcelApplication");
}
catch (E) {
try {
oXL = new ActiveXObject("ExcelApplication");
}
catch (E2) {
//alert("Please confirm:\n1Microsoft Excel has been installed\n2Internet Options=>Security=>Setting \"Enable unsafe ActiveX\"");
alert("请确认:\n1机器上Excel已经安装\n2Internet 选项=>安全=>Internet \"对没有标记为安全的ActiveX控件进行初始化和脚本运行,设定为启用\"");
return;
}
}
//创建AX对象excel
var oWB = oXLWorkbooksAdd();
//获取workbook对象
var oSheet = oWBActiveSheet;
//激活当前sheet
var sel = documentbodycreateTextRange();
selmoveToElementText(curTbl);
//把表格中的内容移到TextRange中
selselect();
//全选TextRange中内容
selexecCommand("Copy");
//复制TextRange中内容
oSheetPaste();
//粘贴到活动的EXCEL中
oXLVisible = true;
//设置excel可见属性
}
问题:使用 koa-bodyparser 可以解析post提交的问题,但是发现获取不到formdata上传的文件。
后经查资料,改用 koa-body 解决:
constkoaBody = require('koa-body'); appuse(koaBody({ multipart:true, formidable: { maxFileSize:20010241024//设置上传文件大小最大限制,默认2M} }))
然后就可以在 ctxrequestfiles 里面获取到上传的 文件信息了。ctxrequestfiles 为一个对象,里面包含一个 file 字段(为你前端上传定义的值),值为文件信息,主要是文件name和path,path为临时存储路径。
二、解析xlsx文件信息
主要使用模块 node-xlsx :npm i xlsx --save
1、加载模块
使用模块:fs--主要用于写入文件;node-xlsx主要用于转换xlsx数据和读取xlsx的文件数据;
constfs = require('fs')constxlsx = require('node-xlsx');//读写xlsx的插件
2、读取功能
读取功能相对简单,直接使用模块的 prase(路径) 方法就能读取
let list = xlsxparse("/doc/helloxlsx");
返回结果,name:表名,data:二维数组。(由于Excel以表格的形式表示,因此表格的数据都是通过二维数组进行接收。)
需要注意的是:data里的第一行数组为表头的信息
[ { name:'firstSheet', data: [ [Array], [Array] ] }, { name:'secondSheet', data: [ [Array], [Array] ] } ]
3、写入功能
写入功能,对应的写入数据与上面读取功能的返回格式需要一致,以数组的形式,name:表名,data:二维数组(Excel每行每列的数据)
如下面代码,生成一个Excel文件,分别两张表,表名分别是firstSheet,secondSheet
let xlsxObj =[ { name:'firstSheet', data: [ [1,2,3], [4,5,6] ], }, { name:'secondSheet', data: [ [7,8,9], [10,1,12] ], } ]
生成Excel文件:
fswriteFileSync(路径,Buffer数据)
xlsxbuild(xlsxObj) 会将数组转换为Buffer数据
执行下面代码就会生成对应的Excel文件
fswriteFileSync('/doc/helloxlsx', xlsxbuild(xlsxObj),"binary")
完整代码:
//导出excelfunction tableToExcel(){
var arrSor = ["sorttable10","sorttable30","sorttable60","sorttable120"]
let str = "";
for (var k = 0; k < arrSorlength; k++) {
var tab=documentgetElementById(arrSor[k]);
var rows=tabrows;
const jsonData = [];
for(var i=2;i<rowslength;i++){ //遍历表格的行
var json = {};
for(var j=0;j<rows[i]cellslength;j++){ //遍历每行的列
json[(i+1)+"-"+(j+1)] = rows[i]cells[j]innerHTML;
}
jsonDatapush(json);
}
//列标题
let str1 = "<tr><td align='center' colspan='5'><b>最大"+arrSor[k]substring(9,arrSor[k]length)+"分钟雨量</b></td></tr>";
let str2 = "<tr align='center'><th>站点</th><th>站名</th><th>雨量最大值</th><th>降水时段</th><th>落入最大降水时段</th></tr>";
//循环遍历,每行加入tr标签,每个单元格加td标签
for(let i = 0 ; i < jsonDatalength ; i++ ){
str2+='<tr>';
for(let item in jsonData[i]){
//增加\t为了不让表格显示科学计数法或者其他格式
var itemTem= jsonData[i][item];
if (itemTem == "暂无数据") {
str2+=`<td colspan='5' align='center'>${ itemTem + '\t'}</td>`;
}else {
str2+=`<td align='center'>${ itemTem + '\t'}</td>`;
}
}
str2+='</tr>';
}
let str3 = "<tr><td colspan='5'></td></tr>";
str += (str1 + str2 + str3);
}
let worksheet = '雨量最大值'
let uri = 'data:application/vndms-excel;base64,';
//下载的表格模板数据
let template = `<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns=">
以上就是关于xlsx.core.min.js读取捕获异常文件类型不正确全部的内容,包括:xlsx.core.min.js读取捕获异常文件类型不正确、JavaScript 原生 提取excel文件 需要学习那些知识或者哪方面的,不要插件。最好有例子。、js怎样获取excel导出控件等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)