js实现操作excel类:exceljs

f663x 10年前

exceljs实现java风格用javascript读写操作excel文件,实现了读取sheet工作表,创建列、插入行数据、行格式化输出、行验证表达、设置字体样式、设置单元格边框大小。

Interface

var Excel = require("exceljs");

Create a Workbook

var workbook = new Excel.Workbook();

Set Workbook Properties
workbook.creator = "Me";  workbook.lastModifiedBy = "Her";  workbook.created = new Date(1985, 8, 30);  workbook.modified = new Date();

Add a Worksheet

var sheet = workbook.addWorksheet("My Sheet");

Access Worksheets
// Iterate over all sheets  // Note: workbook.worksheets.forEach will still work but this is better  workbook.eachSheet(function(worksheet, sheetId) {      // ...  });    // fetch sheet by name  var worksheet = workbook.getWorksheet("My Sheet");    // fetch sheet by id  var worksheet = workbook.getWorksheet(1);

Columns
// Add column headers and define column keys and widths  // Note: these column structures are a workbook-building convenience only,  // apart from the column width, they will not be fully persisted.  worksheet.columns = [      { header: "Id", key: "id", width: 10 },      { header: "Name", key: "name", width: 32 },      { header: "D.O.B.", key: "DOB", width: 10 }  ];    // Access an individual columns by key, letter and 1-based column number  var idCol = worksheet.getColumn("id");  var nameCol = worksheet.getColumn("B");  var dobCol = worksheet.getColumn(3);    // set column properties    // Note: will overwrite cell value C1  dobCol.header = "Date of Birth";    // Note: this will overwrite cell values C1:C2  dobCol.header = ["Date of Birth", "A.K.A. D.O.B."];    // from this point on, this column will be indexed by "dob" and not "DOB"  dobCol.key = "dob";    dobCol.width = 15;

项目主页:http://www.open-open.com/lib/view/home/1426304997326