标题: 从Excel中导数据到DataGridView
- 长空新雁 2008-04-28 10:12 阅读:253
- 评论:2 查看评论 | 添加评论

最近项目中需要从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[图片]    }

Demo下载:/Files/wxj1020/ExcelToDatatable.rar
查看评论 | 添加评论
返回顶部 | 返回首页