Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Create Excel Doc with OpenXML works on Windows, but on Android the file is empty

Discussion in 'Scripting' started by Topunint, Feb 21, 2019.

  1. Topunint

    Topunint

    Joined:
    Oct 25, 2018
    Posts:
    3
    Hello,

    I'm trying to create Excel document on the app folder, i have found a good tutorial and i'm using this code for test :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using DocumentFormat.OpenXml;
    5. using DocumentFormat.OpenXml.Packaging;
    6. using DocumentFormat.OpenXml.Spreadsheet;
    7.  
    8. public class Simulexcel : MonoBehaviour
    9. {
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         Report report = new Report();
    14.         report.CreateExcelDoc(Application.persistentDataPath+"/"+Team.TeamName+"Report.xls");
    15.     }
    16.     public class Employee
    17.     {
    18.         public int Id { get; set; }
    19.         public string Name { get; set; }
    20.         public System.DateTime DOB { get; set; }
    21.         public decimal Salary { get; set; }
    22.     }
    23.  
    24.     public sealed class Employees
    25.     {
    26.         static List<Employee> _employees;
    27.         const int COUNT = 15;
    28.  
    29.         public static List<Employee> EmployeesList
    30.         {
    31.             private set { }
    32.             get
    33.             {
    34.                 return _employees;
    35.             }
    36.         }
    37.  
    38.         static Employees()
    39.         {
    40.             Initialize();
    41.         }
    42.  
    43.         private static void Initialize()
    44.         {
    45.             _employees = new List<Employee>();
    46.  
    47.             Random random = new Random();
    48.  
    49.             for (int i = 0; i < COUNT; i++)
    50.             {
    51.                 _employees.Add(new Employee()
    52.                 {
    53.                     Id = i,
    54.                     Name = "Employee " + i,
    55.                     DOB = new System.DateTime(1999, 1, 1).AddMonths(i),
    56.                     Salary = 100
    57.                 });
    58.             }
    59.         }
    60.     }
    61.     public class Report
    62.     {
    63.         public void CreateExcelDoc(string fileName)
    64.         {
    65.             using (SpreadsheetDocument document = SpreadsheetDocument.Create(fileName, SpreadsheetDocumentType.Workbook))
    66.             {
    67.                 WorkbookPart workbookPart = document.AddWorkbookPart();
    68.                 workbookPart.Workbook = new Workbook();
    69.  
    70.                 WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
    71.                 worksheetPart.Worksheet = new Worksheet();
    72.  
    73.                 Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());
    74.  
    75.                 Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Employees" };
    76.  
    77.                 sheets.Append(sheet);
    78.  
    79.                 workbookPart.Workbook.Save();
    80.  
    81.                 List<Employee> employees = Employees.EmployeesList;
    82.  
    83.                 SheetData sheetData = worksheetPart.Worksheet.AppendChild(new SheetData());
    84.  
    85.                 // Constructing header
    86.                 Row row = new Row();
    87.  
    88.                 row.Append(
    89.                     ConstructCell("Id", CellValues.String),
    90.                     ConstructCell("Name", CellValues.String),
    91.                     ConstructCell("Birth Date", CellValues.String),
    92.                     ConstructCell("Salary", CellValues.String));
    93.  
    94.                 // Insert the header row to the Sheet Data
    95.                 sheetData.AppendChild(row);
    96.  
    97.                 // Inserting each employee
    98.                 foreach (var employee in employees)
    99.                 {
    100.                     row = new Row();
    101.  
    102.                     row.Append(
    103.                         ConstructCell(employee.Id.ToString(), CellValues.Number),
    104.                         ConstructCell(employee.Name, CellValues.String),
    105.                         ConstructCell(employee.DOB.ToString("yyyy/MM/dd"), CellValues.String),
    106.                         ConstructCell(employee.Salary.ToString(), CellValues.Number));
    107.  
    108.                     sheetData.AppendChild(row);
    109.                 }
    110.  
    111.                 worksheetPart.Worksheet.Save();
    112.             }
    113.         }
    114.  
    115.         private Cell ConstructCell(string value, CellValues dataType)
    116.         {
    117.             return new Cell()
    118.             {
    119.                 CellValue = new CellValue(value),
    120.                 DataType = new EnumValue<CellValues>(dataType)
    121.             };
    122.         }
    123.     }
    124.     // Update is called once per frame
    125.     void Update()
    126.     {
    127.      
    128.     }
    129. }
    130.  
    131.  
    Well, the job is done when i try with Unity on my computer.
    But when i export the application on my android tablet, the Excel file was created but "problem with some content".

    I can't understand at this time the problem, maybe the creation method ? Incompatibility with Android ?

    Any idea ?

    Thanks you a lot.
     
    Last edited: Feb 21, 2019
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,640
    Have checked logcat for any errors?
     
  3. Topunint

    Topunint

    Joined:
    Oct 25, 2018
    Posts:
    3
    When the application do the creation :

    Code (CSharp):
    1. 2019-02-21 16:17:29.742 3237-3266/com.Horizon.Trivial I/Unity: soleil
    2.     (Filename: ./Runtime/Export/Debug.bindings.h Line: 45)
    3. 2019-02-21 16:17:30.624 3237-3266/com.Horizon.Trivial E/Unity: Unable to find MonoPosixHelper
    4. 2019-02-21 16:17:30.959 3237-3266/com.Horizon.Trivial E/Unity: DllNotFoundException: MonoPosixHelper
    5.         at (wrapper managed-to-native) zipsharp.NativeZip.zipOpen2_32(string,int,intptr,zipsharp.ZlibFileFuncDef32&)
    6.       at zipsharp.NativeZip.OpenArchive32 (zipsharp.ZlibFileFuncDef32 funcDef, zipsharp.Append append) [0x00000] in <d4f23ef8dcd14ba795947d3d0e6041f3>:0
    7.       at zipsharp.ZipArchive..ctor (System.IO.Stream stream, zipsharp.Append append, System.Boolean ownsStream) [0x00039] in <d4f23ef8dcd14ba795947d3d0e6041f3>:0
    8.       at System.IO.Packaging.ZipPackage.FlushCore () [0x00067] in <d4f23ef8dcd14ba795947d3d0e6041f3>:0
    9.       at System.IO.Packaging.Package.Flush () [0x00038] in <d4f23ef8dcd14ba795947d3d0e6041f3>:0
    10.       at System.IO.Packaging.ZipPartStream.Flush () [0x00006] in <d4f23ef8dcd14ba795947d3d0e6041f3>:0
    11.       at System.IO.StreamWriter.Flush (System.Boolean flushStream, System.Boolean flushEncoder) [0x00094] in <0000d56de0ae43ca875d7babfd990580>:0
    12.       at System.IO.StreamWriter.Dispose (System.Boolean disposing) [0x00022] in <0000d56de0ae43ca875d7babfd990580>:0
    13.       at System.IO.S
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,640
    It looks you need to include MonoPosixHelper.dll as plugin.
     
  5. Topunint

    Topunint

    Joined:
    Oct 25, 2018
    Posts:
    3
    Hello,
    Thanks for your answer.
    I did the logcat on Android Studio.
    I try to include MonoPosixHelper.dll as plugin (Source : Unity\Editor\Data\PlaybackEngines\windowsstandalonesupport\Variations\win64_development_mono\MonoBleedingEdge\EmbedRuntime
    But again this error.

    I place him on the same Plugins Folder than WindowsBase.dll and DocumentFormat.OpenXml.dll.

    The XLSX/XLS file is still created but 0 octet and same problem with content. (Android)
     
  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,640
    Check for error again. It's possible that those dlls are not compatible with Android.
     
  7. nakkalokesh99

    nakkalokesh99

    Joined:
    Jul 19, 2017
    Posts:
    7
    DocumentFormat.OpenXml.dll works perfectly with android when using mono.
    but there is an error when using il2cpp

    tested in android device error
    DllNotFoundException: Unable to load DLL 'MonoPosixHelper': The specified module could not be found.

    zipsharp.NativeZip.zipOpen2_32 (System.String pathname, System.Int32 append, System.IntPtr globalcomment, zipsharp.ZlibFileFuncDef32& pzlib_filefunc_def) (at <00000000000000000000000000000000>:0)
    zipsharp.NativeZip.OpenArchive32 (zipsharp.ZlibFileFuncDef32 funcDef, zipsharp.Append append) (at <00000000000000000000000000000000>:0)
    zipsharp.ZipArchive..ctor (System.IO.Stream stream, zipsharp.Append append, System.Boolean ownsStream) (at <00000000000000000000000000000000>:0)
    System.IO.Packaging.ZipPackage.FlushCore () (at <00000000000000000000000000000000>:0)
    System.IO.Packaging.Package.Flush () (at <00000000000000000000000000000000>:0)
    System.IO.StreamWriter.Dispose (System.Boolean disposing) (at <00000000000000000000000000000000>:0)
    System.IO.StreamWriter.Close () (at <00000000000000000000000000000000>:0)
    System.Xml.XmlTextWriter.Close () (at <00000000000000000000000000000000>:0)
    System.IO.Packaging.Package.WriteRelationships (System.Collections.Generic.Dictionary`2[TKey,TValue] relationships, System.IO.Stream stream) (at <00000000000000000000000000000000>:0)
    System.IO.Packaging.Package.CreateRelationship (System.Uri targetUri, System.IO.Packaging.TargetMode targetMode, System.String relationshipType, System.String id, System.Boolean loading) (at <00000000000000000000000000000000>:0)
    System.IO.Packaging.Package.CreateRelationship (System.Uri targetUri, System.IO.Packaging.TargetMode targetMode, System.String relationshipType) (at <00000000000000000000000000000000>:0)
    DocumentFormat.OpenXml.Packaging.OpenXmlPackage.CreateRelationship (System.Uri targetUri, System.IO.Packaging.TargetMode targetMode, System.String relationshipType) (at <00000000000000000000000000000000>:0)
    DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer.AttachChild (DocumentFormat.OpenXml.Packaging.OpenXmlPart part, System.String rId) (at <00000000000000000000000000000000>:0)
    DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer.InitPart[T] (T newPart, System.String contentType, System.String id) (at <00000000000000000000000000000000>:0)
    DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer.InitPart[T] (T newPart, System.String contentType) (at <00000000000000000000000000000000>:0)
    DocumentFormat.OpenXml.Packaging.WordprocessingDocument.AddMainDocumentPart () (at <00000000000000000000000000000000>:0)
    DefaultNamespace.ResumeBuilder.Generate () (at <00000000000000000000000000000000>:0)
    UnityEngine.Events.UnityEvent.Invoke () (at <00000000000000000000000000000000>:0)
    UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at <00000000000000000000000000000000>:0)
    UnityEngine.EventSystems.StandaloneInputModule.ProcessTouchPress (UnityEngine.EventSystems.PointerEventData pointerEvent, System.Boolean pressed, System.Boolean released) (at <00000000000000000000000000000000>:0)
    UnityEngine.EventSystems.StandaloneInputModule.ProcessTouchEvents () (at <00000000000000000000000000000000>:0)
    UnityEngine.EventSystems.StandaloneInputModule.Process () (at <00000000000000000000000000000000>:0)
     
  8. m-y

    m-y

    Joined:
    Sep 22, 2013
    Posts:
    470
    Hello , what is the news about this problem ?
    ii would like to know if open XML SDK will help me if i want to Edit already exist excel file ?
    if i just want to edit a specific cell inside the Excel file ?
     
  9. patSilva

    patSilva

    Joined:
    Mar 25, 2019
    Posts:
    27
    Where did you find the OpenXML SDK for unity because the repository I found came with loads of errors
     
  10. a-bottosso

    a-bottosso

    Joined:
    Nov 13, 2015
    Posts:
    22
    Hello everyone,

    Facing the same issue.
    I was able to build after including MonoPosixHelper.dll, WindowsBase.dll and DocumentFormat.OpenXml.dll in the "Plugins" folder, but the output on Android is an empty file (it works perfectly on Windows).

    Debugging the app with logcat, an exception is thrown:
    ArgumentException: Unable to find font Calibri or fallback font Microsoft Sans Serif. Install missing fonts or specify a different fallback font through LoadOptions.DefaultGraphicEngine = new DefaultGraphicEngine(Fallback font name).

    Obviously, Android is missing the Calibri font.
    By setting an android system font, like:
    LoadOptions.DefaultGraphicEngine = new DefaultGraphicEngine("Roboto")

    the exception is pretty the same:
    ArgumentException: Unable to find font Calibri or fallback font Roboto. Install missing fonts or specify a different fallback font through LoadOptions.DefaultGraphicEngine = new DefaultGraphicEngine(Fallback font name).

    Has anyone faced the same issue?

    Thanks in advance,
    Alessio