public void StartProcessing(NonterminalExpression arg)//Arguments: 1)name database 2)name new table 3)-more is data for table { Regex ob1 = new Regex(@"[A-Z||a-z]{1}[a-z||0-9]{0,12}.[int||txt||date]"); string[] args = arg.NoTInfo; for (int i = 2; i < args.Length - 1; i++) { if (ob1.IsMatch(args[i])) { ; } else { Console.WriteLine("Incorrect syntax, try again or call the helpme command"); return; } } Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); //Подклюаем Excel Excel.Workbook xlWorkBook; FileInfo fi = new FileInfo("D:\\" + arg.NoTInfo[0] + ".xlsx"); //Проверяем есть ли файл с таким же названием, если да то выведит ошибку, т.к. БД уже сществует if (fi.Exists) { Console.WriteLine("File is open, processing..."); xlWorkBook = xlApp.Workbooks.Open(@"D:\" + arg.NoTInfo[0] + ".xlsx"); } else { Console.WriteLine("DataBase {0} is not, try more...", arg.NoTInfo[0]); ClearExcel.Clear_all(); return; } var xlSheets = xlWorkBook.Sheets as Excel.Sheets; var xlNewSheet = (Excel.Worksheet)xlSheets.Add(xlSheets[xlSheets.Count], Type.Missing, Type.Missing, Type.Missing);//Создаем новый лист // xlSheets[xlSheets.Count - 1].Visible = false; // string ValueForCells = ""; for (int i = 0; i < args.Length - 2; i++) { xlNewSheet.Cells[1, i + 1] = args[i + 2]; // xlNewSheet.Cells[1,i+1]=TableInfo[i]; } xlNewSheet.Name = Convert.ToString(arg.NoTInfo[1]);//указываем имя книги //xlNewSheet.Visible = true; //xlNewSheet.Range["A0"].TextToColumns("jhk"); xlWorkBook.ReadOnlyRecommended = false;//выключаем защиту документа xlWorkBook.Save(); xlWorkBook.Close(); xlApp.Quit(); ClearExcel.Clear_all(); Console.WriteLine("Making table is end"); }
static void Main(string[] args) { log.Trace("Start of the work(making objects for work, processing of request)"); //работаем с аргументами в интерпретаторе var context = new Context(args); var list = new List <AbstractExpression>(); TerminalExpression ob1 = new TerminalExpression(); NonterminalExpression ob2 = new NonterminalExpression(); list.Add(ob1); list.Add(ob2); foreach (AbstractExpression exp in list) { exp.Interpret(context); }//terminal exp. have key for collection with objects commands //Nonterminal expression have arguments for processing data in this command FuncCreateDB createDB = new FuncCreateDB();//make object CreateTable createT = new CreateTable(); FuncSelect select = new FuncSelect(); Update Upd = new Update(); Delete Del = new Delete(); RequestInfo req = new RequestInfo(); Dictionary <string, ICommand> ListCommand = new Dictionary <string, ICommand>();//make collection ListCommand.Add("helpme", new OutInfo()); ListCommand.Add("CreateDB", new FuncCreateDBCommand(createDB, ob2));//distribution him in collection and add key for him ListCommand.Add("CreateTable", new CreateTableCommand(createT, ob2)); ListCommand.Add("Select", new FuncSelectCommand(select, ob2)); ListCommand.Add("Update", new UpdateCommand(Upd, ob2)); ListCommand.Add("Delete", new DeleteCommand(Del, ob2)); ListCommand.Add("RequestInfo", new RequestInfoCommand(req)); Set set = new Set(); try { if (ListCommand.ContainsKey(ob1.TerInfo) == false) { throw new Exception("This command doesn't exist!"); } log.Trace("The key is found, receiving the object"); set.SetCommand(ListCommand[ob1.TerInfo]); } catch (Exception e) { Console.WriteLine(e.Message + "\nUse command \"helpme\""); log.Trace("This command doesn't exist!Send message of expression and closure of the programme"); return; } set.Processing(); log.Trace("Call clean-up function"); ClearExcel.Clear_all(); log.Trace("Good job! Closure of the application"); Console.ReadKey(); }
static void Main(string[] args) { //работаем с аргументами в интерпретаторе var context = new Context(args); var list = new List <AbstractExpression>(); TerminalExpression ob1 = new TerminalExpression(); NonterminalExpression ob2 = new NonterminalExpression(); list.Add(ob1); list.Add(ob2); foreach (AbstractExpression exp in list) { exp.Interpret(context); }//terminal exp. have key for collection with objects commands //Nonterminal expression have arguments for processing data in this command FuncCreateDB createDB = new FuncCreateDB();//make object CreateTable createT = new CreateTable(); FuncSelect select = new FuncSelect(); Update Upd = new Update(); Delete Del = new Delete(); Request req = new Request(); Dictionary <string, ICommand> ListCommand = new Dictionary <string, ICommand>();//make collection ListCommand.Add("helpme", new OutInfo()); ListCommand.Add("CreateDB", new FuncCreateDBCommand(createDB, ob2));//distribution him in collection and add key for him ListCommand.Add("CreateTable", new CreateTableCommand(createT, ob2)); ListCommand.Add("Select", new FuncSelectCommand(select, ob2)); ListCommand.Add("Update", new UpdateCommand(Upd, ob2)); ListCommand.Add("Delete", new DeleteCommand(Del, ob2)); ListCommand.Add("RequestInfo", new RequestCommand(req)); Set set = new Set(); set.SetCommand(ListCommand[ob1.TerInfo]); set.Processing(); ClearExcel.Clear_all(); Console.ReadKey(); }
public void StartProcessing(NonterminalExpression arg) { Console.WriteLine("Start creating file for DB"); //действия для функции оздания базы данных Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();//Подклюаем Excel Excel.Workbook xlWorkBook; FileInfo fi = new FileInfo("D:\\" + arg.NoTInfo[0] + ".xlsx");//Проверяем есть ли файл с таким же названием, если да то выведит ошибку, т.к. БД уже сществует if (fi.Exists) { Console.WriteLine("The name is occupied by another database!Try more... Or take select function for change Table in DB"); return; } else { xlWorkBook = xlApp.Workbooks.Add(); Console.WriteLine("Database creation..."); } xlWorkBook.SaveAs(@"D:\" + arg.NoTInfo[0] + ".xlsx");//сохраняем файл как... ClearExcel.Clear_all(); Console.WriteLine("Ready, you can continue self job (for list of command enter \"helpme\")"); }
public void StartProcessing(NonterminalExpression argm) { string[] arg = argm.NoTInfo; if (arg.Length == 2) { Console.WriteLine("Delete database..."); try { File.Delete(@"D:\" + argm.NoTInfo[0] + ".xlsx"); } catch (IOException e) { Console.WriteLine(e.Message); return; } } else if (arg.Length == 3) { Console.WriteLine("Delete table..."); try { Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); Excel.Workbook xlWorkBook; xlApp.Visible = true; xlWorkBook = xlApp.Workbooks.Open(@"D:\" + argm.NoTInfo[0] + ".xlsx"); Excel.Worksheet ObjWorkSheet = (Excel.Worksheet)xlWorkBook.Sheets[arg[1]]; var lastCell = ObjWorkSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell); int temp = lastCell.Row; for (int j = 1, i = 0; i != temp; i++)//clearing table { Console.WriteLine(j + "||" + lastCell.Row); ObjWorkSheet.Rows[j].Delete(); } xlWorkBook.Save();//Table is Clear xlWorkBook = xlApp.Workbooks.Open(@"D:\" + argm.NoTInfo[0] + ".xlsx"); var sheet = (Excel.Worksheet)xlWorkBook.Sheets[arg[1]];//delete clear table sheet.Delete(); xlWorkBook.Save(); } catch (Exception e) { Console.WriteLine(e.Message); Console.ReadKey(); return; } } else if (arg.Length == 4) { Console.WriteLine("Delete row..."); Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); Excel.Workbook xlWorkBook; try { xlWorkBook = xlApp.Workbooks.Open(@"D:\" + argm.NoTInfo[0] + ".xlsx"); } catch (Exception e) { Console.WriteLine(e.Message); return; } Regex ob2 = new Regex(@"[id].[0-9]{0,255}"); Regex ob1 = new Regex(@"[column].[A-Z||a-z||0-9]{0,12}.[A-Z||a-z||0-9]{0,12}"); string[] args = argm.NoTInfo; for (int i = 1; i < args.Length - 1; i++) { if (ob1.IsMatch(args[i])) { ; } else if (ob2.IsMatch(args[2])) { ; } else { Console.WriteLine("Incorrect syntax, try again or call the helpme command"); return; } } Excel.Worksheet ObjWorkSheet = (Excel.Worksheet)xlWorkBook.Sheets[Convert.ToString(arg[1])]; var xlSheets = xlWorkBook.Sheets as Excel.Sheets; var lastCell = ObjWorkSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell); if (ob1.IsMatch(arg[2])) { string temp = arg[2], NameColumn = ""; string type = ""; type = temp.Remove(temp.IndexOf(".")); Console.WriteLine(type); if (type == "column") { Console.WriteLine("Processing..." + temp); temp = temp.Remove(0, temp.IndexOf(".") + 1); NameColumn = temp.Remove(temp.IndexOf(".")); temp = temp.Remove(0, temp.IndexOf(".") + 1); // string temp3 = temp.Remove(0, 1); for (int i = 1; i < lastCell.Column; i++) { // Console.WriteLine(ObjWorkSheet.Cells[1, i].Text.ToString() + "||" + temp + ".txt" + "||" + NameColumn + ".txt" + "||" + temp3 + ".txt"); if (ObjWorkSheet.Cells[1, i].Text.ToString() == NameColumn + ".txt") { Console.WriteLine("Найден столбец, ищем строку " + temp); for (int j = 2; j <= lastCell.Row; j++) { Console.WriteLine(ObjWorkSheet.Cells[j, i].Text.ToString()); if (ObjWorkSheet.Cells[j, i].Text.ToString() == temp) { Console.WriteLine("Найден строка"); ObjWorkSheet.Rows[j].Delete(); Console.WriteLine("Удаление строки произошло"); break; } } break; } } } } if (ob2.IsMatch(arg[2])) { string RowId = arg[2]; RowId = RowId.Remove(0, RowId.IndexOf(".") + 1); ObjWorkSheet.Rows[RowId + 1].Delete(); } xlWorkBook.Save(); xlWorkBook.Close(); ClearExcel.Clear_all(); Console.WriteLine("End..."); } }
public void StartProcessing(NonterminalExpression arg) { string[] arguments = arg.NoTInfo; Regex ob1 = new Regex(@"[A-Z||a-z]{1}[a-z||0-9]{0,12}"); string[] args = arg.NoTInfo; for (int i = 1; i < args.Length - 1; i++) { if (ob1.IsMatch(args[i])) { ; } else { Console.WriteLine("Incorrect syntax, try again or call the helpme command"); return; } } Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();//Подклюаем Excel Excel.Workbook xlWorkBook; FileInfo fi = new FileInfo("D:\\" + arg.NoTInfo[0] + ".xlsx");//Проверяем есть ли файл с таким же названием, если да то выведит ошибку, т.к. БД уже сществует if (fi.Exists) { xlWorkBook = xlApp.Workbooks.Open(@"D:\" + arg.NoTInfo[0] + ".xlsx"); } else { Console.WriteLine("File is not!"); return; } var xlSheets = xlWorkBook.Sheets as Excel.Sheets; if (arguments.Length == 2) { string[] result; result = new string[xlWorkBook.Sheets.Count]; try { var sheet = (Excel.Worksheet)xlWorkBook.Sheets["Лист1"];//delete default sheet sheet.Delete(); } catch (Exception e) { } try { for (int i = 0; i < xlWorkBook.Sheets.Count; i++) { Console.WriteLine((i + 1) + ". " + ((Excel.Worksheet)xlWorkBook.Sheets[i + 1]).Name);//out list of names tables xlWorkBook.Save(); // ClearExcel.Clear_all(); } } catch (Exception e) { } } else { if (arguments.Length == 3) { Console.WriteLine(Convert.ToString(arguments[1])); Excel.Worksheet ObjWorkSheet = (Excel.Worksheet)xlWorkBook.Sheets[Convert.ToString(arguments[1])]; var lastCell = ObjWorkSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell); //1 ячейку string[,] list = new string[lastCell.Column, lastCell.Row]; // массив значений с листа равен по размеру листу for (int i = 0; i < lastCell.Column; i++) //по всем колонкам { for (int j = 0; j < lastCell.Row; j++) // по всем строкам { list[i, j] = ObjWorkSheet.Cells[j + 1, i + 1].Text.ToString(); //считываем текст в строку } } for (int i = 0; i < lastCell.Row; i++) { for (int j = 0; j < lastCell.Column; j++) { if (list[j, i].IndexOf(".") != -1) { list[j, i] = list[j, i].Remove(list[j, i].IndexOf(".")); } Console.Write("{0,-10}|", list[j, i]); } Console.WriteLine(); } } } xlWorkBook.Close(false, Type.Missing, Type.Missing); //закрыть не сохраняя ClearExcel.Clear_all(); }