从Excel中导数据到DataGridView
最近项目中需要从Excel中导数据到DataGridView中,所以写了一个通用类,代码如下:
1
public class ExcelToDatableHelper
2
{
3
private static ExcelToDatableHelper instance = null;
4
private static Object locker = new Object();
5
6
public static ExcelToDatableHelper Instance
7
{
8
get
9
{
10
if (instance == null)
11
{
12
lock (locker)
13
{
14
if (instance == null)
15
{
16
instance = new ExcelToDatableHelper();
17
}
18
}
19
}
20
return instance;
21
}
22
}
23
24
/// <summary>
25
/// Reads the excel.
26
/// </summary>
27
/// <param name="strFileName">Name of the file.</param>
28
/// <param name="sheetName">Name of the sheet.</param>
29
/// <param name="colName">Name of the col.</param>
30
/// <returns></returns>
31
public DataSet ReadExcel(string strFileName, string sheetName, List<string> colName)
32
{
33
string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + strFileName + ";Extended Properties = Excel 8.0";
34
OleDbConnection oleConnection = new OleDbConnection(strConnection);
35
StringBuilder strColName = new StringBuilder("");
36
string myStrColName = "";
37
if (colName.Count > 0)
38
{
39
foreach (string str in colName)
40
{
41
strColName.Append(str);
42
strColName.Append(",");
43
}
44
myStrColName = strColName.ToString().Substring(0, strColName.Length - 1); // 去掉","
45
}
46
try
47
{
48
oleConnection.Open();
49
DataSet dsRead = new DataSet();
50
OleDbDataAdapter oleAdper = new OleDbDataAdapter(" SELECT " + myStrColName + " FROM [" + sheetName + "$]", oleConnection);
51
oleAdper.Fill(dsRead, "result");
52
return dsRead;
53
}
54
catch (Exception ex)
55
{
56
MessageBox.Show(ex.ToString());
57
return null;
58
}
59
finally
60
{
61
oleConnection.Close();
62
}
63
}
64
65
/// <summary>
66
/// Opens the file.
67
/// </summary>
68
/// <returns></returns>
69
public string OpenFile()
70
{
71
OpenFileDialog openFileDialog = new OpenFileDialog();
72
openFileDialog.InitialDirectory = Application.StartupPath;
73
//openFileDialog.Filter="文本文件|*.*|Excel文件|*.xls|C#文件|*.cs|所有文件|*.*";
74
openFileDialog.Filter = "Excel文件|*.xls";
75
openFileDialog.RestoreDirectory = true;
76
openFileDialog.Title = "打开文件";
77
openFileDialog.FilterIndex = 1;
78
if (openFileDialog.ShowDialog() == DialogResult.OK)
79
{
80
return (openFileDialog.FileName);
81
}
82
else return String.Empty;
83
}
84
}

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

Demo下载:/Files/wxj1020/ExcelToDatatable.rar
分类:
WinForm
标签:
datagridview
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述