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

如何使用SuiteScript 2.0及HTML Suitelet创建Excel与Word文件?

使用SuiteScript 2.0创建Excel和Word文件(含HTML Suitelet场景修复)

Hey there! Let's break down how to tackle both your questions—first the basics of generating Excel/Word files with SuiteScript 2.0, then fixing the specific issue you're hitting with your HTML Suitelet.


问题1:基础方法:创建Excel/Word文件

The core tool here is NetSuite's N/file module. The key is using the correct file type enums and matching content formats that NetSuite can parse into valid Office files.

生成Excel(XLSX)文件

The easiest way for most use cases is to use an HTML table as your content—NetSuite will automatically convert this into a valid XLSX file. For complex spreadsheets, you could work with raw Office Open XML, but that's overkill for most scenarios.

define(['N/file'], function(file) {
  function createSimpleExcel() {
    // Build your content as an HTML table
    const excelHtml = `
      <table>
        <tr><th>Order #</th><th>Customer</th><th>Amount</th></tr>
        <tr><td>SO12345</td><td>ABC Corp</td><td>$1,200.00</td></tr>
        <tr><td>SO12346</td><td>XYZ Inc</td><td>$850.00</td></tr>
      </table>
    `;

    // Create and save the XLSX file
    const excelFile = file.create({
      name: 'Customer_Orders.xlsx',
      fileType: file.Type.XLSX,
      contents: excelHtml,
      folder: 123 // Replace with your target folder ID (optional)
    });

    const savedFileId = excelFile.save();
    return savedFileId;
  }

  return { createSimpleExcel: createSimpleExcel };
});

生成Word文件

NetSuite's file.Type.WORD supports HTML content, so you can build styled documents with headings, tables, and formatting just like you would for a web page.

define(['N/file'], function(file) {
  function createSimpleWordDoc() {
    // Build styled HTML content for Word
    const wordHtml = `
      <h1>Customer Order Summary</h1>
      <p>Generated on: ${new Date().toLocaleDateString()}</p>
      <table border="1" cellpadding="5">
        <tr><th>Order #</th><th>Customer</th><th>Amount</th></tr>
        <tr><td>SO12345</td><td>ABC Corp</td><td>$1,200.00</td></tr>
        <tr><td>SO12346</td><td>XYZ Inc</td><td>$850.00</td></tr>
      </table>
      <p><em>Note: This document is auto-generated. Do not edit manually.</em></p>
    `;

    // Create and save the Word file
    const wordFile = file.create({
      name: 'Order_Summary.docx',
      fileType: file.Type.WORD,
      contents: wordHtml,
      folder: 123 // Replace with your target folder ID (optional)
    });

    const savedFileId = wordFile.save();
    return savedFileId;
  }

  return { createSimpleWordDoc: createSimpleWordDoc };
});

问题2:HTML Suitelet生成Word/Excel的修复方案

You mentioned you can generate PDFs and CSVs but not Word/Excel—this almost always boils down to incorrect file type enums or mismatched content formats. Here's a complete, fixed Suitelet example, plus key pitfalls to avoid:

Core Fixes to Remember

  • Don't reuse PDF content logic (like pdffile.getContents())—Word/Excel need HTML content, not PDF data
  • Use the correct file.Type enums:
    • Excel: file.Type.XLSX (not CSV—CSV is a separate file type)
    • Word: file.Type.WORD (not RTF or other legacy types)
  • Use response.writeFile() to send the file to the user, with isInline: false to trigger a download

Complete Working Suitelet Example

define(['N/file', 'N/ui/serverWidget', 'N/response'], function(file, serverWidget, response) {
  function onRequest(context) {
    // Show a form to select file type on GET request
    if (context.request.method === 'GET') {
      const form = serverWidget.createForm({ title: 'Generate Office Files' });
      const fileTypeSelect = form.addField({
        id: 'custpage_file_type',
        type: serverWidget.FieldType.SELECT,
        label: 'Select File Type'
      });
      fileTypeSelect.addSelectOption({ value: 'EXCEL', text: 'Excel (.xlsx)' });
      fileTypeSelect.addSelectOption({ value: 'WORD', text: 'Word (.docx)' });
      form.addSubmitButton({ label: 'Generate File' });
      context.response.writePage(form);
    } 
    // Generate and send the file on POST request
    else {
      const selectedType = context.request.parameters.custpage_file_type;
      let outputFile;

      // Reusable table content (replace with real NetSuite data queries)
      const baseTableHtml = `
        <table border="1" cellpadding="5">
          <tr><th>Order #</th><th>Customer</th><th>Amount</th></tr>
          <tr><td>SO12345</td><td>ABC Corp</td><td>$1,200.00</td></tr>
          <tr><td>SO12346</td><td>XYZ Inc</td><td>$850.00</td></tr>
        </table>
      `;

      if (selectedType === 'EXCEL') {
        outputFile = file.create({
          name: 'Order_Report.xlsx',
          fileType: file.Type.XLSX,
          contents: baseTableHtml
        });
      } else if (selectedType === 'WORD') {
        // Add extra styling for Word documents
        const fullWordHtml = `<h1>Order Summary Report</h1><p>Generated: ${new Date().toLocaleString()}</p>${baseTableHtml}`;
        outputFile = file.create({
          name: 'Order_Report.docx',
          fileType: file.Type.WORD,
          contents: fullWordHtml
        });
      }

      // Send the file to the user for download
      response.writeFile({
        file: outputFile,
        isInline: false
      });
    }
  }

  return { onRequest: onRequest };
});

Common Pitfalls to Troubleshoot

  1. Permission Issues: Ensure your script has access to the N/file module and write permissions for any folders you're saving files to
  2. Special Characters: If your content has accents or special symbols, make sure your HTML uses UTF-8 encoding (SuiteScript handles this by default for string content)
  3. Invalid Content: Avoid broken HTML tags—NetSuite will fail to parse invalid HTML into Office files

内容的提问来源于stack exchange,提问作者Mr. Kamran

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 19:57:36