示例#1
0
        /// <summary>
        /// Load workbook from Stream.
        /// </summary>
        /// <param name="fileStream"></param>
        /// <returns></returns>
        public static Workbook Load(Stream stream)
        {
            CompoundDocument doc = CompoundDocument.Load(stream);

            if (doc == null)
            {
                throw new Exception("Invalid Excel file");
            }
            byte[] bookdata = doc.GetStreamData("Workbook");
            return(WorkbookDecoder.Decode(new MemoryStream(bookdata)));
        }
        /// <summary>
        /// parse the source file
        /// </summary>
        /// <param name="path">the file path</param>
        /// <returns>return true if parse successfully</returns>
        private bool Parse(string path)
        {
            try
            {
                CompoundDocument doc = CompoundDocument.Load(path);
                if (doc == null)
                {
                    throw new InvalidOperationException(Constants.Messages.Error_ExcelFileNotFound);
                }

                Lines.Clear();
                byte[] bookdata = doc.GetStreamData("Workbook");
                if (bookdata == null)
                {
                    throw new InvalidOperationException(Constants.Messages.Error_ExcelFileNoWorkbook);
                }

                Workbook workbook = WorkbookDecoder.Decode(new MemoryStream(bookdata));
                if (workbook.Worksheets.Count == 0)
                {
                    throw new InvalidOperationException(Constants.Messages.Error_ExcelFileNoWorksheet);
                }

                Worksheet sheet = workbook.Worksheets[0];
                m_WorksheetName = sheet.Name;
                for (int rowIndex = sheet.Cells.FirstRowIndex; rowIndex <= sheet.Cells.LastRowIndex; rowIndex++)
                {
                    SourceLine line = new SourceLine();
                    Row        row  = sheet.Cells.GetRow(rowIndex);
                    for (int colIndex = row.FirstColIndex; colIndex <= row.LastColIndex; colIndex++)
                    {
                        Cell cell = row.GetCell(colIndex);
                        line.Columns.Add(cell.StringValue);
                    }
                    Lines.Add(line);
                }

                doc.Close();
                return(true);
            }
            catch
            {
                Lines.Clear();
                throw;
            }
            finally
            {
                if (this.FileParsed != null)
                {
                    this.FileParsed();
                }
            }
        }