skip to Main Content

How to add custom fonts to excel generator in flutter. https://pub.dev/packages/excel . We want to create excel file with japanese font "MS Pゴシック" . Is there any chance to add ttf file externally and apply to excel file?

I have added .ttf file in asset folder and applied to the excel file generator . But font did not reflect in the excel file. only inbuilt fonts of that package only applying to excel file, I want to add external fonts "MS Pゴシック" to the file

2

Answers


  1. Chosen as BEST ANSWER

    I found the solution to add custom font in the flutter excel generator by using "syncfusion_flutter_xlsio" sdk.

    I have added the sample code for the reference

    pubspeck.yaml file

     fonts:
    - family: MS Pゴシック
      fonts:
        - asset: assets/font/MSPGothic.ttf
    

    import 'package:syncfusion_flutter_xlsio/xlsio.dart' as xlsio;

    generateXLSX() async {

    final xlsio.Workbook workbook = xlsio.Workbook();
    final xlsio.Worksheet sheet = workbook.worksheets[0];
    sheet.enableSheetCalculations();
    
    // Set the custom font
    final xlsio.Style style = workbook.styles.add('customFontStyle');
    style.fontName = "MS Pゴシック";
    style.fontSize = 12;
    
    // Apply style to a cell
      
      sheet.getRangeByIndex(1, 1).cellStyle = style;
      sheet.getRangeByIndex(1, 1).setText("益田市");
    
    
    // Save the file
    final List<int> bytes = workbook.saveAsStream();
    
    final Uint8List data = Uint8List.fromList(bytes);
    
    var value = await FileSaver.instance.saveFile(
      name: 'fileName.xlsx',
      bytes: data,
    );
    
    workbook.dispose();
    

    }


  2. Currently, the excel package in Flutter does not natively support embedding custom fonts, such as “MS Pゴシック,” into an Excel file. The package only works with the built-in fonts, and it lacks the functionality for specifying external fonts or embedding .ttf files directly in the generated Excel file.

    However, you can consider a few alternative approaches to achieve this, like

    • Use Post-Processing in Excel
    • Use an External Library that Supports Custom Fonts
    • Apply Font Styles with Excel Macros
    • HTML to Excel Conversion
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search