Example #1
0
        internal static XlsxReader Create(ZipEntry entry)
        {
            if (entry == null)
            {
                return(null);
            }
            var inst = new XlsxReader();

            do
            {
                if (inst._TryReadWorkbookXml(entry) ||
                    inst._TryReadWorkbookRels(entry) ||
                    inst._TryLoadSharedStringsXml(entry) ||
                    inst._TryLoadSheetXml(entry) ||
                    inst._TryLoadSheetRels(entry) ||
                    inst._TryLoadCommentsXml(entry)
                    )
                {
                }
            } while ((entry = entry.Next()) != null);
            if (inst._sheetNames.Count == 0)
            {
                inst.Dispose();
                return(null);
            }
            inst.sheetCount = inst._sheetRelsIds.Count;
            inst.sheetIndex = -1;
            inst.MoveToNextSheet();
            return(inst);
        }
Example #2
0
        private List <KeyValuePair <CellRef, string> > _MakeDtsTexts(
            XlsxRequest request, XlsxReader reader)
        {
            var texts = new List <KeyValuePair <CellRef, string> >();

            foreach (var pdtsText in request.pdtsTexts)
            {
                if (pdtsText.sheetIndex == reader.sheetIndex)
                {
                    texts.Add(new KeyValuePair <CellRef, string>(
                                  pdtsText.cellRef, pdtsText.text
                                  ));
                }
            }
            texts.AddRange(reader.GetCellComments());
            return(texts);
        }
Example #3
0
        private IEnumerator _Parse()
        {
            while (progress < 0.3F)
            {
                progress = 0.3F * _GetLoadProgress();
                yield return(null);
            }
            progress = 0.3F;
            for (var n = 0; n < requests.Count; ++n)
            {
                var result = _loadingResults[n];
                if (string.IsNullOrEmpty(result.error))
                {
                    var req         = requests[n];
                    var entry       = ZipEntry.Create(result.stream);
                    var hasPassword = !string.IsNullOrEmpty(req.password);
                    if (entry == null && hasPassword)
                    {
                        var dr = new XlsxDecryption.Result();
                        yield return(_coroutineRunner.StartCoroutine(
                                         XlsxDecryption
                                         .Decrypt(result.stream, req.password, dr)
                                         ));

                        if (!string.IsNullOrEmpty(dr.error))
                        {
                            result.error = dr.error;
                        }
                        else if (dr.bytes != null)
                        {
                            result.stream.Dispose();
                            result.stream = new MemoryStream(dr.bytes);
                            entry         = ZipEntry.Create(result.stream);
                        }
                    }
                    else if (entry == null && !hasPassword)
                    {
                        if (XlsxDecryption.IsEncryptedFile(result.stream))
                        {
                            result.error = "Password required.";
                        }
                    }
                    _readers[n] = XlsxReader.Create(entry);
                    if (_readers[n] == null &&
                        string.IsNullOrEmpty(result.error))
                    {
                        result.error = "Invalid xlsx stream.";
                    }
                }
                progress = 0.3F + (0.2F * ((float)n / requests.Count));
                yield return(null);
            }
            progress = 0.5F;
            XlsxReader lastReader = null;

            for (var n = 0; n < requests.Count;)
            {
                yield return(null);

                var reader = _readers[n];
                if (reader == null)
                {
                    ++n; continue;
                }
                System.Action updateProgress = () => {
                    var c = 0.5F * (1.0F / requests.Count);
                    progress = 0.5F + (c * (n + reader.progress));
                    progress = Mathf.Min(0.99F, progress);
                };
                updateProgress();
                if (!ReferenceEquals(reader, lastReader))
                {
                    reader.Read();
                    lastReader = reader;
                }
                var sheetIndex = reader.sheetIndex;
                if (sheetIndex == reader.sheetCount)
                {
                    ++n; continue;
                }
                var schemas = _CreateSchemas(requests[n], n);
                if (schemas.Count == 0)
                {
                    reader.MoveToNextSheet();
                    continue;
                }
                var builders       = _CreateBuilders(schemas);
                var appendingCount = 0;
                do
                {
                    builders.ForEach(x => x.tryAppendCell(reader));
                    appendingCount += builders.Count;
                    if (appendingCount >= 5000)
                    {
                        appendingCount = 0;
                        updateProgress();
                        yield return(null);
                    }
                    reader.Read();
                } while (sheetIndex == reader.sheetIndex);
                updateProgress();
                foreach (var builder in builders)
                {
                    var ftc   = requests[n].fieldTypeConverter;
                    var table = builder.Create(ftc);
                    _tables[table.name] = table;
                    yield return(null);
                }
            }
            progress     = 1;
            dataSet      = new DataSet(_tables);
            _elapsedTime = Time.realtimeSinceStartup - startTime;
            Dispose();
        }
Example #4
0
 //监听自定义资源导入
 public override void OnImportAsset(AssetImportContext ctx)
 {
     XlsxReader.Excute(ctx.assetPath);
 }