㈠ 怎麼用js將excel中的數據讀取後顯示到網頁中的表格
以前讀書的時候絕不會想到會用客戶端腳本來實現這些功能,現在卻一開始就要用上了,而且還覺得挺實用的。
參考《Windows腳本技術》,應該會有一點收獲。
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
</head>
<script language="javascript" type="text/javascript">
function importXLS(fileName)
{
objCon = new ActiveXObject("ADODB.Connection");
objCon.Provider = "Microsoft.Jet.OLEDB.4.0";
objCon.ConnectionString = "Data Source=" + fileName + ";Extended Properties=Excel 8.0;";
objCon.CursorLocation = 1;
objCon.Open;
var strQuery;
//Get the SheetName
var strSheetName = "Sheet1$";
var rsTemp = new ActiveXObject("ADODB.Recordset");
rsTemp = objCon.OpenSchema(20);
if(!rsTemp.EOF)
strSheetName = rsTemp.Fields("Table_Name").Value;
rsTemp = null
rsExcel = new ActiveXObject("ADODB.Recordset");
strQuery = "SELECT * FROM [" + strSheetName + "]";
rsExcel.ActiveConnection = objCon;
rsExcel.Open(strQuery);
while(!rsExcel.EOF)
{
for(i = 0;i<rsExcel.Fields.Count;++i)
{
alert(rsExcel.Fields(i).value);
}
rsExcel.MoveNext;
}
// Close the connection and dispose the file
objCon.Close;
objCon =null;
rsExcel = null;
}
</script>
</head>
<body>
<input type="file" id="f" />
<input type="button" id="b" value="import" onclick="if(f.value=='')alert('請選擇xls文件');else importXLS(f.value)" />
</body>
</html>
trackback:http://hi..com/netcorner/blog/item/4c35a818788f670635fa41d3.html
通過Javascript操作Excel
function AutomateExcel()
{
// Start Excel and get Application object.
var oXL = new ActiveXObject("Excel.Application");
oXL.Visible = true;
// Get a new workbook.
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
// Add table headers going cell by cell.
oSheet.Cells(1, 1).Value = "First Name";
oSheet.Cells(1, 2).Value = "Last Name";
oSheet.Cells(1, 3).Value = "Full Name";
oSheet.Cells(1, 4).Value = "Salary";
// Format A1:D1 as bold, vertical alignment = center.
oSheet.Range("A1", "D1").Font.Bold = true;
oSheet.Range("A1", "D1").VerticalAlignment = -4108; //xlVAlignCenter
// Create an array to set multiple values at once.
// Fill A2:B6 with an array of values (from VBScript).
oSheet.Range("A2", "B6").Value = CreateNamesArray();
// Fill C2:C6 with a relative formula (=A2 & " " & B2).
var oRng = oSheet.Range("C2", "C6");
oRng.Formula = "=A2 & " " & B2";
// Fill D2:D6 with a formula(=RAND()*100000) and apply format.
oRng = oSheet.Range("D2", "D6");
oRng.Formula = "=RAND()*100000";
oRng.NumberFormat = "$0.00";
// AutoFit columns A:D.
oRng = oSheet.Range("A1", "D1");
oRng.EntireColumn.AutoFit();
// Manipulate a variable number of columns for Quarterly Sales Data.
DispalyQuarterlySales(oSheet);
// Make sure Excel is visible and give the user control
// of Excel's lifetime.
oXL.Visible = true;
oXL.UserControl = true;
}
<HTML>
<HEAD>
<TITLE>將頁面中指定表格的數據導入到Excel中</TITLE>
<SCRIPT LANGUAGE="javascript">
<!--
function AutomateExcel()
{
var oXL = new ActiveXObject("Excel.Application"); //創建應該對象
var oWB = oXL.Workbooks.Add();//新建一個Excel工作簿
var oSheet = oWB.ActiveSheet;//指定要寫入內容的工作表為活動工作表
var table = document.all.data;//指定要寫入的數據源的id
var hang = table.rows.length;//取數據源行數
var lie = table.rows(0).cells.length;//取數據源列數
// Add table headers going cell by cell.
for (i=0;i<hang;i++){//在Excel中寫行
for (j=0;j<lie;j++){//在Excel中寫列
//定義格式
oSheet.Cells(i+1,j+1).NumberFormatLocal = "@";
//!!!!!!!上面這一句是將單元格的格式定義為文本
oSheet.Cells(i+1,j+1).Font.Bold = true;//加粗
oSheet.Cells(i+1,j+1).Font.Size = 10;//字體大小
oSheet.Cells(i+1,j+1).value = table.rows(i).cells(j).innerText;//向單元格寫入值
}
}
oXL.Visible = true;
oXL.UserControl = true;
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<table border="0" width="300" id="data" bgcolor="black" cellspacing="1">
<tr bgcolor="white">
<td>編號</td>
<td>姓名</td>
<td>年齡</td>
<td>性別</td>
</tr>
<tr bgcolor="white">
<td>0001</td>
<td>張三</td>
<td>22</td>
<td>女</td>
</tr>
<tr bgcolor="white">
<td>0002</td>
<td>李四</td>
<td>23</td>
<td>男</td>
</tr>
</table>
<input type="button" name="out_excel" onclick="AutomateExcel();" value="導出到excel">
</BODY>
</HTML>
以前讀書的時候絕不會想到會用客戶端腳本來實現這些功能,現在卻一開始就要用上了,而且還覺得挺實用的。
參考《Windows腳本技術》,應該會有一點收獲。
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
</head>
<script language="javascript" type="text/javascript">
function importXLS(fileName)
{
objCon = new ActiveXObject("ADODB.Connection");
objCon.Provider = "Microsoft.Jet.OLEDB.4.0";
objCon.ConnectionString = "Data Source=" + fileName + ";Extended Properties=Excel 8.0;";
objCon.CursorLocation = 1;
objCon.Open;
var strQuery;
//Get the SheetName
var strSheetName = "Sheet1$";
var rsTemp = new ActiveXObject("ADODB.Recordset");
rsTemp = objCon.OpenSchema(20);
if(!rsTemp.EOF)
strSheetName = rsTemp.Fields("Table_Name").Value;
rsTemp = null
rsExcel = new ActiveXObject("ADODB.Recordset");
strQuery = "SELECT * FROM [" + strSheetName + "]";
rsExcel.ActiveConnection = objCon;
rsExcel.Open(strQuery);
while(!rsExcel.EOF)
{
for(i = 0;i<rsExcel.Fields.Count;++i)
{
alert(rsExcel.Fields(i).value);
}
rsExcel.MoveNext;
}
// Close the connection and dispose the file
objCon.Close;
objCon =null;
rsExcel = null;
}
</script>
</head>
<body>
<input type="file" id="f" />
<input type="button" id="b" value="import" onclick="if(f.value=='')alert('請選擇xls文件');else importXLS(f.value)" />
</body>
</html>
trackback:http://hi..com/netcorner/blog/item/4c35a818788f670635fa41d3.html
通過Javascript操作Excel
function AutomateExcel()
{
// Start Excel and get Application object.
var oXL = new ActiveXObject("Excel.Application");
oXL.Visible = true;
// Get a new workbook.
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
// Add table headers going cell by cell.
oSheet.Cells(1, 1).Value = "First Name";
oSheet.Cells(1, 2).Value = "Last Name";
oSheet.Cells(1, 3).Value = "Full Name";
oSheet.Cells(1, 4).Value = "Salary";
// Format A1:D1 as bold, vertical alignment = center.
oSheet.Range("A1", "D1").Font.Bold = true;
oSheet.Range("A1", "D1").VerticalAlignment = -4108; //xlVAlignCenter
// Create an array to set multiple values at once.
// Fill A2:B6 with an array of values (from VBScript).
oSheet.Range("A2", "B6").Value = CreateNamesArray();
// Fill C2:C6 with a relative formula (=A2 & " " & B2).
var oRng = oSheet.Range("C2", "C6");
oRng.Formula = "=A2 & " " & B2";
// Fill D2:D6 with a formula(=RAND()*100000) and apply format.
oRng = oSheet.Range("D2", "D6");
oRng.Formula = "=RAND()*100000";
oRng.NumberFormat = "$0.00";
// AutoFit columns A:D.
oRng = oSheet.Range("A1", "D1");
oRng.EntireColumn.AutoFit();
// Manipulate a variable number of columns for Quarterly Sales Data.
DispalyQuarterlySales(oSheet);
// Make sure Excel is visible and give the user control
// of Excel's lifetime.
oXL.Visible = true;
oXL.UserControl = true;
}
<HTML>
<HEAD>
<TITLE>將頁面中指定表格的數據導入到Excel中</TITLE>
<SCRIPT LANGUAGE="javascript">
<!--
function AutomateExcel()
{
var oXL = new ActiveXObject("Excel.Application"); //創建應該對象
var oWB = oXL.Workbooks.Add();//新建一個Excel工作簿
var oSheet = oWB.ActiveSheet;//指定要寫入內容的工作表為活動工作表
var table = document.all.data;//指定要寫入的數據源的id
var hang = table.rows.length;//取數據源行數
var lie = table.rows(0).cells.length;//取數據源列數
// Add table headers going cell by cell.
for (i=0;i<hang;i++){//在Excel中寫行
for (j=0;j<lie;j++){//在Excel中寫列
//定義格式
oSheet.Cells(i+1,j+1).NumberFormatLocal = "@";
//!!!!!!!上面這一句是將單元格的格式定義為文本
oSheet.Cells(i+1,j+1).Font.Bold = true;//加粗
oSheet.Cells(i+1,j+1).Font.Size = 10;//字體大小
oSheet.Cells(i+1,j+1).value = table.rows(i).cells(j).innerText;//向單元格寫入值
}
}
oXL.Visible = true;
oXL.UserControl = true;
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<table border="0" width="300" id="data" bgcolor="black" cellspacing="1">
<tr bgcolor="white">
<td>編號</td>
<td>姓名</td>
<td>年齡</td>
<td>性別</td>
</tr>
<tr bgcolor="white">
<td>0001</td>
<td>張三</td>
<td>22</td>
<td>女</td>
</tr>
<tr bgcolor="white">
<td>0002</td>
<td>李四</td>
<td>23</td>
<td>男</td>
</tr>
</table>
<input type="button" name="out_excel" onclick="AutomateExcel();" value="導出到excel">
</BODY>
</HTML>
㈡ js怎麼獲取 excel的數據
js怎麼獲取 excel的數據
在一個html頁面生成的時候,js就已經把dom樹創建好了
只要這棵樹上有的數據,我們都可以通過js來獲取使用
那麼這個問題就變成js如何去查詢dom樹的節點了
js內置了以下幾個查詢節點的方法
document.getElementById("id") //通過id來獲取一個dom元素
document.getElementsByName("name") //通過name屬性來獲取一個集合
document.getElementsByTagName("tag") //通過標簽來獲取一個集合
如果要查找的元素本身有id,通過第一個方法可以直接獲取,當我們得到了這個dom節點的時候就可以輸出他的一系列數據了
舉個栗子
var dom = document.getElementById("id");
//如果是圖片
alert(dom.src);
//如果該dom有class
alert(dom.className);
//如果該dom有value
alert(dom.value);
//如果該dom有文本
alert(dom.innerHTML);
//輸出dom的style
alert(dom.style.width);
//等等
當dom沒有id的時候,我們就要依靠一些條件去找到我們想要的dom了
//尋找所有class為bold的div
var c = document.getElementsByTagName("div"), len = c.length, r = [];
for(var i=0;i<len;i++){
if(c[i].className == 'bold'){
r.push(c[i]);
}
}
//當得到了這些dom節點後,就可以用上面提到的方法繼續訪問每個dom的數據啦
㈢ JavaScript 原生 提取excel文件 需要學習那些知識或者哪方面的,不要插件。最好有例子。
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. TheSpreadJS 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 Spread.Sheets script and the CSS files that are included in your SpreadJS download:
<!DOCTYPE html> <html> <head>
<title>SpreadJS ExcelIO</title>
<script src="http://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.min.js" type="text/javascript"></script>
<link href="http://cdn.grapecity.com/spreadjs/hosted/css/gc.spread.sheets.excel2013white.10.1.0.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://cdn.grapecity.com/spreadjs/hosted/scripts/gc.spread.sheets.all.10.1.0.min.js"></script>
<script type="text/javascript" src="http://cdn.grapecity.com/spreadjs/hosted/scripts/interop/gc.spread.excelio.10.1.0.min.js"></script> </head> <body>
<div id="ss" style="height:600px ; width :100%; "></div> </body> </html>
Then add a script to the page that initializes the Spread.Sheets 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 GC.Spread.Sheets.Workbook(document.getElementById("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 GC.Spread.Excel.IO();
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="http://www.testwebsite.com/files/TestExcel.xlsx" style="width:300px" />
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 = "./test.xlsx";
var oReq = new XMLHttpRequest();
oReq.open('get', excelUrl, true);
oReq.responseType = 'blob';
oReq.onload = function () {
var blob = oReq.response;
excelIO.open(blob, LoadSpread, function (message) {
console.log(message);
});
};
oReq.send(null);
}
function LoadSpread(json) {
jsonData = json;
workbook.fromJSON(json);
workbook.setActiveSheet("Revenues (Sales)");
}
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).readyfunction:
$(document).ready(function () {
$.support.cors = true;
workbook = new GC.Spread.Sheets.Workbook(document.getElementById("ss"));
//... });
你可以搜尋 黑體字 , 如果需要更詳細的介紹
㈣ node.js解析excel大文件的問題
問題:使用 koa-bodyparser 可以解析post提交的問題,但是發現獲取不到formdata上傳的文件。
後經查資料,改用 koa-body 解決:
constkoaBody = require('koa-body'); app.use(koaBody({ multipart:true, formidable: { maxFileSize:200*1024*1024//設置上傳文件大小最大限制,默認2M} }))
然後就可以在 ctx.request.files 裡面獲取到上傳的 文件信息了。ctx.request.files 為一個對象,裡麵包含一個 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 = xlsx.parse("./doc/hello.xlsx");
返回結果,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文件:
fs.writeFileSync(路徑,Buffer數據)
xlsx.build(xlsxObj) 會將數組轉換為Buffer數據
執行下面代碼就會生成對應的Excel文件
fs.writeFileSync('./doc/hello.xlsx', xlsx.build(xlsxObj),"binary")
㈤ IE、谷歌如何通過js讀取Excel中數據到Web頁面的表格
Excel文件,另存為Html格式,就知道了
Response.ContentType 是告訴瀏覽器,伺服器傳遞過來的內容是什麼格式版
這個值默認是權text/html
這個要在所有的客戶機上設置瀏覽器許可權,比如IE的修改方法:
進入Internet屬性=》安全=》自定義級別,把ActiveX控制項和插件下的所有選項都改成啟用
因為js是客戶端的,所以伺服器不可能控制
我一般是用伺服器生成html格式的Excel,然後設置
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "inline;filename=abc.xls");
這樣來下載excel,而且這樣不用修改客戶端的ie設置
㈥ js解析excel表格
https://www.cnblogs.com/liuxianan/p/js-excel.html
由 SheetJS 出品的js-xlsx是一款非常方便的只需要純JS即可讀取和導出excel的工具庫,功能強大,支持格式眾多,支持xls、xlsx、ods(一種OpenOffice專有表格文件格式)等十幾種格式。本文全部都是以xlsx格式為例。
dist目錄下有很多個JS文件,一般情況下用xlsx.core.min.js就夠了,xlsx.full.min.js則是包含了所有功能模塊。
JavaScript讀取和導出excel示例(基於js-xlsx)
http://demo.haoji.me/2017/02/08-js-xlsx/
㈦ JS讀取excel,獲取其中的單元格佔多少行/列
獲取行/列
var colcount=wb.Worksheets(1).UsedRange.Cells.Rows.Count ;
var colcolumn=wb.Worksheets(1).UsedRange.Columns.Count;
讀取本地Excel文件內容的Javascript代碼:
<script type="text/javascript">
function read_excel(){
var filePath="D:\abcd9.com.xls"; //要讀取的xls
var sheet_id=2; //讀取第2個表
var row_start=3; //從第3行開始讀取
var tempStr='';
try{
var oXL = new ActiveXObject("Excel.application"); //創建Excel.Application對象
}catch(err)
{
alert(err);
}
var oWB = oXL.Workbooks.open(filePath);
oWB.worksheets(sheet_id).select();
var oSheet = oWB.ActiveSheet;
var colcount=oXL.Worksheets(sheet_id).UsedRange.Cells.Rows.Count ;
for(var i=row_start;i<=colcount;i++){
if (typeof(oSheet.Cells(i,8).value)=='date'){ //處理第8列部分單元格內容是日期格式時的讀取問題
d= new Date(oSheet.Cells(i,8).value);
temp_time=d.getFullYear()+"-"+(d.getMonth() + 1)+"-"+d.getDate();
}
else
temp_time=$.trim(oSheet.Cells(i,7).value.toString());
tempStr+=($.trim(oSheet.Cells(i,2).value)+" "+$.trim(oSheet.Cells(i,4).value)+" "+$.trim(oSheet.Cells(i,6).value.toString())+" "+temp_time+"\n");
//讀取第2、4、6、8列內容
}
return tempStr; //返回
oXL.Quit();
CollectGarbage();
}
</script>
㈧ 哥們,你好,我在網上找到一段JS代碼可以讀取excel數據,有些地方不懂問一下您
這位仁兄,我建議你先去w3school去看下基礎,先幫你改了
<script language="javascript" type="text/javascript">
var fileName = "D:\monkeyrunner\debugcase\Address\TestResult.xls";
importXLS(fileName);
function importXLS(fileName)
{
objCon = new ActiveXObject("ADODB.Connection");
objCon.Provider = "Microsoft.Jet.OLEDB.4.0";
objCon.ConnectionString = "Data Source=" + fileName + ";Extended Properties=Excel 8.0;";
objCon.CursorLocation = 1;
objCon.Open;
var strQuery;
var strSheetName = "Sheet1$";
var rsTemp = new ActiveXObject("ADODB.Recordset");
rsTemp = objCon.OpenSchema(20);
㈨ 怎麼在JS讀取excel文件
貌似js沒有這種功能,做為客戶端語言有限制。
㈩ js XLSX讀取 excel 轉換日期格式
前端讀取excel 文件,excel內容展示在頁面上,用戶確認無誤後再上傳至後端,使用的XLSX來解析excel,讀取日期的時候是數字。
excel數據解析出阿里雲JSON數據如下:(tableDataJson)