public void BaseTestWithHeaderRow(string filename)
        {
            ParserContext      context = new ParserContext(TestDataSample.GetExcelPath(filename));
            ISpreadsheetParser parser  = ParserFactory.CreateSpreadsheet(context);

            parser.Context.Properties.Add("HasHeader", "1");
            ToxySpreadsheet ss = parser.Parse();

            Assert.AreEqual(1, ss.Tables[0].HeaderRows.Count);
            Assert.AreEqual("A", ss.Tables[0].HeaderRows[0].Cells[0].Value);
            Assert.AreEqual("B", ss.Tables[0].HeaderRows[0].Cells[1].Value);
            Assert.AreEqual("C", ss.Tables[0].HeaderRows[0].Cells[2].Value);
            Assert.AreEqual("D", ss.Tables[0].HeaderRows[0].Cells[3].Value);
            Assert.AreEqual(3, ss.Tables[0].Rows.Count);
            Assert.AreEqual("1", ss.Tables[0].Rows[0].Cells[0].Value);
            Assert.AreEqual("2", ss.Tables[0].Rows[0].Cells[1].Value);
            Assert.AreEqual("3", ss.Tables[0].Rows[0].Cells[2].Value);
            Assert.AreEqual("4", ss.Tables[0].Rows[0].Cells[3].Value);

            Assert.AreEqual("A1", ss.Tables[0].Rows[1].Cells[0].Value);
            Assert.AreEqual("A2", ss.Tables[0].Rows[1].Cells[1].Value);
            Assert.AreEqual("A3", ss.Tables[0].Rows[1].Cells[2].Value);
            Assert.AreEqual("A4", ss.Tables[0].Rows[1].Cells[3].Value);

            Assert.AreEqual("B1", ss.Tables[0].Rows[2].Cells[0].Value);
            Assert.AreEqual("B2", ss.Tables[0].Rows[2].Cells[1].Value);
            Assert.AreEqual("B3", ss.Tables[0].Rows[2].Cells[2].Value);
            Assert.AreEqual("B4", ss.Tables[0].Rows[2].Cells[3].Value);
        }
Exemple #2
0
        public ToxySpreadsheet Parse()
        {
            if (!File.Exists(Context.Path))
            {
                throw new FileNotFoundException("File " + Context.Path + " is not found");
            }

            bool hasHeader = false;

            if (Context.Properties.ContainsKey("HasHeader"))
            {
                hasHeader = Utility.IsTrue(Context.Properties["HasHeader"]);
            }
            bool extractHeader = false;

            if (Context.Properties.ContainsKey("ExtractSheetHeader"))
            {
                extractHeader = Utility.IsTrue(Context.Properties["ExtractSheetHeader"]);
            }
            bool extractFooter = false;

            if (Context.Properties.ContainsKey("ExtractSheetFooter"))
            {
                extractFooter = Utility.IsTrue(Context.Properties["ExtractSheetFooter"]);
            }
            bool showCalculatedResult = false;

            if (Context.Properties.ContainsKey("ShowCalculatedResult"))
            {
                showCalculatedResult = Utility.IsTrue(Context.Properties["ShowCalculatedResult"]);
            }
            bool fillBlankCells = false;

            if (Context.Properties.ContainsKey("FillBlankCells"))
            {
                fillBlankCells = Utility.IsTrue(Context.Properties["FillBlankCells"]);
            }
            bool includeComment = true;

            if (Context.Properties.ContainsKey("IncludeComments"))
            {
                includeComment = Utility.IsTrue(Context.Properties["IncludeComments"]);
            }
            ToxySpreadsheet ss = new ToxySpreadsheet();

            ss.Name = Context.Path;
            IWorkbook workbook = WorkbookFactory.Create(Context.Path);

            HSSFDataFormatter formatter = new HSSFDataFormatter();

            for (int i = 0; i < workbook.NumberOfSheets; i++)
            {
                ToxyTable table = Parse(workbook, i, extractHeader, extractFooter, hasHeader, fillBlankCells, includeComment, formatter);

                ss.Tables.Add(table);
            }
            return(ss);
        }
        public void BaseTestWithoutHeader(string filename)
        {
            ParserContext      context = new ParserContext(TestDataSample.GetExcelPath(filename));
            ISpreadsheetParser parser  = ParserFactory.CreateSpreadsheet(context);
            ToxySpreadsheet    ss      = parser.Parse();

            Assert.IsNull(ss.Tables[0].PageHeader);

            Assert.AreEqual(0, ss.Tables[0].HeaderRows.Count);
            Assert.AreEqual(9, ss.Tables[0].Rows.Count);
        }
        public void BaseTestHeader(string filename)
        {
            ParserContext      context = new ParserContext(TestDataSample.GetExcelPath(filename));
            ISpreadsheetParser parser  = ParserFactory.CreateSpreadsheet(context);
            ToxySpreadsheet    ss      = parser.Parse();

            Assert.IsNull(ss.Tables[0].PageHeader);

            parser.Context.Properties.Add("ExtractSheetHeader", "1");
            ToxySpreadsheet ss2 = parser.Parse();

            Assert.IsNotNull(ss2.Tables[0].PageHeader);
            Assert.AreEqual("|testdoc|test phrase", ss2.Tables[0].PageHeader);
        }
        public void BaseTestExcelComment(string filename)
        {
            ParserContext      context = new ParserContext(TestDataSample.GetExcelPath(filename));
            ISpreadsheetParser parser  = ParserFactory.CreateSpreadsheet(context);
            ToxySpreadsheet    ss      = parser.Parse();

            Assert.AreEqual(3, ss.Tables.Count);
            Assert.AreEqual(3, ss.Tables[0].Rows.Count);
            Assert.AreEqual(0, ss.Tables[0].Rows[1].Cells[0].CellIndex);
            Assert.AreEqual(2, ss.Tables[0].Rows[1].RowIndex);
            //TODO: fix this comment without cell value
            Assert.AreEqual("comment top row1 (index0)\n", ss.Tables[0].Rows[0].Cells[0].Comment);
            Assert.AreEqual("comment top row3 (index2)\n", ss.Tables[0].Rows[1].Cells[0].Comment);
            Assert.AreEqual("comment top row4 (index3)\n", ss.Tables[0].Rows[2].Cells[0].Comment);
        }
        public ToxySpreadsheet Parse()
        {
            if (!File.Exists(Context.Path))
                throw new FileNotFoundException("File " + Context.Path + " is not found");

            bool hasHeader = false;
            if (Context.Properties.ContainsKey("HasHeader"))
            {
                hasHeader = Utility.IsTrue(Context.Properties["HasHeader"]);
            }
            bool extractHeader = false;
            if (Context.Properties.ContainsKey("ExtractSheetHeader"))
            {
                extractHeader = Utility.IsTrue(Context.Properties["ExtractSheetHeader"]);
            }
            bool extractFooter = false;
            if (Context.Properties.ContainsKey("ExtractSheetFooter"))
            {
                extractFooter = Utility.IsTrue(Context.Properties["ExtractSheetFooter"]);
            }
            bool showCalculatedResult = false;
            if (Context.Properties.ContainsKey("ShowCalculatedResult"))
            {
                showCalculatedResult = Utility.IsTrue(Context.Properties["ShowCalculatedResult"]);
            }
            bool fillBlankCells = false;
            if (Context.Properties.ContainsKey("FillBlankCells"))
            {
                fillBlankCells = Utility.IsTrue(Context.Properties["FillBlankCells"]);
            }
            bool includeComment = true;
            if (Context.Properties.ContainsKey("IncludeComments"))
            {
                includeComment = Utility.IsTrue(Context.Properties["IncludeComments"]);
            }
            ToxySpreadsheet ss = new ToxySpreadsheet();
            ss.Name = Context.Path;
            IWorkbook workbook = WorkbookFactory.Create(Context.Path);

            HSSFDataFormatter formatter = new HSSFDataFormatter();
            for (int i = 0; i < workbook.NumberOfSheets; i++)
            {
                ToxyTable table = Parse(workbook, i, extractHeader, extractFooter, hasHeader, fillBlankCells, includeComment, formatter);

                ss.Tables.Add(table);
            }
            return ss;
        }
Exemple #7
0
        protected void Page_Load(object sender, EventArgs e)
        {
            //string path = Server.MapPath("~") + "\\AreaSearch.xlsx";
            //string path = Server.MapPath("~") + "App_Data\\";
            //string filename =  Session["FileName"].ToString();



            //string copyPath = path + r.Next().ToString() + ext;
            string path = Session["FileName"].ToString();
            //File.Copy(path + filename, path);

            ParserContext      context = new ParserContext(path);
            ISpreadsheetParser parser  = ParserFactory.CreateSpreadsheet(context);
            ToxySpreadsheet    ss      = parser.Parse();
            DataSet            ds      = ss.ToDataSet();

            if (ds.Tables.Count > 0)
            {
                GridView1.DataSource = ds.Tables[0];
                GridView1.DataBind();
            }

            if (ds.Tables.Count > 1)
            {
                GridView2.DataSource = ds.Tables[1];
                GridView2.DataBind();
            }

            if (ds.Tables.Count > 2)
            {
                GridView3.DataSource = ds.Tables[2];
                GridView3.DataBind();
            }

            //using (var stream = File.Open(path, FileMode.Open, FileAccess.Write, FileShare.ReadWrite))
            //{

            //}

            //if ((System.IO.File.Exists(path)))
            //{
            //    System.IO.File.Delete(path);
            //}
        }
        public void BaseTestExcelFormatedString(string filename)
        {
            ParserContext      context = new ParserContext(TestDataSample.GetExcelPath(filename));
            ISpreadsheetParser parser  = ParserFactory.CreateSpreadsheet(context);
            ToxySpreadsheet    ss      = parser.Parse();

            Assert.AreEqual(13, ss.Tables[0].Rows.Count);
            Assert.AreEqual("Dates, all 24th November 2006", ss.Tables[0].Rows[0].Cells[0].ToString());
            Assert.AreEqual("24/11/2006", ss.Tables[0].Rows[1].Cells[1].ToString());
            Assert.AreEqual("2006/11/24", ss.Tables[0].Rows[2].Cells[1].ToString());
            Assert.AreEqual("2006-11-24", ss.Tables[0].Rows[3].Cells[1].ToString());
            Assert.AreEqual("06/11/24", ss.Tables[0].Rows[4].Cells[1].ToString());
            Assert.AreEqual("24/11/06", ss.Tables[0].Rows[5].Cells[1].ToString());
            Assert.AreEqual("24-11-06", ss.Tables[0].Rows[6].Cells[1].ToString());

            Assert.AreEqual("10.52", ss.Tables[0].Rows[9].Cells[1].ToString());
            Assert.AreEqual("10.520", ss.Tables[0].Rows[10].Cells[1].ToString());
            Assert.AreEqual("10.5", ss.Tables[0].Rows[11].Cells[1].ToString());
            Assert.AreEqual("£10.52", ss.Tables[0].Rows[12].Cells[1].ToString());
        }
Exemple #9
0
        public ExcelTableTemplate Parse(string filepath)
        {
            var templateMetadata = new TemplateMetaData();

            if (filepath.StartsWith("/"))
            {
                filepath = HostingEnvironment.MapPath(filepath);
            }
            FilePath = filepath;
            ParserContext      context = new ParserContext(filepath);
            ISpreadsheetParser parser  = ParserFactory.CreateSpreadsheet(context);
            ToxySpreadsheet    ss      = parser.Parse();

            foreach (var table in ss.Tables)
            {
                _currTable = table;
                for (_currRowIndex = 0; _currRowIndex < table.Rows.Count; _currRowIndex++)
                {
                    var row = table.Rows[_currRowIndex];
                    for (_currColIndex = 0; _currColIndex < row.Cells.Count; _currColIndex++)
                    {
                        var cell = row.Cells[_currColIndex];

                        var tabletoken = ParseTable(cell);
                        if (tabletoken != null)
                        {
                            templateMetadata.Tables.Add(tabletoken);
                        }
                        else
                        {
                            var cellToken = ParseCell(cell);
                            if (cellToken != null)
                            {
                                templateMetadata.Cells.Add(cellToken);
                            }
                        }
                    }
                }
            }
            return(null);
        }
Exemple #10
0
        public void TestParseToxySpreadsheet()
        {
            string path = TestDataSample.GetFilePath("countrylist.csv", null);

            ParserContext context = new ParserContext(path);

            context.Properties.Add("HasHeader", "1");
            ISpreadsheetParser parser = (ISpreadsheetParser)ParserFactory.CreateSpreadsheet(context);
            ToxySpreadsheet    ss     = parser.Parse();

            Assert.AreEqual(1, ss.Tables.Count);
            Assert.AreEqual(14, ss.Tables[0].ColumnHeaders.Cells.Count);
            Assert.AreEqual("Sort Order", ss.Tables[0].ColumnHeaders.Cells[0].Value);
            Assert.AreEqual("Sub Type", ss.Tables[0].ColumnHeaders.Cells[4].Value);
            Assert.AreEqual(272, ss.Tables[0].Rows.Count);
            Assert.AreEqual(12, ss.Tables[0].Rows[12].RowIndex);
            Assert.AreEqual("Kingdom of Bahrain", ss.Tables[0].Rows[12].Cells[2].ToString());
            Assert.AreEqual(".bo", ss.Tables[0].Rows[20].Cells[13].ToString());

            Assert.AreEqual(271, ss.Tables[0].LastRowIndex);
        }
Exemple #11
0
        public void BaseTestFillBlankCells(string filename)
        {
            ParserContext      context = new ParserContext(TestDataSample.GetExcelPath(filename));
            ISpreadsheetParser parser  = ParserFactory.CreateSpreadsheet(context);
            ToxySpreadsheet    ss      = parser.Parse();

            Assert.AreEqual(1, ss.Tables[0].Rows[0].Cells.Count);
            Assert.AreEqual(0, ss.Tables[0].Rows[1].Cells.Count);
            Assert.AreEqual(2, ss.Tables[0].Rows[2].Cells.Count);
            Assert.AreEqual(2, ss.Tables[0].Rows[3].Cells.Count);
            Assert.AreEqual(2, ss.Tables[0].Rows[4].Cells.Count);

            parser.Context.Properties.Add("FillBlankCells", "1");
            ToxySpreadsheet ss2 = parser.Parse();

            Assert.AreEqual(3, ss2.Tables[0].Rows[0].Cells.Count);
            Assert.AreEqual(3, ss2.Tables[0].Rows[1].Cells.Count);
            Assert.AreEqual(3, ss2.Tables[0].Rows[2].Cells.Count);
            Assert.AreEqual(3, ss2.Tables[0].Rows[3].Cells.Count);
            Assert.AreEqual(3, ss2.Tables[0].Rows[4].Cells.Count);
        }
        public ToxySpreadsheet LoadData(DataContext context)
        {
            Database db = DatabaseFactory.CreateDatabase(context.ConnectionStringName);

            DbCommand       cmd = db.GetSqlStringCommand(context.QueryString);
            ToxySpreadsheet ss  = new ToxySpreadsheet();

            ss.Name = context.ConnectionStringName;
            int i = 0;

            using (IDataReader reader = db.ExecuteReader(cmd))
            {
                ToxyTable table = new ToxyTable();
                table.Name = "Sheet1";
                ToxyRow row = new ToxyRow(i);
                if (i == 0)
                {
                    for (int j = 0; j < reader.FieldCount; j++)
                    {
                        row.Cells.Add(new ToxyCell(j, reader.GetName(i)));
                    }
                }
                else
                {
                    for (int j = 0; j < reader.FieldCount; j++)
                    {
                        string value = reader.GetString(j);
                        if (!string.IsNullOrEmpty(value))
                        {
                            row.Cells.Add(new ToxyCell(j, value));
                        }
                    }
                }
                table.Rows.Add(row);
                i++;
                ss.Tables.Add(table);
            }
            return(ss);
        }
Exemple #13
0
        public void BaseTestExcelContent(string filename)
        {
            ParserContext      context = new ParserContext(TestDataSample.GetExcelPath(filename));
            ISpreadsheetParser parser  = ParserFactory.CreateSpreadsheet(context);
            ToxySpreadsheet    ss      = parser.Parse();

            Assert.AreEqual(3, ss.Tables.Count);
            Assert.AreEqual("Sheet1", ss.Tables[0].Name);
            Assert.AreEqual("Sheet2", ss.Tables[1].Name);
            Assert.AreEqual("Sheet3", ss.Tables[2].Name);
            Assert.AreEqual(5, ss.Tables[0].Rows.Count);
            Assert.AreEqual(0, ss.Tables[1].Rows.Count);
            Assert.AreEqual(0, ss.Tables[2].Rows.Count);

            ToxyTable table = ss.Tables[0];

            Assert.AreEqual(1, table.Rows[0].RowIndex);
            Assert.AreEqual(2, table.Rows[1].RowIndex);
            Assert.AreEqual(3, table.Rows[2].RowIndex);
            Assert.AreEqual(4, table.Rows[3].RowIndex);
            Assert.AreEqual(5, table.Rows[4].RowIndex);


            Assert.AreEqual(1, table.Rows[0].Cells.Count);
            Assert.AreEqual(0, table.Rows[1].Cells.Count);
            Assert.AreEqual(2, table.Rows[2].Cells.Count);
            Assert.AreEqual(2, table.Rows[3].Cells.Count);
            Assert.AreEqual(2, table.Rows[4].Cells.Count);
            Assert.AreEqual("Employee Info", table.Rows[0].Cells[0].ToString());
            Assert.AreEqual(1, table.Rows[0].Cells[0].CellIndex);
            Assert.AreEqual("Last name:", table.Rows[2].Cells[0].ToString());
            Assert.AreEqual(1, table.Rows[2].Cells[0].CellIndex);
            Assert.AreEqual("lastName", table.Rows[2].Cells[1].ToString());
            Assert.AreEqual(2, table.Rows[2].Cells[1].CellIndex);
            Assert.AreEqual("First name:", table.Rows[3].Cells[0].ToString());
            Assert.AreEqual("firstName", table.Rows[3].Cells[1].ToString());
            Assert.AreEqual("SSN:", table.Rows[4].Cells[0].ToString());
            Assert.AreEqual("ssn", table.Rows[4].Cells[1].ToString());
        }
Exemple #14
0
        public ToxySpreadsheet Parse()
        {
            if (!File.Exists(Context.Path))
            {
                throw new FileNotFoundException("File " + Context.Path + " is not found");
            }

            bool hasHeader = false;

            if (Context.Properties.ContainsKey("GenerateColumnHeader"))
            {
                hasHeader = Utility.IsTrue(Context.Properties["GenerateColumnHeader"]);
            }
            bool extractHeader = false;

            if (Context.Properties.ContainsKey("ExtractSheetHeader"))
            {
                extractHeader = Utility.IsTrue(Context.Properties["ExtractSheetHeader"]);
            }
            bool extractFooter = false;

            if (Context.Properties.ContainsKey("ExtractSheetFooter"))
            {
                extractFooter = Utility.IsTrue(Context.Properties["ExtractSheetFooter"]);
            }
            bool showCalculatedResult = false;

            if (Context.Properties.ContainsKey("ShowCalculatedResult"))
            {
                showCalculatedResult = Utility.IsTrue(Context.Properties["ShowCalculatedResult"]);
            }
            bool fillBlankCells = false;

            if (Context.Properties.ContainsKey("FillBlankCells"))
            {
                fillBlankCells = Utility.IsTrue(Context.Properties["FillBlankCells"]);
            }
            bool includeComment = true;

            if (Context.Properties.ContainsKey("IncludeComments"))
            {
                includeComment = Utility.IsTrue(Context.Properties["IncludeComments"]);
            }
            ToxySpreadsheet ss = new ToxySpreadsheet();

            ss.Name = Context.Path;
            IWorkbook workbook = WorkbookFactory.Create(Context.Path);

            HSSFDataFormatter formatter = new HSSFDataFormatter();

            for (int i = 0; i < workbook.NumberOfSheets; i++)
            {
                ToxyTable table = new ToxyTable();
                ISheet    sheet = workbook.GetSheetAt(i);
                table.Name = sheet.SheetName;

                if (extractHeader && sheet.Header != null)
                {
                    table.PageHeader = sheet.Header.Left + "|" + sheet.Header.Center + "|" + sheet.Header.Right;
                }

                if (extractFooter && sheet.Footer != null)
                {
                    table.PageFooter = sheet.Footer.Left + "|" + sheet.Footer.Center + "|" + sheet.Footer.Right;
                }

                bool firstRow = true;
                table.LastRowIndex = sheet.LastRowNum;
                foreach (IRow row in sheet)
                {
                    ToxyRow tr = null;
                    if (!hasHeader || !firstRow)
                    {
                        tr = new ToxyRow(row.RowNum);
                    }
                    foreach (ICell cell in row)
                    {
                        if (hasHeader && firstRow)
                        {
                            table.ColumnHeaders.Cells.Add(new ToxyCell(cell.ColumnIndex, cell.ToString()));
                        }
                        else
                        {
                            if (tr.LastCellIndex < cell.ColumnIndex)
                            {
                                tr.LastCellIndex = cell.ColumnIndex;
                            }
                            ToxyCell c = new ToxyCell(cell.ColumnIndex, formatter.FormatCellValue(cell));
                            if (!string.IsNullOrEmpty(cell.ToString()))
                            {
                                tr.Cells.Add(c);
                            }
                            else if (fillBlankCells)
                            {
                                tr.Cells.Add(c);
                            }
                            if (includeComment && cell.CellComment != null)
                            {
                                c.Comment = cell.CellComment.String.String;
                            }
                        }
                    }
                    if (tr != null)
                    {
                        tr.RowIndex = row.RowNum;
                        table.Rows.Add(tr);
                    }
                    if (firstRow)
                    {
                        firstRow = false;
                    }
                    if (table.LastColumnIndex < tr.LastCellIndex)
                    {
                        table.LastColumnIndex = tr.LastCellIndex;
                    }
                }
                for (int j = 0; j < sheet.NumMergedRegions; j++)
                {
                    var range = sheet.GetMergedRegion(j);
                    table.MergeCells.Add(new MergeCellRange()
                    {
                        FirstRow = range.FirstRow, FirstColumn = range.FirstColumn, LastRow = range.LastRow, LastColumn = range.LastColumn
                    });
                }
                ss.Tables.Add(table);
            }
            return(ss);
        }
Exemple #15
0
        public ToxySpreadsheet Parse()
        {
            if (!File.Exists(Context.Path))
                throw new FileNotFoundException("File " + Context.Path + " is not found");

            bool hasHeader = false;
            if (Context.Properties.ContainsKey("GenerateColumnHeader"))
            {
                hasHeader = Utility.IsTrue(Context.Properties["GenerateColumnHeader"]);
            }
            bool extractHeader = false;
            if (Context.Properties.ContainsKey("ExtractSheetHeader"))
            {
                extractHeader = Utility.IsTrue(Context.Properties["ExtractSheetHeader"]);
            }
            bool extractFooter = false;
            if (Context.Properties.ContainsKey("ExtractSheetFooter"))
            {
                extractFooter = Utility.IsTrue(Context.Properties["ExtractSheetFooter"]);
            }
            bool showCalculatedResult = false;
            if (Context.Properties.ContainsKey("ShowCalculatedResult"))
            {
                showCalculatedResult = Utility.IsTrue(Context.Properties["ShowCalculatedResult"]);
            }
            bool fillBlankCells = false;
            if (Context.Properties.ContainsKey("FillBlankCells"))
            {
                fillBlankCells = Utility.IsTrue(Context.Properties["FillBlankCells"]);
            }
            bool includeComment = true;
            if (Context.Properties.ContainsKey("IncludeComments"))
            {
                includeComment = Utility.IsTrue(Context.Properties["IncludeComments"]);
            }
            ToxySpreadsheet ss = new ToxySpreadsheet();
            ss.Name = Context.Path;
            IWorkbook workbook = WorkbookFactory.Create(Context.Path);
           
            HSSFDataFormatter formatter = new HSSFDataFormatter();
            for (int i = 0; i < workbook.NumberOfSheets; i++)
            {
                ToxyTable table=new ToxyTable();
                ISheet sheet = workbook.GetSheetAt(i);
                table.Name = sheet.SheetName;
                
                if (extractHeader && sheet.Header != null)
                {
                    table.PageHeader = sheet.Header.Left + "|" + sheet.Header.Center + "|" + sheet.Header.Right;
                }

                if (extractFooter && sheet.Footer != null)
                {
                    table.PageFooter = sheet.Footer.Left + "|" + sheet.Footer.Center + "|" + sheet.Footer.Right;
                }

                bool firstRow = true;
                table.LastRowIndex = sheet.LastRowNum;
                foreach (IRow row in sheet)
                {
                    ToxyRow tr=null;
                    if (!hasHeader || !firstRow)
                    {
                        tr=new ToxyRow(row.RowNum);
                    }
                    foreach (ICell cell in row)
                    {
                        if (hasHeader&& firstRow)
                        {
                            table.ColumnHeaders.Cells.Add(new ToxyCell(cell.ColumnIndex, cell.ToString()));
                        }
                        else 
                        {
                            if (tr.LastCellIndex < cell.ColumnIndex)
                            {
                                tr.LastCellIndex = cell.ColumnIndex;
                            }
                            ToxyCell c = new ToxyCell(cell.ColumnIndex, formatter.FormatCellValue(cell));
                            if (!string.IsNullOrEmpty(cell.ToString()))
                            {
                                tr.Cells.Add(c);
                            }
                            else if (fillBlankCells)
                            {
                                tr.Cells.Add(c);
                            }
                            if (cell.CellComment != null)
                            {
                                c.Comment = cell.CellComment.String.String;
                            }
                        }
                    }
                    if (tr != null)
                    {
                        tr.RowIndex = row.RowNum;
                        table.Rows.Add(tr);
                    }
                    if (firstRow)
                    {
                        firstRow = false;
                    }
                    if(table.LastColumnIndex<tr.LastCellIndex)
                        table.LastColumnIndex=tr.LastCellIndex;
                }
                for (int j = 0; j < sheet.NumMergedRegions; j++)
                { 
                    var range = sheet.GetMergedRegion(j);
                    table.MergeCells.Add(new MergeCellRange() { FirstRow = range.FirstRow, FirstColumn = range.FirstColumn, LastRow = range.LastRow, LastColumn = range.LastColumn });
                }
                ss.Tables.Add(table);
            }
            if (workbook is XSSFWorkbook)
            {
                var props= ((XSSFWorkbook)workbook).GetProperties();

                if (props.CoreProperties != null)
                {
                    if (props.CoreProperties.Title != null)
                    {
                        ss.Properties.Add("Title", props.CoreProperties.Title );
                    }
                    else if (props.CoreProperties.Identifier != null)
                    {
                        ss.Properties.Add("Identifier", props.CoreProperties.Identifier );
                    }
                    else if (props.CoreProperties.Keywords != null)
                    {
                        ss.Properties.Add("Keywords", props.CoreProperties.Keywords);
                    }
                    else if (props.CoreProperties.Revision != null)
                    {
                        ss.Properties.Add("Revision", props.CoreProperties.Revision);
                    }
                    else if (props.CoreProperties.Subject != null)
                    {
                        ss.Properties.Add("Subject", props.CoreProperties.Subject);
                    }
                    else if (props.CoreProperties.Modified != null)
                    {
                        ss.Properties.Add("Modified", props.CoreProperties.Modified);
                    }
                    else if (props.CoreProperties.LastPrinted != null)
                    {
                        ss.Properties.Add("LastPrinted", props.CoreProperties.LastPrinted);
                    }
                    else if (props.CoreProperties.Created != null)
                    {
                        ss.Properties.Add("Created", props.CoreProperties.Created);
                    }
                    else if (props.CoreProperties.Creator != null)
                    {
                        ss.Properties.Add("Creator", props.CoreProperties.Creator);
                    }
                    else if (props.CoreProperties.Description != null)
                    {
                        ss.Properties.Add("Description", props.CoreProperties.Description);
                    }
                }
                if (props.ExtendedProperties != null && props.ExtendedProperties.props!=null)
                {
                    var extProps = props.ExtendedProperties.props.GetProperties();
                    if (extProps.Application != null)
                    {
                        ss.Properties.Add("Application", extProps.Application);
                    }
                    if (extProps.AppVersion != null)
                    {
                        ss.Properties.Add("AppVersion", extProps.AppVersion);
                    }
                    if (extProps.Characters>0)
                    {
                        ss.Properties.Add("Characters", extProps.Characters);
                    }
                    if (extProps.CharactersWithSpaces>0)
                    {
                        ss.Properties.Add("CharactersWithSpaces", extProps.CharactersWithSpaces);
                    }
                    if (extProps.Company != null)
                    {
                        ss.Properties.Add("Company", extProps.Company);
                    }
                    if (extProps.Lines > 0)
                    {
                        ss.Properties.Add("Lines", extProps.Lines);
                    }
                    if (extProps.Manager != null)
                    {
                        ss.Properties.Add("Manager", extProps.Manager);
                    }
                    if (extProps.Notes> 0)
                    {
                        ss.Properties.Add("Notes", extProps.Notes);
                    }
                    if (extProps.Pages>0)
                    {
                        ss.Properties.Add("Pages", extProps.Pages);
                    }
                    if (extProps.Paragraphs>0)
                    {
                        ss.Properties.Add("Paragraphs", extProps.Paragraphs);
                    }
                    if (extProps.Words>0)
                    {
                        ss.Properties.Add("Words", extProps.Words);
                    }
                    if (extProps.TotalTime>0)
                    {
                        ss.Properties.Add("TotalTime", extProps.TotalTime);
                    }
                }
            }
            else
            {
                //HSSFWorkbook
                var si = ((HSSFWorkbook)workbook).SummaryInformation;
                if (si != null)
                {
                    if (si.Title != null)
                    {
                        ss.Properties.Add("Title", si.Title);
                    }
                    else if (si.LastSaveDateTime != null)
                    {
                        ss.Properties.Add("LastSaveDateTime", si.LastSaveDateTime);
                    }
                    else if (si.PageCount > 0)
                    {
                        ss.Properties.Add("PageCount", si.PageCount);
                    }
                    else if (si.OSVersion > 0)
                    {
                        ss.Properties.Add("OSVersion", si.OSVersion);
                    }
                    else if (si.Security > 0)
                    {
                        ss.Properties.Add("Security", si.Security);
                    }
                    else if (si.Keywords != null)
                    {
                        ss.Properties.Add("Keywords", si.Keywords);
                    }
                    else if (si.EditTime > 0)
                    {
                        ss.Properties.Add("EditTime", si.EditTime);
                    }
                    else if (si.Subject != null)
                    {
                        ss.Properties.Add("Subject", si.Subject);
                    }
                    else if (si.CreateDateTime != null)
                    {
                        ss.Properties.Add("CreateDateTime", si.CreateDateTime);
                    }
                    else if (si.LastPrinted != null)
                    {
                        ss.Properties.Add("LastPrinted", si.LastPrinted);
                    }
                    else if (si.CharCount != null)
                    {
                        ss.Properties.Add("CharCount", si.CharCount);
                    }
                    else if (si.Author != null)
                    {
                        ss.Properties.Add("Author", si.Author);
                    }
                    else if (si.LastAuthor != null)
                    {
                        ss.Properties.Add("LastAuthor", si.LastAuthor);
                    }
                    else if (si.ApplicationName != null)
                    {
                        ss.Properties.Add("ApplicationName", si.ApplicationName);
                    }
                    else if (si.RevNumber != null)
                    {
                        ss.Properties.Add("RevNumber", si.RevNumber);
                    }
                    else if (si.Template != null)
                    {
                        ss.Properties.Add("Template", si.Template);
                    }
                }
                var dsi = ((HSSFWorkbook)workbook).DocumentSummaryInformation;
                if(dsi!=null)
                {
                    if (dsi.ByteCount > 0)
                    {
                        ss.Properties.Add("ByteCount", dsi.ByteCount);
                    }
                    else if (dsi.Company !=null)
                    {
                        ss.Properties.Add("Company", dsi.Company);
                    }
                    else if (dsi.Format>0)
                    {
                        ss.Properties.Add("Format", dsi.Format);
                    }
                    else if (dsi.LineCount!= null)
                    {
                        ss.Properties.Add("LineCount", dsi.Company);
                    }
                    else if (dsi.LinksDirty)
                    {
                        ss.Properties.Add("LinksDirty", true);
                    }
                    else if (dsi.Manager!=null)
                    {
                        ss.Properties.Add("Manager", dsi.Manager);
                    }
                    else if (dsi.NoteCount != null)
                    {
                        ss.Properties.Add("NoteCount", dsi.NoteCount);
                    }
                    else if (dsi.Scale)
                    {
                        ss.Properties.Add("Scale", dsi.Scale);
                    }
                    else if (dsi.Company != null)
                    {
                        ss.Properties.Add("Company", dsi.Company);
                    }
                    else if (dsi.MMClipCount != null)
                    {
                        ss.Properties.Add("MMClipCount", dsi.MMClipCount);
                    }
                    else if (dsi.ParCount != null)
                    {
                        ss.Properties.Add("ParCount", dsi.ParCount);
                    }
                }
            }
            return ss;
        }
Exemple #16
0
        private void ShowDocument(string filepath, string encoding, string extension)
        {
            ParserContext context = new ParserContext(filepath);

            context.Encoding = Encoding.GetEncoding(encoding);

            if (Mode == ViewMode.Text)
            {
                AppendRichTextBox();
                var tparser = ParserFactory.CreateText(context);
                rtbPanel.Text     = tparser.Parse();
                tbParserType.Text = tparser.GetType().Name;
            }
            else if (Mode == ViewMode.Structured)
            {
                switch (extension)
                {
                case ".csv":
                    AppendSpreadsheetGrid();
                    context.Properties.Add("HasHeader", "1");
                    ISpreadsheetParser csvparser = ParserFactory.CreateSpreadsheet(context);
                    ss = csvparser.Parse();
                    tbParserType.Text = csvparser.GetType().Name;
                    var table1 = ss.Tables[0];
                    ShowToGrid(table1);
                    cbSheets.Items.Clear();
                    foreach (var table in ss.Tables)
                    {
                        cbSheets.Items.Add(table.Name);
                    }
                    cbSheets.SelectedIndex = 0;
                    panel1.Visible         = true;
                    break;

                case ".xlsx":
                case ".xls":
                    AppendSpreadsheetGrid();
                    ISpreadsheetParser ssparser = ParserFactory.CreateSpreadsheet(context);
                    ss = ssparser.Parse();
                    tbParserType.Text = ssparser.GetType().Name;
                    var table0 = ss.Tables[0];
                    ShowToGrid(table0);
                    cbSheets.Items.Clear();
                    foreach (var table in ss.Tables)
                    {
                        cbSheets.Items.Add(table.Name);
                    }
                    cbSheets.SelectedIndex = 0;
                    panel1.Visible         = true;
                    break;

                case ".vcf":
                    AppendDataGridView();
                    var vparser = ParserFactory.CreateVCard(context);
                    ToxyBusinessCards vcards = vparser.Parse();
                    tbParserType.Text             = vparser.GetType().Name;
                    gridPanel.GridView.DataSource = vcards.ToDataTable().DefaultView;
                    break;

                case ".pptx":
                    //TODO: show slides
                    break;

                case ".xml":
                case ".htm":
                case ".html":
                    AppendTreePanel();
                    var      domparser = ParserFactory.CreateDom(context);
                    ToxyDom  htmlDom   = domparser.Parse();
                    TreeNode rootNode  = treePanel.Tree.Nodes.Add(htmlDom.Root.NodeString);
                    treePanel.Tree.BeginUpdate();
                    AppendTree(rootNode, htmlDom.Root);
                    treePanel.Tree.EndUpdate();
                    //rootNode.ExpandAll();
                    break;
                }
            }
            else
            {
                AppendPropertyListPanel();
                var          tparser   = ParserFactory.CreateMetadata(context);
                ToxyMetadata metadatas = tparser.Parse();
                plPanel.Clear();
                foreach (var data in metadatas)
                {
                    plPanel.AddItem(data.Name, data.Value.ToString());
                }
                tbParserType.Text = tparser.GetType().Name;
            }
        }
Exemple #17
0
        public ToxySpreadsheet Parse()
        {
            if (!File.Exists(Context.Path))
                throw new FileNotFoundException("File " + Context.Path + " is not found");

            bool extractHeader=false;
            if (Context.Properties.ContainsKey("ExtractHeader"))
            {
                string sHasHeader = Context.Properties["ExtractHeader"].ToLower();
                if (sHasHeader == "1" || sHasHeader == "on" || sHasHeader == "true")
                    extractHeader = true;
            }
            char delimiter =',';
            if (Context.Properties.ContainsKey("delimiter"))
            {
                delimiter = Context.Properties["delimiter"][0];
            }

            
            Encoding encoding = Encoding.UTF8;

            StreamReader sr = null;
            try
            {
                if (Context.Encoding == null)
                {
                    sr = new StreamReader(Context.Path, true);
                }
                else
                {
                    sr = new StreamReader(Context.Path, true);
                }
                CsvReader reader=new CsvReader(sr, extractHeader,delimiter);
                string[] headers = reader.GetFieldHeaders();
                ToxySpreadsheet ss = new ToxySpreadsheet();
                ToxyTable t1 = new ToxyTable();
                ss.Tables.Add(t1);

                int i = 0;
                if (headers.Length > 0)
                {
                    t1.HeaderRows.Add(new ToxyRow(i));
                    i++;
                }
                for (int j = 0; j < headers.Length;j++ )
                {
                    t1.HeaderRows[0].Cells.Add(new ToxyCell(j, headers[j]));
                    t1.LastColumnIndex = t1.HeaderRows[0].Cells.Count-1;
                }
                while(reader.ReadNextRecord())
                {
                    ToxyRow tr=new ToxyRow(i);
                    tr.LastCellIndex = reader.FieldCount-1;
                    if (tr.LastCellIndex > t1.LastColumnIndex)
                    {
                        t1.LastColumnIndex = tr.LastCellIndex;
                    }
                    tr.RowIndex = i;
                    for (int j = 0; j <= tr.LastCellIndex; j++)
                    {
                        if (this.ParseSegment != null)
                        {
                            this.ParseSegment(this, new ParseSegmentEventArgs(reader[j], i, j));
                        }
                        ToxyCell c = new ToxyCell(j, reader[j]);
                        if (tr.LastCellIndex < c.CellIndex)
                        {
                            tr.LastCellIndex = c.CellIndex;
                        }
                        tr.Cells.Add(c);
                    }
                    
                    t1.Rows.Add(tr);
                    i++;
                }
                t1.LastRowIndex = i - 1;
                return ss;
            }
            finally
            {
                if (sr != null)
                    sr.Close();
            }
        }
Exemple #18
0
        public new ToxySpreadsheet Parse()
        {
            if (!File.Exists(Context.Path))
                throw new FileNotFoundException("File " + Context.Path + " is not found");

            bool hasHeader=false;
            if (Context.Properties.ContainsKey("HasHeader"))
            {
                string sHasHeader = Context.Properties["HasHeader"].ToLower();
                if (sHasHeader == "1" || sHasHeader == "on" || sHasHeader == "true")
                    hasHeader = true;
            }
            char delimiter =',';
            if (Context.Properties.ContainsKey("delimiter"))
            {
                delimiter = Context.Properties["delimiter"][0];
            }

            
            Encoding encoding = Encoding.UTF8;

            StreamReader sr = null;
            try
            {
                if (Context.Encoding == null)
                {
                    sr = new StreamReader(Context.Path, true);
                }
                else
                {
                    sr = new StreamReader(Context.Path, true);
                }
                CsvReader reader=new CsvReader(sr, hasHeader,delimiter);
                string[] headers = reader.GetFieldHeaders();
                ToxySpreadsheet ss = new ToxySpreadsheet();
                ToxyTable t1 = new ToxyTable();
                ss.Tables.Add(t1);

                for (int j = 0; j < headers.Length;j++ )
                {
                    t1.ColumnHeaders.Cells.Add(new ToxyCell(j, headers[j]));
                }
                int i=0;
                while(reader.ReadNextRecord())
                {
                    ToxyRow tr=new ToxyRow(i);
                    tr.LastCellIndex = t1.ColumnHeaders.Cells.Count;
                    for (int j = 0; j < tr.LastCellIndex; j++)
                    {
                        ToxyCell c = new ToxyCell(j, reader[j]);
                        if (tr.LastCellIndex < c.CellIndex)
                        {
                            tr.LastCellIndex = c.CellIndex;
                        }
                        tr.Cells.Add(c);
                    }
                    
                    t1.Rows.Add(tr);
                }
                return ss;
            }
            finally
            {
                if (sr != null)
                    sr.Close();
            }
        }
Exemple #19
0
        public ToxySpreadsheet Parse()
        {
            if (!File.Exists(Context.Path))
            {
                throw new FileNotFoundException("File " + Context.Path + " is not found");
            }

            bool hasHeader = false;

            if (Context.Properties.ContainsKey("HasHeader"))
            {
                string sHasHeader = Context.Properties["HasHeader"].ToLower();
                if (sHasHeader == "1" || sHasHeader == "on" || sHasHeader == "true")
                {
                    hasHeader = true;
                }
            }
            char delimiter = ',';

            if (Context.Properties.ContainsKey("delimiter"))
            {
                delimiter = Context.Properties["delimiter"][0];
            }


            Encoding encoding = Encoding.UTF8;

            StreamReader sr = null;

            try
            {
                if (Context.Encoding == null)
                {
                    sr = new StreamReader(Context.Path, true);
                }
                else
                {
                    sr = new StreamReader(Context.Path, true);
                }
                CsvReader       reader  = new CsvReader(sr, hasHeader, delimiter);
                string[]        headers = reader.GetFieldHeaders();
                ToxySpreadsheet ss      = new ToxySpreadsheet();
                ToxyTable       t1      = new ToxyTable();
                ss.Tables.Add(t1);

                for (int j = 0; j < headers.Length; j++)
                {
                    t1.ColumnHeaders.Cells.Add(new ToxyCell(j, headers[j]));
                    t1.LastColumnIndex = t1.ColumnHeaders.Cells.Count - 1;
                }
                int i = 0;
                while (reader.ReadNextRecord())
                {
                    ToxyRow tr = new ToxyRow(i);
                    tr.LastCellIndex = reader.FieldCount - 1;
                    if (tr.LastCellIndex > t1.LastColumnIndex)
                    {
                        t1.LastColumnIndex = tr.LastCellIndex;
                    }
                    tr.RowIndex = i;
                    for (int j = 0; j <= tr.LastCellIndex; j++)
                    {
                        if (this.ParseSegment != null)
                        {
                            this.ParseSegment(this, new ParseSegmentEventArgs(reader[j], i, j));
                        }
                        ToxyCell c = new ToxyCell(j, reader[j]);
                        if (tr.LastCellIndex < c.CellIndex)
                        {
                            tr.LastCellIndex = c.CellIndex;
                        }
                        tr.Cells.Add(c);
                    }

                    t1.Rows.Add(tr);
                    i++;
                }
                t1.LastRowIndex = i - 1;
                return(ss);
            }
            finally
            {
                if (sr != null)
                {
                    sr.Close();
                }
            }
        }