You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Protractor中用SheetJS+FileSaver导出对象数组到D盘XLSX文件问题

Protractor + SheetJS + FileSaver: Fixing XLSX Export Path Issues & Object Array Conversion

Hey there! Let's break down your problem and fix it step by step.

1. Why Your D:\ Path Isn't Working

First off, the core issue here is browser security restrictions. FileSaver.js relies on the browser's native saveAs API, which does NOT allow specifying absolute local paths (like D:\\output.xlsx). When you pass that path as the filename, the browser will ignore the path part and just use output.xlsx as the filename, saving it to your default downloads folder (or prompting you to choose a location, depending on browser settings).

There's no way to bypass this directly in the browser context—it's a deliberate safety measure to prevent websites from writing arbitrary files to your system.

2. How to Save to a Specific Path (Protractor-Specific Solution)

Since Protractor runs on Node.js (your test script) alongside the browser, you can shift the XLSX generation and file writing to the Node.js layer instead of relying on browser-side FileSaver. This lets you write directly to any local path you have permissions for.

3. Converting Your Object Array to XLSX

SheetJS (xlsx library) has a handy json_to_sheet method that converts your object array directly into a worksheet—no need to manually map data to arrays. You can also control column order if needed.

Full Working Code Examples

Option 1: Browser-Side Export (Default Download Folder)

If you still want to handle export in the browser (but accept the default download path), fix your existing code like this:

// Import SheetJS and FileSaver first (ensure they're loaded in your page)
import XLSX from 'xlsx';
import FileSaver from 'file-saver';

// Your object array
const dataArray = [
  { Col1: 'data1', Col2: 'data2', Col3: 'data3', Col4: 'data4', Col5: 'data5', Col6: 'data6', Col7: 'data7', Col8: '', Col8Datetime: 8, ColId: 'dataid1', EventType: 'Event3' },
  { Col1: 'datarow1', Col2: 'datarow2', Col3: 'datarow3', Col4: 'datarow4', Col5: 'datarow5', Col6: 'datarow6', Col7: 'datarow7', Col8: '', Col8Datetime: 8, ColId: 'dataid2', EventType: 'Event3' }
];

// Create workbook and worksheet
const workbook = XLSX.utils.book_new();
workbook.Props = {
  Title: "SheetJS Tutorial",
  Subject: "Test",
  Author: "Red Stapler",
  CreatedDate: new Date(2017, 11, 19) // Note: Months are 0-indexed in JavaScript!
};
const worksheet = XLSX.utils.json_to_sheet(dataArray);
XLSX.utils.book_append_sheet(workbook, worksheet, "Test Sheet");

// Generate binary output and convert to Blob
const wbout = XLSX.write(workbook, { bookType: 'xlsx', type: 'binary' });
function s2ab(s) {
  const buf = new ArrayBuffer(s.length);
  const view = new Uint8Array(buf);
  for (let i = 0; i !== s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
  return buf;
}
const blob = new Blob([s2ab(wbout)], { type: 'application/octet-stream' });

// Trigger download (browser will save to default folder)
FileSaver.saveAs(blob, "output.xlsx");

Option 2: Node.js-Side Export (Save to D:\ Directly)

In your Protractor test script, use SheetJS directly in Node.js to write the file to your desired path:

// In your Protractor spec file
const XLSX = require('xlsx');
const path = require('path');

// Your object array
const dataArray = [
  { Col1: 'data1', Col2: 'data2', Col3: 'data3', Col4: 'data4', Col5: 'data5', Col6: 'data6', Col7: 'data7', Col8: '', Col8Datetime: 8, ColId: 'dataid1', EventType: 'Event3' },
  { Col1: 'datarow1', Col2: 'datarow2', Col3: 'datarow3', Col4: 'datarow4', Col5: 'datarow5', Col6: 'datarow6', Col7: 'datarow7', Col8: '', Col8Datetime: 8, ColId: 'dataid2', EventType: 'Event3' }
];

// Create workbook and worksheet
const workbook = XLSX.utils.book_new();
workbook.Props = {
  Title: "SheetJS Tutorial",
  Subject: "Test",
  Author: "Red Stapler",
  CreatedDate: new Date(2017, 11, 19)
};
const worksheet = XLSX.utils.json_to_sheet(dataArray);
XLSX.utils.book_append_sheet(workbook, worksheet, "Test Sheet");

// Write directly to D:\output.xlsx
const outputPath = path.resolve('D:\\output.xlsx');
XLSX.writeFile(workbook, outputPath);
console.log(`File saved to: ${outputPath}`);

Key Fixes & Notes

  • Month Indexing: In JavaScript, new Date(2017,12,19) is invalid (months are 0-11). Use new Date(2017,11,19) for December 19, 2017.
  • Blob Content: Your original code used ["Hello, world!"] for the Blob—this is wrong. You need to pass the converted binary output of the workbook (s2ab(wbout)).
  • Node.js vs Browser: For controlled path saving, always use Node.js in Protractor test scripts, as browser APIs can't access local filesystem paths directly.

内容的提问来源于stack exchange,提问作者Arif Rizwan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 07:12:44