最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ExcelJS freeze rows - Stack Overflow

programmeradmin1浏览0评论

I'm trying to freeze rows in ExcelJS but cannot get it to work.

I'm trying both

worksheet.views = [
{state: 'frozen', ySplit: 1}
];

and

workbook.addWorksheet(sheetName, {
views: [
{state: 'frozen', ySplit: 1}
]
});

but it doesn't work.

I also get the "We found a problem with...Do you want to recover as much.." warning when opening up the spreadsheet.

Anyone getting this to work? All I want to do is freeze the 7th row. I can do this in Excel itself

There's an Issue reported here that seems to say it's just not a feature in Excel itself. But I'm wondering why it corrupts the file too.

I'm trying to freeze rows in ExcelJS but cannot get it to work.

I'm trying both

worksheet.views = [
{state: 'frozen', ySplit: 1}
];

and

workbook.addWorksheet(sheetName, {
views: [
{state: 'frozen', ySplit: 1}
]
});

but it doesn't work.

I also get the "We found a problem with...Do you want to recover as much.." warning when opening up the spreadsheet.

Anyone getting this to work? All I want to do is freeze the 7th row. I can do this in Excel itself

There's an Issue reported here that seems to say it's just not a feature in Excel itself. But I'm wondering why it corrupts the file too.

Share Improve this question asked Apr 6, 2020 at 17:41 user2402616user2402616 1,5734 gold badges23 silver badges55 bronze badges 2
  • 1 Just double-checking that you're using Node.js and the exceljs package...? I mistakenly read the question at first as being about Excel's own JavaScript API, but you used the excejs tag and I see that views is something that the package talks about, so... – T.J. Crowder Commented Apr 6, 2020 at 17:48
  • 1 Correct, I'm using excelJS package and not Excel's JS API (didn't even know there was an official one..?) – user2402616 Commented Apr 6, 2020 at 18:43
Add a ment  | 

1 Answer 1

Reset to default 4

Try this:

workbook.addWorksheet(sheetName, {
    views: [{ state: "frozen", ySplit: 1 }],
});

It's working for me.

More details:

const Excel = require("exceljs");

const writeFile = async (sheetName, columns, list, file) => {
  let workbook = new Excel.Workbook();
  let sheet = workbook.addWorksheet(sheetName, {
    views: [{ state: "frozen", ySplit: 1 }],
  });
  sheet.columns = columns;

  for (const record of list) {
    sheet.addRow(record);
  }
  workbook.xlsx.writeFile(file);
};


const test = async () => {
  const columns = [
    { header: "Column1", key: "column1" },
    { header: "Column2", key: "column2" },
    { header: "Column3", key: "column3" },
  ];
  let list = [];
  for (let i = 1; i < 100; i++) {
    let record = {};
    record.column1 = `line${i}_column1`;
    record.column2 = `line${i}_column2`;
    record.column3 = `line${i}_column3`;
    list.push(record);
  }
  const file = "c:/temp/test.xlsx";
  await writeFile("sheet1", columns, list, file);
};
test();
发布评论

评论列表(0)

  1. 暂无评论