Example #1
0
        //[MenuItem("Tools/Excel Importer/Import TestDataWorkbookImporter", priority = 300)]
        public static void Import()
        {
            try
            {
                EditorUtility.DisplayProgressBar("Excel Importer", "Loading Workbook", 0f);
                var workbook = ExcelWorkbookFactory.Create(ExcelFilePath);

                EditorUtility.DisplayProgressBar("Excel Importer", "Importing Sheet: TestThings", 1f);
                HandleSheet <TestThingsImport_TEMPLATE, TestThingsImport_TEMPLATE.Row>(workbook, "TestThings");



                EditorUtility.DisplayProgressBar("Excel Importer", "Saving Assets", 1f);
                AssetDatabase.SaveAssets();

                UnityEngine.Debug.Log($"Imported {workbook.Sheets.Count} sheets from {workbook.FilePath}");
            }
            catch (Exception e)
            {
                EditorUtility.DisplayDialog(nameof(TestDataWorkbookImporter_TEMPLATE), "Import Failed! See console log for details", "Ok");
                UnityEngine.Debug.LogError(e);
            }

            EditorUtility.ClearProgressBar();
        }
 public static void Process(string excelPath)
 {
     try
     {
         EditorUtils.Progress("Loading Excel", 0.0f);
         var excelWorkbook = ExcelWorkbookFactory.Create(excelPath);
         Process(excelWorkbook);
     }
     catch (Exception e)
     {
         OnProcessingError(e);
     }
 }
Example #3
0
        static void Main(string[] args)
        {
            string tableName = "Table", namedRangeName = "NamedRange", sheetName = "Data";
            string testExcelSheetName      = "Data.xlsx";
            string consoleProjectLocalPath = Directory.GetParent(Directory
                                                                 .GetParent(Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).FullName).FullName).FullName;

            //ClosedXML requires .xlsx and .xlsm files. Older version of excel are no compatible.
            string pathXls = consoleProjectLocalPath + "\\" + testExcelSheetName;
            IExcelWorkbookFactory factory = new ExcelWorkbookFactory();
            IExcelWorkbook        workbook;

            //First assume excel worksheet is closed and try to read with ClosedXML. IExcelWorkbook is opened in
            //ClosedXML mode.
            try
            {
                workbook = factory.GetExcelWorkbook(ExcelPackage.ClosedXml, pathXls);
            }
            //If excel workbook is open closed XML will throw an exception. The IExcelWorkbook will then be
            //opened in Interop mode
            catch (IOException e)
            {
                workbook = factory.GetExcelWorkbook(ExcelPackage.Interop, pathXls);
            }

            IRangeData importedDataTable = workbook.ReadNamedRangeOrTable(sheetName, tableName);

            string[,] tableTextData     = importedDataTable.GetTextArray();
            double?[,] tableNumericData = importedDataTable.GetNumericArray();

            IRangeData importedDataNamedRange = workbook.ReadNamedRangeOrTable(sheetName, namedRangeName);

            string[,] namedRangeTextData     = importedDataNamedRange.GetTextArray();
            double?[,] namedRangeNumericData = importedDataNamedRange.GetNumericArray();

            //table includes headers in ClosedXML model but not in Interop
            //so height is one less than the same named range in Interop
            bool lengthTest = tableTextData.GetLength(0) == (namedRangeTextData.GetLength(0) - 1);
            bool isInterop  = lengthTest;
        }