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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Problem writing to file xls from application

Discussion in 'Scripting' started by nikitabelykov, Feb 17, 2023.

  1. nikitabelykov

    nikitabelykov

    Joined:
    Jan 24, 2023
    Posts:
    6
    I don't understand what the problem is. After compilation, the function (editing data in a file) does not work. In the Editor, everything works perfectly and does not give any errors or warnings. Please, help.
    Function:

    Code (CSharp):
    1.  public void UpdateExcel2(string[] Value)
    2.     {
    3.         fs2 = new FileStream (filePath2,FileMode.OpenOrCreate,FileAccess.ReadWrite);
    4.         wk2 = new HSSFWorkbook(fs2);
    5.         sheet2 = wk2.GetSheetAt(0);
    6.         for (int j = 0; j <= sheet2.LastRowNum; j++)
    7.         {
    8.             row2 = sheet2.GetRow(j);
    9.             if (row2 != null)
    10.             {
    11.                 for (int k = 1; k < row2.LastCellNum; k++)
    12.                 {
    13.                     cell2 = row2.CreateCell(1);
    14.                     cell2.SetCellValue(Value[j]);
    15.                 }
    16.             }
    17.         }
    18.         fs2.Close();
    19.         fs2 = File.OpenWrite(filePath2);
    20.         wk2.Write(fs2);
    21.         fs2.Close();
    22.         fs2.Dispose();
    23.     }
    24.  
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,021
  3. nikitabelykov

    nikitabelykov

    Joined:
    Jan 24, 2023
    Posts:
    6
    1. Player.log Info:
    UnloadTime: 0.756100 ms
    NotSupportedException: Encoding 1251 data could not be found. Make sure you have correct international codeset assembly installed and enabled.
    at System.Text.Encoding.GetEncoding (System.Int32 codepage) [0x0022d] in <75633565436c42f0a6426b33f0132ade>:0
    at NPOI.HPSF.CodePageString.GetJavaValue (System.Int32 codepage) [0x00016] in <e8217499ea4b4337aa3682d4fadff7a1>:0
    at NPOI.HPSF.VariantSupport.Read (System.Byte[] src, System.Int32 offset, System.Int32 length, System.Int64 type, System.Int32 codepage) [0x000f0] in <e8217499ea4b4337aa3682d4fadff7a1>:0
    at NPOI.HPSF.Property..ctor (System.Int64 id, System.Byte[] src, System.Int64 offset, System.Int32 Length, System.Int32 codepage) [0x00037] in <e8217499ea4b4337aa3682d4fadff7a1>:0
    at NPOI.HPSF.Section..ctor (System.Byte[] src, System.Int32 offset) [0x001c0] in <e8217499ea4b4337aa3682d4fadff7a1>:0
    at NPOI.HPSF.PropertySet.init (System.Byte[] src, System.Int32 offset, System.Int32 Length) [0x00081] in <e8217499ea4b4337aa3682d4fadff7a1>:0
    at NPOI.HPSF.PropertySet..ctor (System.IO.Stream stream) [0x0002d] in <e8217499ea4b4337aa3682d4fadff7a1>:0
    at NPOI.HPSF.PropertySetFactory.Create (System.IO.Stream stream) [0x00000] in <e8217499ea4b4337aa3682d4fadff7a1>:0
    at NPOI.POIDocument.GetPropertySet (System.String setName) [0x0002e] in <e8217499ea4b4337aa3682d4fadff7a1>:0
    at NPOI.POIDocument.ReadProperties () [0x00000] in <e8217499ea4b4337aa3682d4fadff7a1>:0
    at NPOI.POIDocument.get_DocumentSummaryInformation () [0x00008] in <e8217499ea4b4337aa3682d4fadff7a1>:0
    at NPOI.HSSF.UserModel.HSSFWorkbook.Write (System.IO.Stream stream) [0x0000d] in <e8217499ea4b4337aa3682d4fadff7a1>:0
    at LoadTable.UpdateExcel2 (System.String[] Value) [0x000b8] in <f3958a6531b644cd848afb4710b3d20a>:0
    at Control.UploadValues () [0x0009a] in <f3958a6531b644cd848afb4710b3d20a>:0
    at Control.ChooseRadioMod1 () [0x00575] in <f3958a6531b644cd848afb4710b3d20a>:0
    at Control.Start () [0x000fc] in <f3958a6531b644cd848afb4710b3d20a>:0
    Setting up 6 worker threads for Enlighten.
    Memory Statistics:
    [ALLOC_TEMP_TLS] TLS Allocator

    2. Excel fiile using only this app.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    Looks like something related to codepages and encoding.

    You may wish to simply write to a .CSV file and do the import into an Excel spreadsheet yourself. Using Excel file formats outside of Excel is always a ceaseless endless hassle, between encoding silliness like the above to operating system read/write/ access and permissions, etc.

    In contrast, a CSV file is simply a text file. The format is so simple you can write it by hand, no need to use a library or anything.
     
  5. nikitabelykov

    nikitabelykov

    Joined:
    Jan 24, 2023
    Posts:
    6
    Of course, it is easiest to use even a textual table simulation, but alas. This is the order, since the rest of the programs will communicate with this application using Excel. Therefore, not finding a solution to this problem, I turned to the forums.
     
  6. nikitabelykov

    nikitabelykov

    Joined:
    Jan 24, 2023
    Posts:
    6
    Guys, any other ideas?
     
  7. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,021
    which excel writer you are using? maybe check or report to their github issues also (if they have).

    does it work if you write simple strings? what kind of data you are current writing?
     
  8. nikitabelykov

    nikitabelykov

    Joined:
    Jan 24, 2023
    Posts:
    6
    Code (CSharp):
    1. using NPOI.HSSF.UserModel;
    2. using NPOI.SS.UserModel;
    3. using System.Collections.Generic;
    4. using System.IO;
    5. using UnityEngine;
    6.  
    7.  
    8.     /// <summary>
    9.     /// read form
    10.     /// </summary>
    11. public class LoadTable : MonoBehaviour
    12. {
    13.  
    14.     string filePath;
    15.     string filePath2;
    16.     //HSSFWorkbook wk = new HSSFWorkbook();
    17.     IWorkbook wk = new HSSFWorkbook();
    18.     IWorkbook wk2 = new HSSFWorkbook();
    19.     private FileStream fs;  
    20.     //private HSSFWorkbook wk2;
    21.     private FileStream fs2;
    22.     private ISheet sheet;        
    23.     private IRow row;            
    24.     private ICell cell;          
    25.     private ISheet sheet2;    
    26.     private IRow row2;      
    27.     private ICell cell2;
    28.  
    29.  
    30.     private void Awake()
    31.     {
    32.         filePath = Application.dataPath + "/StreamingAssets/Channel_Table.xls";
    33.         filePath2 = Application.dataPath + "/StreamingAssets/OPU_CONFIG.xls";
    34.         LoadExcel();
    35.     }
    36.  
    37.  
    38.  
    39.     /// <summary>
    40.     /// table creation
    41.     /// </summary>
    42.     public void CreateExcel()
    43.     {
    44.         Debug.Log(1);
    45.         wk = new HSSFWorkbook();
    46.         //wk = new HSSFWorkbook(new FileStream(filePath2, FileMode.OpenOrCreate, FileAccess.ReadWrite));
    47.         sheet = wk.CreateSheet("mySheet");
    48.         //sheet = wk.GetSheetAt(0);
    49.         Debug.Log(2);
    50.  
    51.         for (int i = 0; i <= 20; i++)
    52.         {
    53.             row = sheet.CreateRow(i);
    54.             cell = row.CreateCell(0);
    55.             cell.SetCellValue(i);
    56.         }
    57.  
    58.         fs2 = File.Create(filePath2);
    59.         wk.Write(fs2);
    60.         fs2.Close();
    61.         fs2.Dispose();
    62.         Debug.Log("form successfully created");
    63.     }
    64.  
    65.     /// <summary>
    66.     /// read form2
    67.     /// </summary>
    68.     public void LoadExcel()
    69.     {
    70.         results = new List<string>();
    71.         fs = File.OpenRead(filePath);
    72.         wk = new HSSFWorkbook(fs);
    73.         sheet = wk.GetSheetAt(0);
    74.         for (int j = 2; j <= sheet.LastRowNum; j++)
    75.         {
    76.             row = sheet.GetRow(j);
    77.             if (row != null)
    78.             {
    79.                 for (int k = 0; k < row.LastCellNum; k++)
    80.                 {
    81.                     //Debug.Log(row.GetCell(k).ToString());
    82.                     results.Add(row.GetCell(k).ToString());
    83.                 }
    84.             }
    85.         }
    86.     }
    87.  
    88. /// <summary>
    89.     /// update form
    90.     /// </summary>
    91.     public void UpdateExcel2(string[] Value)
    92.     {
    93.         //results = new List<string>();
    94.         fs2 = new FileStream (filePath2,FileMode.OpenOrCreate,FileAccess.ReadWrite);
    95.         wk2 = new HSSFWorkbook(fs2);
    96.         sheet2 = wk2.GetSheetAt(0);
    97.         for (int j = 0; j <= sheet2.LastRowNum; j++)
    98.         {
    99.             row2 = sheet2.GetRow(j);
    100.             if (row2 != null)
    101.             {
    102.                 for (int k = 1; k < row2.LastCellNum; k++)
    103.                 {
    104.                     cell2 = row2.CreateCell(1);
    105.                     cell2.SetCellValue(Value[j]);
    106.                 }
    107.             }
    108.         }
    109.         fs2.Close();
    110.         fs2 = File.OpenWrite(filePath2);
    111.         wk2.Write(fs2);
    112.         fs2.Close();
    113.         fs2.Dispose();
    114.     }    
    115. }
    116.  
    LoadTable, LoadTable2, CreateTable work fine, but UpdateExcel2 don't.
     
  9. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,021
  10. nikitabelykov

    nikitabelykov

    Joined:
    Jan 24, 2023
    Posts:
    6
    С проблемой разобрался сам, перерыв множество форумов и получив советы. Дело было с недоработкой библиотеки разработчиком. Также очень помогла эта тема:
    https://answers.unity.com/ques... build.html