Example #1
0
        public static bool ImportDatabase()
        {
            try
            {
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
                string         path            = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                openFileDialog1.InitialDirectory = path;
                openFileDialog1.Filter           = "Database Files (*.db)|*.db";
                openFileDialog1.Title            = "Select the location to import the database file.";
                openFileDialog1.DefaultExt       = "db";
                openFileDialog1.CheckFileExists  = true;

                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    if (openFileDialog1.FileName != "")
                    {
                        File.Copy(openFileDialog1.FileName, @"Databases/Stock.db", true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }

                openFileDialog1.Reset();
                return(true);
            }
            catch (IOException IOThrow)
            {
                MessageBox.Show(IOThrow.ToString());
                return(false);
            }
        }
Example #2
0
        public static bool ExportToExcel(DataGridView dataGridView)
        {
            try
            {
                Workbook  workbook  = new Workbook();
                Worksheet worksheet = new Worksheet("First Sheet");
                worksheet.Cells.ColumnWidth[0, 1] = 3000;

                List <DataGridViewColumn> listVisible = new List <DataGridViewColumn>();
                foreach (DataGridViewColumn col in dataGridView.Columns)
                {
                    if (col.Visible)
                    {
                        listVisible.Add(col);
                    }
                }

                for (int i = 0; i < listVisible.Count; i++)
                {
                    worksheet.Cells[0, i] = new Cell(listVisible[i].HeaderText);
                }

                int rowCount = 1;
                for (int i = 0; i < dataGridView.Rows.Count; i++)
                {
                    for (int j = 0; j < listVisible.Count; j++)
                    {
                        worksheet.Cells[i + 1, j] = new Cell(dataGridView.Rows[i].Cells[listVisible[j].Name].Value.ToString());
                        rowCount++;
                    }
                }

                while (rowCount++ < 150)
                {
                    worksheet.Cells[rowCount, 0] = new Cell("");
                }

                SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                string         path            = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                saveFileDialog1.InitialDirectory = path;
                saveFileDialog1.Filter           = "Excel Files (*.xls)|*.xls|All files (*.*)|*.*";
                saveFileDialog1.Title            = "Select the location to export the Excel file.";
                saveFileDialog1.DefaultExt       = "xls";
                saveFileDialog1.CheckPathExists  = true;
                saveFileDialog1.FileName         = "Report" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";

                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    if (saveFileDialog1.FileName != "")
                    {
                        workbook.Worksheets.Add(worksheet);
                        workbook.Save(saveFileDialog1.FileName);

                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }
            catch (IOException IOThrow)
            {
                MessageBox.Show(IOThrow.ToString());
                return(false);
            }
        }