flutter 如何自动设置Excel表格的栏宽?

rjee0c15  于 2023-05-18  发布在  Flutter
关注(0)|答案(1)|浏览(161)

我正在使用excel.dart包来保存excel中的数据。列的自动宽度设置有问题

qc6wkl3g

qc6wkl3g1#

有同样的问题,然后通过询问Chatgpt找到了解决方案。

Future<void> setColumnAutoFit() async {
  var excel = Excel.createExcel();
  var sheet = excel[sheetName];

  // Your logic to populate the Excel sheet goes here

  // Set auto-fit for columns
  for (var column in sheet.columns) {
    var maxLength = 0;

    for (var cell in column) {
      if (cell.value.toString().length > maxLength) {
        maxLength = cell.value.toString().length;
      }
    }

    // Set column width based on the maximum cell value length
    sheet.setColumnWidth(column.columnIndex, maxLength);
  }

  // Save the Excel file
  await excel.save(fileName);
}

相关问题