sheetjs_sheetjs/packages/dta/README.md

1.7 KiB

DTA Data File Codec

Codec for reading Stata .DTA files and generating CSF workbook objects compatible with the SheetJS library constellation.

DTA datasets can support millions of observations and over 32767 variables. The codec will truncate data to 1048576 observations and 16384 variables.

https://docs.sheetjs.com/docs/constellation/dta includes a live demo.

Installation

Using NodeJS package manager:

npm install --save https://cdn.sheetjs.com/dta-0.0.1/dta-0.0.1.tgz

The standalone script is also hosted on the SheetJS CDN:

<script src="https://cdn.sheetjs.com/dta-0.0.1/package/dist/dta.min.js"></script>

Usage

The parse method accepts a Uint8Array representing the file data. It returns a "Common Spreadsheet Format" workbook object.

The set_utils method accepts a utils object from SheetJS CE or a SheetJS Pro build. parse will use methods from the utils object.

NodeJS

const XLSX = require("xlsx"), DTA = require("dta");
DTA.set_utils(XLSX.utils);

const wb = DTA.parse(fs.readFileSync("auto.dta"));

Browser

dist/dta.min.js is a standalone build designed to be added with <script>.

<script lang="javascript" src="https://cdn.sheetjs.com/xlsx-latest/package/dist/xlsx.full.min.js"></script>
<script src="dist/dta.min.js"></script>
<div id="out"></div>
<script>
DTA.set_utils(XLSX.utils);
(async() => {
  /* fetch file */
  const data = await (await fetch("test.dta")).arrayBuffer();
  /* parse */
  const wb = DTA.parse(new Uint8Array(data));
  /* wb is a SheetJS workbook object */
  const html = XLSX.utils.sheet_to_html(wb.Sheets[wb.SheetNames[0]]);
  out.innerHTML = html;
})();
</script>