Esempio n. 1
0
        private void Dt_RowChanged(object sender, DataRowChangeEventArgs e)
        { // Change & Add
            try
            {
                var sheet = Bexcel.Sheets.First(x => x.Name == ((dynamic)sender).TableName);

                if (e.Action == DataRowAction.Add)
                {
                    var bexcelRow = new Definitions.Bexcel.Row
                    {
                        Index1 = -1,
                        Index2 = 1
                    };

                    for (int i = 0; i < e.Row.ItemArray.Length; i++)
                    {
                        bexcelRow.Cells.Add(new Definitions.Bexcel.Cell {
                            Index = i, Name = Convert.ToString(e.Row.ItemArray[i])
                        });
                    }

                    sheet.Rows.Add(bexcelRow);
                    Debug.WriteLine($"New row added to {((dynamic)sender).TableName} table");
                }
                else
                {
                    var rowIndex = sheet.Dt.Rows.IndexOf(e.Row);

                    for (int i = 0; i < e.Row.ItemArray.Length; i++)
                    {
                        if (sheet.Rows[rowIndex].Cells.Find(x => x.Index == i) != null)
                        {
                            sheet.Rows[rowIndex].Cells[i].Name = Convert.ToString(e.Row.ItemArray[i]);
                        }
                        else
                        {
                            sheet.Rows[rowIndex].Cells.Add(new Definitions.Bexcel.Cell
                            {
                                Index = i,
                                Name  = Convert.ToString(e.Row.ItemArray[i])
                            });
                        }
                    }

                    Debug.WriteLine($"Row updated on {((dynamic)sender).TableName} table");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error happened when updating the row.\n\n" + ex);
            }
        }
Esempio n. 2
0
        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (Bexcel == null || Sheet.ItemsSource == null)
                {
                    return;
                }

                DataView dataViewSheet = (DataView)Sheet.ItemsSource;
                var      sheet         = Bexcel.Sheets.First(x => x.Name == dataViewSheet.Table.TableName);

                foreach (DataRow row in sheet.Dt.Rows)
                {
                    var rowIndex = sheet.Dt.Rows.IndexOf(row);

                    if (sheet.Rows.ElementAtOrDefault(rowIndex) == null)
                    {
                        var bexcelRow = new Definitions.Bexcel.Row
                        {
                            Index1 = -1,
                            Index2 = 1
                        };

                        for (int i = 0; i < row.ItemArray.Length; i++)
                        {
                            bexcelRow.Cells.Add(new Definitions.Bexcel.Cell {
                                Name = (string)row.ItemArray[i]
                            });
                        }

                        sheet.Rows.Add(bexcelRow);
                    }
                    else
                    {
                        for (int i = 0; i < row.ItemArray.Length; i++)
                        {
                            sheet.Rows[rowIndex].Cells[i].Name = (string)row.ItemArray[i];
                        }
                    }
                }

                MessageBox.Show("Data saved to memory");
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error happened when updating the row.\n\n" + ex);
            }
        }
Esempio n. 3
0
        public static Bexcel Read(string inputPath)
        {
            try
            {
                var bexcel = new Bexcel();

                using (BinaryReader bReader = new BinaryReader(File.Open(inputPath, FileMode.Open, FileAccess.Read, FileShare.Read), Encoding.Unicode))
                {
                    int sheetCount = bReader.ReadInt32();

                    for (int i = 0; i < sheetCount; i++)
                    {
                        var tableName = ReadString(bReader);
                        var tableType = bReader.ReadInt32();

                        bexcel.Sheets.Add(new Bexcel.Sheet
                        {
                            Name = tableName,
                            Type = tableType
                        });
                    }

                    sheetCount = bReader.ReadInt32();
                    for (int j = 0; j < sheetCount; j++)
                    {
                        string sheetName    = ReadString(bReader);
                        var    currentSheet = bexcel.Sheets.First(x => x.Name == sheetName);

                        currentSheet.Index1 = bReader.ReadInt32();
                        currentSheet.Index2 = bReader.ReadInt32();

                        int columnCount = bReader.ReadInt32();
                        for (int k = 0; k < columnCount; k++)
                        {
                            currentSheet.Columns.Add(new Bexcel.Column
                            {
                                //Index = k,
                                Name = ReadString(bReader)
                            });
                        }

                        int rowCount = bReader.ReadInt32();
                        for (int l = 0; l < rowCount; l++)
                        {
                            Bexcel.Row row = new Bexcel.Row
                            {
                                //Index = l,
                                Index1 = bReader.ReadInt32(),
                                Index2 = bReader.ReadInt32()
                            };

                            int cellCount = bReader.ReadInt32();

                            for (int m = 0; m < cellCount; m++)
                            {
                                row.Cells.Add(new Bexcel.Cell
                                {
                                    Index = m,
                                    Name  = ReadString(bReader)
                                });
                            }

                            currentSheet.Rows.Add(row);
                        }

                        int columns = bReader.ReadInt32();
                        for (int n = 0; n < columns; n++)
                        {
                            currentSheet.Unknown1.Add(new Bexcel.Unknown
                            {
                                Index  = n,
                                Text   = ReadString(bReader),
                                Number = bReader.ReadInt32()
                            });
                        }

                        int rowCount2 = bReader.ReadInt32();
                        for (int num8 = 0; num8 < rowCount2; num8++)
                        {
                            currentSheet.TableDetails.Add(new Bexcel.Unknown
                            {
                                Index  = num8,
                                Text   = ReadString(bReader),
                                Number = bReader.ReadInt32()
                            });
                        }
                    }

                    bexcel.FileEnding = ReadString(bReader);
                }

                return(bexcel);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"An error happened while reading Bexcel file. Is the file corrupted or not compatible?!\n\n{ex}");
            }

            return(null);
        }