Пример #1
0
        private void export(string tableName)
        {
            using (SQLiteConnection connection = createConnection())
            {
                List <string> columns = fetchColumns(connection, tableName);

                string appFolder        = Path.GetDirectoryName(Application.ExecutablePath);
                string templateFileName = Path.Combine(appFolder, string.Format(@"resources\{0}.xlsx", tableName));
                string exportFileName   = string.Format("{0}.xlsx", tableName);

                if (File.Exists(templateFileName))
                {
                    using (ExcelHelper helper = new ExcelHelper(templateFileName, exportFileName))
                    {
                        helper.Direction        = ExcelHelper.DirectionType.TOP_TO_DOWN;
                        helper.CurrentSheetName = "Sheet1";

                        helper.InsertRange("header");
                        CellRangeTemplate rowTemplate = helper.CreateCellRangeTemplate("row", columns);

                        helper.InsertRange(rowTemplate, fetchData(connection, tableName, columns));

                        helper.DeleteSheet("Templates");
                    }

                    MessageBox.Show("Exported!");
                }
                else
                {
                    MessageBox.Show(String.Format("Template xlsx not found ('{0}')!\nAutocreating it, please check it and retry!", templateFileName));
                    string basicTempateFileName = Path.Combine(appFolder, @"resources\BasicTemplate.xlsx");
                    using (ExcelHelper helper = new ExcelHelper(basicTempateFileName, templateFileName))
                    {
                        helper.Direction        = ExcelHelper.DirectionType.LEFT_TO_RIGHT;
                        helper.CurrentSheetName = "Templates";

                        CellRangeTemplate template = helper.CreateCellRangeTemplate("header_row", new List <string>()
                        {
                            "header", "row"
                        });

                        var result =
                            from item in columns
                            select new List <object>()
                        {
                            item, string.Format("<{0}>", item)
                        };

                        helper.InsertRange(template, result);

                        var names = new string[] { "header", "row" };
                        foreach (string name in names)
                        {
                            CellRangeRef range = helper.FindDefinedNameRange(name);

                            //extending it to the length of the columns
                            CellRef end = range.End;
                            end.OffsetIt(new CellRef(0, columns.Count - 1));
                            range.End       = end;
                            range.SheetName = "Templates";

                            helper.SetDefinedNameRange(name, range);
                        }

                        helper.DeleteSheet("_Templates");
                    }
                }
            }
        }