日期:2014-05-18 浏览次数:20868 次
/// <summary> /// 解析Excel,返回DataTable /// </summary> /// <param name="fileName"></param> /// <returns></returns> public static System.Data.DataTable ImpExcel(string fileName) { System.Data.DataTable dt = new System.Data.DataTable(); try { Microsoft.Office.Interop.Excel.Application app; Workbooks wbs; Worksheet ws; app = new Microsoft.Office.Interop.Excel.Application(); wbs = app.Workbooks; wbs.Add(fileName); ws = (Worksheet)app.Worksheets.get_Item(1); int a = ws.Rows.Count; int b = ws.Columns.Count; string name = ws.Name; for (int i = 1; i < 行数; i++) { DataRow dr = dt.NewRow(); for (int j = 1; j <= 列数; j++) { Range range = ws.get_Range(app.Cells[i, j], app.Cells[i, j]); range.Select(); dr[j - 1] = app.ActiveCell.Text.ToString(); } dt.Rows.Add(dr); KillProcess(app); return dt; } catch (Exception ex) { MessageBox.Show("数据绑定Excel失败! 失败原因:Excel格式不正确!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); return dt; } } 参考 [url=http://blog.csdn.net/happy09li/article/details/7431967][/url]