FrmClearDB.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Windows.Forms;
  10. namespace BooksManageSystem
  11. {
  12. public partial class FrmClearDB : Form
  13. {
  14. public FrmClearDB()
  15. {
  16. InitializeComponent();
  17. }
  18. private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  19. {
  20. if (textBox1.Text=="888888")
  21. {
  22. button1.Enabled = true;
  23. btn_Brower.Enabled = true;
  24. }
  25. else
  26. {
  27. button1.Enabled = false;
  28. btn_Brower.Enabled = false;
  29. }
  30. }
  31. private void button1_Click(object sender, EventArgs e)
  32. {
  33. if (new DBcon().ClearDataBase())
  34. {
  35. MessageBox.Show("数据库已经清空");
  36. }
  37. else
  38. {
  39. MessageBox.Show("清空数据库失败!");
  40. }
  41. }
  42. private void btn_bookListTemplate_Click(object sender, EventArgs e)
  43. {
  44. using (OfficeOpenXml.ExcelPackage ep = new OfficeOpenXml.ExcelPackage())
  45. {
  46. var sheet = ep.Workbook.Worksheets.Add("Books");
  47. sheet.Cells[1, 1].Value = "书名";
  48. sheet.Cells[1, 2].Value = "定价";
  49. sheet.Cells[1, 3].Value = "购入价";
  50. sheet.Cells[1, 4].Value = "数量";
  51. sheet.Cells[1, 5].Value = "学生用书";
  52. sheet.Cells[2, 1].Value = "示例数据书";
  53. sheet.Cells[2, 2].Value = 22.5;
  54. sheet.Cells[2, 3].Value = 18.3;
  55. sheet.Cells[2, 4].Value = 20;
  56. sheet.Cells[2, 5].Value = true;
  57. SaveFileDialog sfd = new SaveFileDialog();
  58. sfd.Filter = "Excel File(*.xlsx)|*.xlsx";
  59. if (sfd.ShowDialog()== System.Windows.Forms.DialogResult.OK)
  60. {
  61. ep.SaveAs(new FileInfo(sfd.FileName));
  62. }
  63. }
  64. }
  65. private void btn_Brower_Click(object sender, EventArgs e)
  66. {
  67. DBcon con = new DBcon();
  68. var ofd = new OpenFileDialog();
  69. ofd.Filter = "Excel File(*.xlsx)|*.xlsx";
  70. if (ofd.ShowDialog()== System.Windows.Forms.DialogResult.OK)
  71. {
  72. btn_Brower.Text = "请等待...";
  73. using (OfficeOpenXml.ExcelPackage ep = new OfficeOpenXml.ExcelPackage(new FileInfo(ofd.FileName)))
  74. {
  75. //int c = ep.Workbook.Worksheets.Count;
  76. //if (c<1)
  77. //{
  78. // return;
  79. //}
  80. var sheet= ep.Workbook.Worksheets[1];
  81. int rowEnd = sheet.Dimension.End.Row;
  82. for (int i = 2; i <= rowEnd; i++)
  83. {
  84. string bkname= sheet.GetValue(i, 1).ToString();
  85. double price = Convert.ToDouble(sheet.GetValue(i, 2));
  86. double ordPrice = Convert.ToDouble(sheet.GetValue(i, 3));
  87. int count = Convert.ToInt32(sheet.GetValue(i, 4));
  88. bool isStu = Convert.ToBoolean(sheet.GetValue(i, 5));
  89. con.AddNewBook(bkname, price, ordPrice, count, isStu);
  90. }
  91. }
  92. btn_Brower.Text = "导入完成";
  93. }
  94. }
  95. }
  96. }