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

基于SheetJS在Electron中访问Excel单元格的技术求助

Hey there! Let's figure out how to access those Excel cells from your test variable—this is a common gotcha when working with SheetJS, so let's break it down step by step.

First, let's clarify what your test variable actually is. When you use XLSX.read() (which is what the Electron demo uses under the hood), you get a Workbook object—not raw cell data directly. This object contains metadata about the file plus all the worksheets inside it.

Step 1: Inspect the Workbook structure

Start by logging test to your Electron dev tools console to see its shape:

console.log(test);

You'll notice two key properties:

  • SheetNames: An array of strings, each being the name of a worksheet in the Excel file (e.g., ["Sheet1", "Sheet2"]).
  • Sheets: An object where each key is a worksheet name, and the value is the worksheet object containing the cell data.

Step 2: Access a specific worksheet

Let's grab the first worksheet (most files have at least one):

// Get the name of the first sheet
const firstSheetName = test.SheetNames[0];
// Get the actual worksheet data
const worksheet = test.Sheets[firstSheetName];

Step 3: Access cells (two easy ways)

Now you have the worksheet—here are the two most common ways to get cell content:

Option 1: Direct cell address access

SheetJS uses Excel-style cell references (like A1, B5) as keys in the worksheet object. Each cell has properties like:

  • v: The raw value (e.g., 123 or "Hello")
  • t: The type (s for string, n for number, b for boolean, etc.)

Example:

// Access cell A1
const cellA1 = worksheet['A1'];
if (cellA1) {
  console.log('Value of A1:', cellA1.v);
  console.log('Type of A1:', cellA1.t);
}

Note: Empty cells won't exist in the worksheet object, so always check if the cell exists before accessing it.

Option 2: Convert to JSON (way more user-friendly)

If you want to work with rows/columns as structured data instead of raw cell addresses, use XLSX.utils.sheet_to_json() to convert the worksheet into an array of objects:

const sheetData = XLSX.utils.sheet_to_json(worksheet);
console.log('Sheet data as JSON:', sheetData);

This will give you something like:

[
  { "Name": "Alice", "Age": 30 },
  { "Name": "Bob", "Age": 25 }
]

You can then access data like any other JavaScript array/object:

// Get the first row's Name value
console.log('First person:', sheetData[0].Name);

Example full code snippet (for your Electron drag-and-drop)

Here's how this fits into your existing drag-and-drop logic:

document.addEventListener('drop', (e) => {
  e.preventDefault();
  const file = e.dataTransfer.files[0];
  
  const reader = new FileReader();
  reader.onload = (event) => {
    const data = new Uint8Array(event.target.result);
    const test = XLSX.read(data, { type: 'array' });
    
    // Get the first worksheet
    const sheetName = test.SheetNames[0];
    const worksheet = test.Sheets[sheetName];
    
    // Option 1: Direct cell access
    const cellB2 = worksheet['B2'];
    if (cellB2) {
      console.log('B2 value:', cellB2.v);
    }
    
    // Option 2: JSON conversion
    const jsonData = XLSX.utils.sheet_to_json(worksheet);
    if (jsonData.length > 0) {
      console.log('All rows:', jsonData);
      console.log('Second row Age:', jsonData[1].Age);
    }
  };
  
  reader.readAsArrayBuffer(file);
});

Common pitfalls to check

  • Make sure you're accessing the correct worksheet name (use test.SheetNames to list all available sheets).
  • Empty cells won't show up in the worksheet object—so if you try to access a blank cell, it will be undefined.
  • If your Excel file has merged cells, SheetJS stores the value only in the top-left cell of the merged range.

内容的提问来源于stack exchange,提问作者Piter Parker

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:23:56