-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormImportCarFromExcel.cs
More file actions
109 lines (102 loc) · 3.79 KB
/
FormImportCarFromExcel.cs
File metadata and controls
109 lines (102 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using ExcelDataReader;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Z.Dapper.Plus;
namespace beerus
{
public partial class FormImportCarFromExcel : Form
{
public FormImportCarFromExcel()
{
InitializeComponent();
}
DataTableCollection tableCollection;
private void btnBrowser_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog()
{
Filter = "Excel 97-2003 Workbook|*.xls| Excel Workbook|*.xlsx"
})
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
txtFileName.Text = openFileDialog.FileName;
using (var stream = File.Open(openFileDialog.FileName, FileMode.Open, FileAccess.Read))
{
using (IExcelDataReader reader = ExcelReaderFactory.CreateReader(stream))
{
DataSet result = reader.AsDataSet(new ExcelDataSetConfiguration()
{
ConfigureDataTable = (_) => new ExcelDataTableConfiguration() { UseHeaderRow = true },
});
tableCollection = result.Tables;
chooseSheet.Items.Clear();
foreach (DataTable table in tableCollection)
chooseSheet.Items.Add(table.TableName);
}
}
}
}
}
private void chooseSheet_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable data = tableCollection[chooseSheet.SelectedItem.ToString()];
if (data != null)
{
List<Car> carList = new List<Car>();
for (int i = 0; i < data.Rows.Count; i++)
{
Car car = new Car();
car.name = data.Rows[i]["name"].ToString();
car.brand = data.Rows[i]["brand"].ToString();
car.model = data.Rows[i]["model"].ToString();
car.car_type = data.Rows[i]["car_type"].ToString();
if (data.Rows[i]["price"] != DBNull.Value && decimal.TryParse(data.Rows[i]["price"].ToString(), out decimal price))
{
car.price = price;
}
else
{
car.price = 0.0M;
}
carList.Add(car);
}
carBindingSource2.DataSource = carList;
}
}
private void btnImport_Click(object sender, EventArgs e)
{
try
{
DapperPlusManager.Entity<Car>().Table("Car");
List<Car> carList = carBindingSource2.DataSource as List<Car>;
string access = db._Access;
if (carList != null)
{
db.cn.Open();
using (IDbConnection dbcon = new SqlConnection(access))
{
dbcon.BulkInsert(carList);
}
}
db.cn.Close();
MessageBox.Show("Success");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void FormImportCarFromExcel_Load(object sender, EventArgs e)
{
}
}
}