Working with Excel Images

9 Apr 20211 minute to read

Adding Images to worksheet

Flutter XlsIO allows to insert images like JPEG and PNG formats into a worksheet.

Refer to the following code snippet to add images to worksheet.

  • dart
  • // Create a new Excel document.
    final Workbook workbook = Workbook();
    
    // Accessing worksheet via index.
    final Worksheet sheet = workbook.worksheets[0];
    
    // Adding an image.
    final List<int> imageBytes = File('image.jpeg').readAsBytesSync();
    sheet.picutes.addStream(1, 1, imageBytes);
    
    // Save and dispose workbook.
    final List<int> bytes = workbook.saveAsStream();
    workbook.dispose();
    
    File('AddImage.xlsx').writeAsBytes(bytes);

    Re-Sizing, Flip and Rotation Images

    Pictures can be re-sized, flip and formatted using various properties of Picture class. Refer to the following code snippet.

  • dart
  • // Create a new Excel document.
    final Workbook workbook = Workbook();
    
    // Accessing worksheet via index.
    final Worksheet sheet = workbook.worksheets[0];
    
    // Add a image.
    final List<int> imageBytes = File('image.jpeg').readAsBytesSync();
    sheet.picutes.addStream(1, 1, imageBytes);
    
    // Re-size an image
    picture.height = 200;
    picture.width = 200;
    
    // rotate an image.
    picture.rotation = 100;
    
    // Flip an image.
    picture.horizontalFlip = true;
    
    // save and dispose workbook
    final List<int> bytes = workbook.saveAsStream();
    workbook.dispose();
    
    File('Image.xlsx').writeAsBytes(bytes);