Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //КЛАСС ФОРМЫ
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using System.Windows.Forms;
- using System.Xml.Serialization;
- using System.Xml;
- using System.Data.OleDb;
- using System.IO.Compression;
- using Microsoft.VisualBasic;
- using Microsoft.Office.Interop.Excel;
- using iTextSharp.text;
- using System.Collections;
- using Word = Microsoft.Office.Interop.Word;
- using PdfSharp.Pdf;
- using PdfSharp.Drawing;
- namespace lab5_3sem
- {
- public partial class Form1 : Form
- {
- List<Student> students = new List<Student>();
- public Form1()
- {
- this.KeyPreview = true;
- thelist = new Students();
- Student Student1 = new Student { Name = "", Faculty = "", Course = "", Birthday = "" };
- InitializeComponent();
- }
- string[] FilePath;
- private void button1_Click(object sender, EventArgs e) //load
- {
- listBox1.Items.Clear();
- openFileDialog1.Multiselect = true;
- openFileDialog1.FileName = "";
- openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- FilePath = openFileDialog1.FileNames;
- var some = from t in FilePath select Path.GetFileNameWithoutExtension(t).ToString();
- listBox1.Items.AddRange(some.ToArray());
- }
- }
- private void toolStripMenuItem2_Click(object sender, EventArgs e) //нечетные строки, 1a
- {
- SaveFileDialog saveFileDialog1 = new SaveFileDialog();
- saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
- saveFileDialog1.RestoreDirectory = true;
- if (saveFileDialog1.ShowDialog() == DialogResult.OK && listBox1.SelectedIndex >= 0)
- {
- string[] page = File.ReadAllLines(FilePath[listBox1.SelectedIndex], Encoding.GetEncoding(1251));
- string fin = "";
- //var tt = (from string t in listBox1.Items select t).Aggregate((str1, str2) => (str1 + Environment.NewLine + str2));
- //var s = (from string t in page where () / 2 == 0 select t);
- for (int i = 0; i < page.Count(); i += 2)
- fin += page[i] + Environment.NewLine;
- File.WriteAllText(saveFileDialog1.FileName, fin);
- //File.WriteAllText(saveFileDialog1.FileName, s.ToString());
- }
- }
- private void toolStripMenuItem3_Click(object sender, EventArgs e) //строки, в которых нет цифр, 1b
- {
- SaveFileDialog saveFileDialog1 = new SaveFileDialog();
- saveFileDialog1.Filter = "txt files (*.txt)|*.txt";
- saveFileDialog1.RestoreDirectory = true;
- string fin = "";
- if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- {
- bool no_num_in_lines = true;
- foreach (int index in listBox1.SelectedIndices)
- {
- string[] page = File.ReadAllLines(FilePath[index], Encoding.GetEncoding(1251));
- for (int i = 0; i < page.Count(); i++)
- {
- foreach (char item in page[i])
- if (char.IsNumber(item))
- {
- no_num_in_lines = false;
- break;
- }
- if (no_num_in_lines)
- fin += page[i] + Environment.NewLine;
- no_num_in_lines = true;
- }
- }
- saveFileDialog1.ShowDialog();
- string filename = saveFileDialog1.FileName;
- File.WriteAllText(filename, fin);
- }
- }
- private void toolStripMenuItem4_Click(object sender, EventArgs e) //все строки кроме пустых, 1с
- {
- SaveFileDialog saveFileDialog1 = new SaveFileDialog();
- saveFileDialog1.Filter = "txt files (*.txt)|*.txt";
- saveFileDialog1.RestoreDirectory = true;
- string fin = "";
- foreach (int index in listBox1.SelectedIndices)
- {
- string[] page = File.ReadAllLines(FilePath[index], Encoding.GetEncoding(1251));
- for (int i = 0; i < page.Count(); i++)
- if (page[i].Length != 0)
- fin += page[i] + Environment.NewLine;
- }
- saveFileDialog1.ShowDialog();
- string filename = saveFileDialog1.FileName;
- File.WriteAllText(filename, fin);
- }
- private void listBox1_SelectedIndexChanged(object sender, EventArgs e) //2b
- {
- if (listBox1.SelectedIndex >= 0)
- {
- string[] page = File.ReadAllLines(FilePath[listBox1.SelectedIndex], Encoding.GetEncoding(1251));
- int fin = 0;
- for (int i = 0; i < page.Count(); i += 2)
- fin += page[i].Length;
- listBox2.Items.Clear();
- //string[] a = File.ReadAllLines(FilePath[listBox1.SelectedIndex], Encoding.GetEncoding(1251));
- for (int i = 0; i < page.Count(); i++)
- listBox2.Items.Add(page[i]);
- label1.Text = string.Format("{0}\n{1}\n{2}", fin, listBox2.Items.Count, string.Format("{0}", listBox2.Items[listBox2.Items.Count - 1]).Length);
- }
- }
- private void строкиВОбратномПорядкеToolStripMenuItem_Click(object sender, EventArgs e)
- {
- SaveFileDialog saveFileDialog1 = new SaveFileDialog();
- saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
- saveFileDialog1.RestoreDirectory = true;
- if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- {
- string fin = "";
- foreach (string item in listBox2.Items)
- fin = item + Environment.NewLine + fin;
- saveFileDialog1.ShowDialog();
- string filename = saveFileDialog1.FileName;
- File.WriteAllText(filename, fin);
- }
- }
- private void сохранитьВыделенныеСтрокиToolStripMenuItem_Click(object sender, EventArgs e)
- {
- SaveFileDialog saveFileDialog1 = new SaveFileDialog();
- saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
- saveFileDialog1.RestoreDirectory = true;
- if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- {
- string fin = "";
- foreach (string item in listBox2.SelectedItems)
- fin += item + Environment.NewLine;
- saveFileDialog1.ShowDialog();
- string filename = saveFileDialog1.FileName;
- File.WriteAllText(filename, fin);
- }
- }
- private void button4_Click(object sender, EventArgs e) //3c, сохранение файла
- {
- listBox2.Sorted = true;
- SaveFileDialog saveFileDialog1 = new SaveFileDialog();
- saveFileDialog1.Filter = "txt files (*.txt)|*.txt";
- saveFileDialog1.RestoreDirectory = true;
- if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- {
- string fin = "";
- foreach (string item in listBox2.Items)
- fin += item + Environment.NewLine;
- saveFileDialog1.ShowDialog();
- string filename = saveFileDialog1.FileName;
- File.WriteAllText(filename, fin);
- }
- }
- ////////////////////////////////////////// 3 num //////////////////////
- private Students thelist; //см инициализацию в Form1
- bool IsXml;
- bool priority;
- private void button3_Click(object sender, EventArgs e)//открытие txt/xml файла для 2 задания
- {
- listBox3.Items.Clear();
- thelist.Thelist.Clear();
- openFileDialog1.Multiselect = true;
- openFileDialog1.FileName = "";
- openFileDialog1.Filter = "txt files (*.txt)|*.txt|xml files (*xml)|*.xml";
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- string h = openFileDialog1.FileName;
- if (Path.GetExtension(openFileDialog1.FileName) == ".txt")
- {
- IsXml = false;
- string text = File.ReadAllText(openFileDialog1.FileName, Encoding.GetEncoding(1251));
- string[] lines = text.Split('\n');
- foreach (string line in lines)
- {
- string[] words = line.Split(' ');
- if (words.Length == 7)
- {
- string fin = words[0] + ' ' + words[1] + ' ' + words[2];
- listBox3.Items.Add(fin);
- thelist.Add(fin, words[3], words[4], words[5], words[6]);
- }
- }
- }
- else
- {
- IsXml = true;
- using (FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.OpenOrCreate))
- {
- XmlSerializer formatter;
- formatter = new XmlSerializer(typeof(Students));
- thelist = (Students)formatter.Deserialize(fs);
- //var grouped = thelist.Thelist.GroupBy(worker => worker.Name).Select(group => new Student { Name = group.Key });
- //listBox3.Items.AddRange(grouped.ToArray());
- var som = from t in thelist.Thelist select t.Name.ToString();
- listBox3.Items.AddRange(som.ToArray<string>());
- }
- }
- }
- }
- private void listBox3_SelectedIndexChanged(object sender, EventArgs e) //вывод инфы из листбокса3
- {
- if (listBox3.SelectedIndex != -1)
- {
- priority = true;
- textBox1.Text = thelist.Thelist[listBox3.SelectedIndex].Name;
- textBox2.Text = thelist.Thelist[listBox3.SelectedIndex].Faculty;
- textBox3.Text = thelist.Thelist[listBox3.SelectedIndex].Course;
- textBox4.Text = thelist.Thelist[listBox3.SelectedIndex].Birthday;
- textBox5.Text = thelist.Thelist[listBox3.SelectedIndex].Birthplace;
- priority = false;
- }
- }
- private void SaveFile(bool IsXml, string Path) //сохранение файла после изменения
- {
- File.Delete(openFileDialog1.FileName);
- if (!IsXml)
- {
- foreach (Student stud in thelist.Thelist)
- File.AppendAllText(openFileDialog1.FileName, stud.Name + " " + stud.Faculty + " " + stud.Course + " " + stud.Birthday + " " + stud.Birthplace + Environment.NewLine, Encoding.GetEncoding(1251));
- }
- else
- {
- using (FileStream fs = new FileStream(Path, FileMode.OpenOrCreate))
- {
- XmlSerializer formatter;
- formatter = new XmlSerializer(typeof(Students));
- formatter.Serialize(fs, thelist);
- }
- }
- }
- private void textBox1_TextChanged(object sender, EventArgs e) // редактирование файла
- {
- if (listBox3.SelectedIndex != -1 && priority == false)
- {
- string h = openFileDialog1.FileName;
- h = h.Substring(h.LastIndexOf('.') + 1); // Получаем расширение файла
- if (textBox1.Text != "") thelist.Thelist[listBox3.SelectedIndex].Name = textBox1.Text;
- if (textBox2.Text != "") thelist.Thelist[listBox3.SelectedIndex].Faculty = textBox2.Text;
- if (textBox3.Text != "") thelist.Thelist[listBox3.SelectedIndex].Course = textBox3.Text;
- if (textBox4.Text != "") thelist.Thelist[listBox3.SelectedIndex].Birthday = textBox4.Text;
- if (textBox5.Text != "") thelist.Thelist[listBox3.SelectedIndex].Birthplace = textBox5.Text;
- SaveFile(IsXml, openFileDialog1.FileName);
- }
- }
- private void deleting(object sender, EventArgs e)
- {
- thelist.Thelist.RemoveAt(listBox3.SelectedIndex);
- listBox3.Items.RemoveAt(listBox3.SelectedIndex);
- }
- private void Create(object sender, EventArgs e)
- {
- listBox3.Items.Add(textBox6.Text);
- thelist.Thelist.Add(new Student(textBox6.Text, "факультет", "курс", "дата рождения", "место рождения"));
- }
- private void SaveAsDoc(object sender, EventArgs e)
- {
- SaveFileDialog saveFileDialog1 = new SaveFileDialog();
- saveFileDialog1.OverwritePrompt = true;
- saveFileDialog1.DefaultExt = ".doc";
- saveFileDialog1.Filter = "Word 2007 Documents (*.doc)|*.doc";
- if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- File.WriteAllText(saveFileDialog1.FileName, thelist.Thelist[listBox3.SelectedIndex].Name+' ' + thelist.Thelist[listBox3.SelectedIndex].Faculty + ' ' + thelist.Thelist[listBox3.SelectedIndex].Course + ' ' + thelist.Thelist[listBox3.SelectedIndex].Birthday + ' ' + thelist.Thelist[listBox3.SelectedIndex].Birthplace);
- //File.WriteAllText(saveFileDialog1.FileName, thelist.ToString());
- }
- private void SaveAsPdf(object sender, EventArgs e)
- {
- SaveFileDialog saveFileDialog1 = new SaveFileDialog();
- saveFileDialog1.OverwritePrompt = true;
- saveFileDialog1.DefaultExt = ".pdf";
- saveFileDialog1.Filter = "PDF Files(*.pdf)| *.pdf";
- if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- {
- XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);
- PdfDocument pdf = new PdfDocument();
- PdfPage pdfPage = pdf.AddPage();
- XGraphics graph = XGraphics.FromPdfPage(pdfPage);
- XFont xFont = new XFont("Times New Roman", 9.25, XFontStyle.Bold, options);
- graph.DrawString(thelist.Thelist[listBox3.SelectedIndex].ToString(), xFont, XBrushes.Black, new XRect(12, 10, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
- //for (int i = 0; i < thelist.Thelist.Count; i++)
- // graph.DrawString(thelist.Thelist[i].ToString(), xFont, XBrushes.Black, new XRect(12, i * 10, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
- pdf.Save(saveFileDialog1.FileName);
- pdf.Close();
- }
- }
- private void SaveAsXls(object sender, EventArgs e)
- {
- SaveFileDialog saveFileDialog1 = new SaveFileDialog();
- saveFileDialog1.OverwritePrompt = true;
- saveFileDialog1.DefaultExt = ".xls";
- saveFileDialog1.Filter = "Excel Files(*.xlsx)|*.xls";
- Microsoft.Office.Interop.Excel.Application exelic = new Microsoft.Office.Interop.Excel.Application();
- exelic.Workbooks.Add();
- Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)exelic.ActiveSheet;
- worksheet.Cells[1, "A"] = "ФИО";
- worksheet.Cells[1, "B"] = "Курс";
- worksheet.Cells[1, "C"] = "Факультет";
- worksheet.Cells[1, "D"] = "Дата рождения";
- worksheet.Cells[1, "E"] = "Место рождения";
- if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- {
- //int n = 0;
- worksheet.Cells[2, "A"] = thelist.Thelist[listBox3.SelectedIndex].Name;
- worksheet.Cells[2, "B"] = thelist.Thelist[listBox3.SelectedIndex].Faculty;
- worksheet.Cells[2, "C"] = thelist.Thelist[listBox3.SelectedIndex].Course;
- worksheet.Cells[2, "D"] = thelist.Thelist[listBox3.SelectedIndex].Birthday;
- worksheet.Cells[2, "E"] = thelist.Thelist[listBox3.SelectedIndex].Birthplace;
- //Microsoft.Office.Interop.Excel.Range wow = worksheet.Range[worksheet.Cells[1, "A"], worksheet.Cells[n, "A"]];
- Microsoft.Office.Interop.Excel.Range wow = worksheet.Range[worksheet.Cells[1, "A"], worksheet.Cells[1, "A"]];
- wow.ColumnWidth = 30;
- //wow= worksheet.Range[worksheet.Cells[1, "B"], worksheet.Cells[n, "E"]];
- wow = worksheet.Range[worksheet.Cells[1, "B"], worksheet.Cells[1, "E"]];
- wow.ColumnWidth = 20;
- worksheet.SaveAs(saveFileDialog1.FileName);
- exelic.Quit();
- }
- }
- }
- }
- //КЛАСС Students
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using System.Windows.Forms;
- using System.Xml.Serialization;
- using System.Xml;
- using System.Data.OleDb;
- using System.IO.Compression;
- using Microsoft.VisualBasic;
- using Microsoft.Office.Interop.Excel;
- using iTextSharp.text;
- using System.Collections;
- using Word = Microsoft.Office.Interop.Word;
- using PdfSharp.Pdf;
- using PdfSharp.Drawing;
- namespace lab5_3sem
- {
- public partial class Form1 : Form
- {
- List<Student> students = new List<Student>();
- public Form1()
- {
- this.KeyPreview = true;
- thelist = new Students();
- Student Student1 = new Student { Name = "", Faculty = "", Course = "", Birthday = "" };
- InitializeComponent();
- }
- string[] FilePath;
- private void button1_Click(object sender, EventArgs e) //load
- {
- listBox1.Items.Clear();
- openFileDialog1.Multiselect = true;
- openFileDialog1.FileName = "";
- openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- FilePath = openFileDialog1.FileNames;
- var some = from t in FilePath select Path.GetFileNameWithoutExtension(t).ToString();
- listBox1.Items.AddRange(some.ToArray());
- }
- }
- private void toolStripMenuItem2_Click(object sender, EventArgs e) //нечетные строки, 1a
- {
- SaveFileDialog saveFileDialog1 = new SaveFileDialog();
- saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
- saveFileDialog1.RestoreDirectory = true;
- if (saveFileDialog1.ShowDialog() == DialogResult.OK && listBox1.SelectedIndex >= 0)
- {
- string[] page = File.ReadAllLines(FilePath[listBox1.SelectedIndex], Encoding.GetEncoding(1251));
- string fin = "";
- //var tt = (from string t in listBox1.Items select t).Aggregate((str1, str2) => (str1 + Environment.NewLine + str2));
- //var s = (from string t in page where () / 2 == 0 select t);
- for (int i = 0; i < page.Count(); i += 2)
- fin += page[i] + Environment.NewLine;
- File.WriteAllText(saveFileDialog1.FileName, fin);
- //File.WriteAllText(saveFileDialog1.FileName, s.ToString());
- }
- }
- private void toolStripMenuItem3_Click(object sender, EventArgs e) //строки, в которых нет цифр, 1b
- {
- SaveFileDialog saveFileDialog1 = new SaveFileDialog();
- saveFileDialog1.Filter = "txt files (*.txt)|*.txt";
- saveFileDialog1.RestoreDirectory = true;
- string fin = "";
- if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- {
- bool no_num_in_lines = true;
- foreach (int index in listBox1.SelectedIndices)
- {
- string[] page = File.ReadAllLines(FilePath[index], Encoding.GetEncoding(1251));
- for (int i = 0; i < page.Count(); i++)
- {
- foreach (char item in page[i])
- if (char.IsNumber(item))
- {
- no_num_in_lines = false;
- break;
- }
- if (no_num_in_lines)
- fin += page[i] + Environment.NewLine;
- no_num_in_lines = true;
- }
- }
- saveFileDialog1.ShowDialog();
- string filename = saveFileDialog1.FileName;
- File.WriteAllText(filename, fin);
- }
- }
- private void toolStripMenuItem4_Click(object sender, EventArgs e) //все строки кроме пустых, 1с
- {
- SaveFileDialog saveFileDialog1 = new SaveFileDialog();
- saveFileDialog1.Filter = "txt files (*.txt)|*.txt";
- saveFileDialog1.RestoreDirectory = true;
- string fin = "";
- foreach (int index in listBox1.SelectedIndices)
- {
- string[] page = File.ReadAllLines(FilePath[index], Encoding.GetEncoding(1251));
- for (int i = 0; i < page.Count(); i++)
- if (page[i].Length != 0)
- fin += page[i] + Environment.NewLine;
- }
- saveFileDialog1.ShowDialog();
- string filename = saveFileDialog1.FileName;
- File.WriteAllText(filename, fin);
- }
- private void listBox1_SelectedIndexChanged(object sender, EventArgs e) //2b
- {
- if (listBox1.SelectedIndex >= 0)
- {
- string[] page = File.ReadAllLines(FilePath[listBox1.SelectedIndex], Encoding.GetEncoding(1251));
- int fin = 0;
- for (int i = 0; i < page.Count(); i += 2)
- fin += page[i].Length;
- listBox2.Items.Clear();
- //string[] a = File.ReadAllLines(FilePath[listBox1.SelectedIndex], Encoding.GetEncoding(1251));
- for (int i = 0; i < page.Count(); i++)
- listBox2.Items.Add(page[i]);
- label1.Text = string.Format("{0}\n{1}\n{2}", fin, listBox2.Items.Count, string.Format("{0}", listBox2.Items[listBox2.Items.Count - 1]).Length);
- }
- }
- private void строкиВОбратномПорядкеToolStripMenuItem_Click(object sender, EventArgs e)
- {
- SaveFileDialog saveFileDialog1 = new SaveFileDialog();
- saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
- saveFileDialog1.RestoreDirectory = true;
- if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- {
- string fin = "";
- foreach (string item in listBox2.Items)
- fin = item + Environment.NewLine + fin;
- saveFileDialog1.ShowDialog();
- string filename = saveFileDialog1.FileName;
- File.WriteAllText(filename, fin);
- }
- }
- private void сохранитьВыделенныеСтрокиToolStripMenuItem_Click(object sender, EventArgs e)
- {
- SaveFileDialog saveFileDialog1 = new SaveFileDialog();
- saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
- saveFileDialog1.RestoreDirectory = true;
- if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- {
- string fin = "";
- foreach (string item in listBox2.SelectedItems)
- fin += item + Environment.NewLine;
- saveFileDialog1.ShowDialog();
- string filename = saveFileDialog1.FileName;
- File.WriteAllText(filename, fin);
- }
- }
- private void button4_Click(object sender, EventArgs e) //3c, сохранение файла
- {
- listBox2.Sorted = true;
- SaveFileDialog saveFileDialog1 = new SaveFileDialog();
- saveFileDialog1.Filter = "txt files (*.txt)|*.txt";
- saveFileDialog1.RestoreDirectory = true;
- if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- {
- string fin = "";
- foreach (string item in listBox2.Items)
- fin += item + Environment.NewLine;
- saveFileDialog1.ShowDialog();
- string filename = saveFileDialog1.FileName;
- File.WriteAllText(filename, fin);
- }
- }
- ////////////////////////////////////////// 3 num //////////////////////
- private Students thelist; //см инициализацию в Form1
- bool IsXml;
- bool priority;
- private void button3_Click(object sender, EventArgs e)//открытие txt/xml файла для 2 задания
- {
- listBox3.Items.Clear();
- thelist.Thelist.Clear();
- openFileDialog1.Multiselect = true;
- openFileDialog1.FileName = "";
- openFileDialog1.Filter = "txt files (*.txt)|*.txt|xml files (*xml)|*.xml";
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- string h = openFileDialog1.FileName;
- if (Path.GetExtension(openFileDialog1.FileName) == ".txt")
- {
- IsXml = false;
- string text = File.ReadAllText(openFileDialog1.FileName, Encoding.GetEncoding(1251));
- string[] lines = text.Split('\n');
- foreach (string line in lines)
- {
- string[] words = line.Split(' ');
- if (words.Length == 7)
- {
- string fin = words[0] + ' ' + words[1] + ' ' + words[2];
- listBox3.Items.Add(fin);
- thelist.Add(fin, words[3], words[4], words[5], words[6]);
- }
- }
- }
- else
- {
- IsXml = true;
- using (FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.OpenOrCreate))
- {
- XmlSerializer formatter;
- formatter = new XmlSerializer(typeof(Students));
- thelist = (Students)formatter.Deserialize(fs);
- //var grouped = thelist.Thelist.GroupBy(worker => worker.Name).Select(group => new Student { Name = group.Key });
- //listBox3.Items.AddRange(grouped.ToArray());
- var som = from t in thelist.Thelist select t.Name.ToString();
- listBox3.Items.AddRange(som.ToArray<string>());
- }
- }
- }
- }
- private void listBox3_SelectedIndexChanged(object sender, EventArgs e) //вывод инфы из листбокса3
- {
- if (listBox3.SelectedIndex != -1)
- {
- priority = true;
- textBox1.Text = thelist.Thelist[listBox3.SelectedIndex].Name;
- textBox2.Text = thelist.Thelist[listBox3.SelectedIndex].Faculty;
- textBox3.Text = thelist.Thelist[listBox3.SelectedIndex].Course;
- textBox4.Text = thelist.Thelist[listBox3.SelectedIndex].Birthday;
- textBox5.Text = thelist.Thelist[listBox3.SelectedIndex].Birthplace;
- priority = false;
- }
- }
- private void SaveFile(bool IsXml, string Path) //сохранение файла после изменения
- {
- File.Delete(openFileDialog1.FileName);
- if (!IsXml)
- {
- foreach (Student stud in thelist.Thelist)
- File.AppendAllText(openFileDialog1.FileName, stud.Name + " " + stud.Faculty + " " + stud.Course + " " + stud.Birthday + " " + stud.Birthplace + Environment.NewLine, Encoding.GetEncoding(1251));
- }
- else
- {
- using (FileStream fs = new FileStream(Path, FileMode.OpenOrCreate))
- {
- XmlSerializer formatter;
- formatter = new XmlSerializer(typeof(Students));
- formatter.Serialize(fs, thelist);
- }
- }
- }
- private void textBox1_TextChanged(object sender, EventArgs e) // редактирование файла
- {
- if (listBox3.SelectedIndex != -1 && priority == false)
- {
- string h = openFileDialog1.FileName;
- h = h.Substring(h.LastIndexOf('.') + 1); // Получаем расширение файла
- if (textBox1.Text != "") thelist.Thelist[listBox3.SelectedIndex].Name = textBox1.Text;
- if (textBox2.Text != "") thelist.Thelist[listBox3.SelectedIndex].Faculty = textBox2.Text;
- if (textBox3.Text != "") thelist.Thelist[listBox3.SelectedIndex].Course = textBox3.Text;
- if (textBox4.Text != "") thelist.Thelist[listBox3.SelectedIndex].Birthday = textBox4.Text;
- if (textBox5.Text != "") thelist.Thelist[listBox3.SelectedIndex].Birthplace = textBox5.Text;
- SaveFile(IsXml, openFileDialog1.FileName);
- }
- }
- private void deleting(object sender, EventArgs e)
- {
- thelist.Thelist.RemoveAt(listBox3.SelectedIndex);
- listBox3.Items.RemoveAt(listBox3.SelectedIndex);
- }
- private void Create(object sender, EventArgs e)
- {
- listBox3.Items.Add(textBox6.Text);
- thelist.Thelist.Add(new Student(textBox6.Text, "факультет", "курс", "дата рождения", "место рождения"));
- }
- private void SaveAsDoc(object sender, EventArgs e)
- {
- SaveFileDialog saveFileDialog1 = new SaveFileDialog();
- saveFileDialog1.OverwritePrompt = true;
- saveFileDialog1.DefaultExt = ".doc";
- saveFileDialog1.Filter = "Word 2007 Documents (*.doc)|*.doc";
- if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- File.WriteAllText(saveFileDialog1.FileName, thelist.Thelist[listBox3.SelectedIndex].Name+' ' + thelist.Thelist[listBox3.SelectedIndex].Faculty + ' ' + thelist.Thelist[listBox3.SelectedIndex].Course + ' ' + thelist.Thelist[listBox3.SelectedIndex].Birthday + ' ' + thelist.Thelist[listBox3.SelectedIndex].Birthplace);
- //File.WriteAllText(saveFileDialog1.FileName, thelist.ToString());
- }
- private void SaveAsPdf(object sender, EventArgs e)
- {
- SaveFileDialog saveFileDialog1 = new SaveFileDialog();
- saveFileDialog1.OverwritePrompt = true;
- saveFileDialog1.DefaultExt = ".pdf";
- saveFileDialog1.Filter = "PDF Files(*.pdf)| *.pdf";
- if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- {
- XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);
- PdfDocument pdf = new PdfDocument();
- PdfPage pdfPage = pdf.AddPage();
- XGraphics graph = XGraphics.FromPdfPage(pdfPage);
- XFont xFont = new XFont("Times New Roman", 9.25, XFontStyle.Bold, options);
- graph.DrawString(thelist.Thelist[listBox3.SelectedIndex].ToString(), xFont, XBrushes.Black, new XRect(12, 10, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
- //for (int i = 0; i < thelist.Thelist.Count; i++)
- // graph.DrawString(thelist.Thelist[i].ToString(), xFont, XBrushes.Black, new XRect(12, i * 10, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
- pdf.Save(saveFileDialog1.FileName);
- pdf.Close();
- }
- }
- private void SaveAsXls(object sender, EventArgs e)
- {
- SaveFileDialog saveFileDialog1 = new SaveFileDialog();
- saveFileDialog1.OverwritePrompt = true;
- saveFileDialog1.DefaultExt = ".xls";
- saveFileDialog1.Filter = "Excel Files(*.xlsx)|*.xls";
- Microsoft.Office.Interop.Excel.Application exelic = new Microsoft.Office.Interop.Excel.Application();
- exelic.Workbooks.Add();
- Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)exelic.ActiveSheet;
- worksheet.Cells[1, "A"] = "ФИО";
- worksheet.Cells[1, "B"] = "Курс";
- worksheet.Cells[1, "C"] = "Факультет";
- worksheet.Cells[1, "D"] = "Дата рождения";
- worksheet.Cells[1, "E"] = "Место рождения";
- if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- {
- //int n = 0;
- worksheet.Cells[2, "A"] = thelist.Thelist[listBox3.SelectedIndex].Name;
- worksheet.Cells[2, "B"] = thelist.Thelist[listBox3.SelectedIndex].Faculty;
- worksheet.Cells[2, "C"] = thelist.Thelist[listBox3.SelectedIndex].Course;
- worksheet.Cells[2, "D"] = thelist.Thelist[listBox3.SelectedIndex].Birthday;
- worksheet.Cells[2, "E"] = thelist.Thelist[listBox3.SelectedIndex].Birthplace;
- //Microsoft.Office.Interop.Excel.Range wow = worksheet.Range[worksheet.Cells[1, "A"], worksheet.Cells[n, "A"]];
- Microsoft.Office.Interop.Excel.Range wow = worksheet.Range[worksheet.Cells[1, "A"], worksheet.Cells[1, "A"]];
- wow.ColumnWidth = 30;
- //wow= worksheet.Range[worksheet.Cells[1, "B"], worksheet.Cells[n, "E"]];
- wow = worksheet.Range[worksheet.Cells[1, "B"], worksheet.Cells[1, "E"]];
- wow.ColumnWidth = 20;
- worksheet.SaveAs(saveFileDialog1.FileName);
- exelic.Quit();
- }
- }
- }
- }
- //////////////////////////////////////
- //КЛАСС Students (контейнер для студентов)
- /////////////////////////////////////
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Xml;
- using System.Xml.Serialization;
- using System.IO.Compression;
- using Microsoft.VisualBasic;
- namespace lab5_3sem
- {
- public class Students
- {
- public List<Student> Thelist { get; set; }
- public Students()
- {
- Thelist = new List<Student>();
- }
- public System.Windows.Forms.ListBox.ObjectCollection oh(object sender)
- {
- System.Windows.Forms.ListBox.ObjectCollection tt = new System.Windows.Forms.ListBox.ObjectCollection((System.Windows.Forms.ListBox)sender);
- for (int i = 0; i < Thelist.Count; i++)
- {
- tt.Add(Thelist[i].Name);
- }
- return tt;
- }
- public void Addxml(List<Student> o)
- {
- Thelist = o;
- }
- public void Add(string n, string f, string c, string bday, string bplace)
- {
- Thelist.Add(new Student (n, f, c, bday, bplace));
- }
- }
- }
- ///////////////////////////
- КЛАСС Student
- //////////////////////////
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO;
- using System.Xml;
- using System.Xml.Serialization;
- namespace lab5_3sem
- {
- [Serializable]
- public class Student
- {
- public string Name { get; set; }
- public string Faculty { get; set; }
- public string Course { get; set; }
- public string Birthday { get; set; }
- public string Birthplace { get; set; }
- public Student()
- {
- }
- public Student(string n, string f, string c, string bday, string bplace)
- {
- Name = n;
- Faculty = f;
- Course = c;
- Birthday = bday;
- Birthplace = bplace;
- }
- public override string ToString()
- {
- string fin = "";
- fin += Name + ' ' + Faculty + ' ' + Course + ' ' + Birthday + ' ' + Birthplace + '\n';
- return fin;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement