为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

C#如何读取Excel表格数据并显示到GridView控件

2018-11-04 10页 doc 40KB 184阅读

用户头像

is_153723

暂无简介

举报
C#如何读取Excel表格数据并显示到GridView控件C#如何读取Excel表格数据并显示到GridView控件 2008/05/06 00:36 近日,有个项目,需要用 Web 形式将 Excel 表格中的数据导入到数据库中,为了简化问题的解决,现在先将表中数据导入到 GridView 控件上( 代码如下: protected void Button1_Click(object sender, EventArgs e) ...{ string filepath=FileUpload1.PostedFile.FileName; ReadExcel(filepath, ...
C#如何读取Excel表格数据并显示到GridView控件
C#如何读取Excel#格#数据并显示到GridView控件 2008/05/06 00:36 近日,有个项目,需要用 Web 形式将 Excel 表格中的数据导入到数据库中,为了简化问的解决,现在先将表中数据导入到 GridView 控件上( 代码如下: protected void Button1_Click(object sender, EventArgs e) ...{ string filepath=FileUpload1.PostedFile.FileName; ReadExcel(filepath, gdBom); } public void ReadExcel(string sExcelFile,GridView dgBom) ...{ DataTable ExcelTable; DataSet ds = new DataSet(); //Excel的连接 OleDbConnection objConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sExcelFile + ";" + "Extended Properties=Excel 8.0;"); objConn.Open(); DataTable schemaTable = objConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null); string tableName = schemaTable.Rows[0][2].ToString().Trim();// 获取 Excel 的表名,默认值是sheet1 string strSql = "select * from [" + tableName + "]"; OleDbCommand objCmd = new OleDbCommand(strSql, objConn); OleDbDataAdapter myData = new OleDbDataAdapter(strSql, objConn); myData.Fill(ds, tableName);//填充数据 dgBom.DataSource =ds; dgBom.DataBind(); objConn.Close(); ExcelTable = ds.Tables[tableName]; int iColums = ExcelTable.Columns.Count;//列数 int iRows = ExcelTable.Rows.Count;//行数 //定义二维数组存储 Excel 表中读取的数据 string[,] storedata = new string[iRows, iColums]; for(int i=0;ialert('您导入的表格不合格式~')"); } else ...{ //LoadDataToDataBase(storedata,excelBom)//该函数主要负责将 storedata 中有用的数据写入到数据库中,在此不是问题的关键省略 } } 运行效果如下图:选择,,,表的路径,点确定后 类别:c# | | 添加到搜藏 | 分享到i贴吧 | 浏览(1391) | 评论 (7) 上一篇:验证文件上传有效类型的正则表达... 下一篇:C#数值结果表(格式化字符串) 相关文章: • sun:将gridview控件中数据以指• 怎样将Gridview控件的内容导出 定... 为... 用户控件上的GridView如何导• 类型"GridView"的控件 必须放在... • 入,... 使用Gridview空间导出到ASP.net的GridView控件中的数• • Excel,Gr... 据... ASP.NET 2.0,C#----利用扩展GridView 控件—支持 • • GridView... Excel ... GridView 不使用数据源控件,导VB,C# GridView导出到• • 出... excel,data... 更多>> 使用C#读取Word表格数据 读取Word表格数据的方法 1//将读取Word表格封装与方法中。 2public string ReadWord(string fileName, int rowIndex, int colIndex) 3{ 4 ApplicationClass cls = null; 5 Document doc = null; 6 7 Table table = null; 8 object missing = Missing.Value; 9 10 object path = fileName; 11 cls = new ApplicationClass(); 12 13 try 14 { 15 doc = cls.Documents.Open 16 (ref path, ref missing, ref missing, ref missing, 17 ref missing, ref missing, ref missing, ref missing, 18 ref missing, ref missing, ref missing, ref missing, 19 ref missing, ref missing, ref missing, ref missing); 20 table = doc.Tables[1]; 21 string text = table.Cell(rowIndex, colIndex).Range.Text.ToString(); 22 text = text.Substring(0, text.Length - 2); //去除尾部的mark 23 return text; 24 } 25 catch (Exception ex) 26 { 27 28 return ex.Message; 29 } 30 finally 31 { 32 if (doc != null) 33 doc.Close(ref missing, ref missing, ref missing); 34 cls.Quit(ref missing, ref missing, ref missing); 35 } 36} 这个方法用于读取Word表格中某个单元格的数据。其中的参数分别为文件名(包括路径),行号,列号。 ============================================ 由于考虑到代码复用,我将代码写成了一个类。此外,通过审视代码可以发现,如果要多次读取同一文件中的不同的单元格数据会造成频繁的打开、关闭Word程序;因此,我将代码进行优化。在我做优化的时候突然想起来ADO.NET的SqlConnection和SqlCommand类。这两个类我常常用做数据库操作,一般用到的方法顺序都是:打开数据库连接,执行数据库查询,关闭数据库连接。我没有使用到两个类,我将这段代码封装于一个类中。使用Open、Close控制Word文档的打开和关闭,使用WordTableRead方法读取表格中的数据。这样对于读取多个单元格中的数据,每次只需要打开、关闭一次Word程序即可,大大的节约了资源的开销和节省了时间,提高的读取效率。此外,对于代码的优化还有可以读取指定表格中数据。下图显示了这个类的结构,代码及相应注释附在图的下方: class WordTableRead 2{ 3 private string fileName; 4 private ApplicationClass cls = null; 5 private Document doc = null; 6 private Table table = null; 7 private object missing = Missing.Value; 8 //Word是否处于打开状态 9 private bool openState; 10 11 12 /**//// 13 /// 自定义构造方法 14 /// 15 /// 包含路径的文件名 16 public WordTableRead(string fileName) 17 { 18 this.fileName = fileName; 19 } 20 21 /**//// 22 /// 打开Word文档 23 /// 24 public void Open() 25 { 26 object path = fileName; 27 cls = new ApplicationClass(); 28 try 29 { 30 doc = cls.Documents.Open 31 (ref path, ref missing, ref missing, ref missing, 32 ref missing, ref missing, ref missing, ref missing, 33 ref missing, ref missing, ref missing, ref missing, 34 ref missing, ref missing, ref missing, ref missing); 35 openState = true; 36 } 37 catch 38 { 39 openState = false; 40 } 41 } 42 43 /**//// 44 /// 返回指定单元格中的数据 45 /// 46 /// 表格号 47 /// 行号 48 /// 列号 49 /// 单元格中的数据 50 public string ReadWord(int tableIndex, int rowIndex, int colIndex) 51 { 52 //Give the value to the tow Int32 params. 53 54 try 55 { 56 if (openState == true) 57 { 58 table = doc.Tables[tableIndex]; 59 string text = table.Cell(rowIndex, colIndex).Range.Text.ToString(); 60 text = text.Substring(0, text.Length - 2); //去除尾部的mark 61 return text; 62 } 63 else 64 { 65 return ""; 66 } 67 } 68 catch 69 { 70 return "Error"; 71 } 72 } 73 74 /**//// 75 /// 关闭Word文档 76 /// 77 public void Close() 78 { 79 if (openState == true) 80 { 81 if (doc != null) 82 doc.Close(ref missing, ref missing, ref missing); 83 cls.Quit(ref missing, ref missing, ref missing); 84 } 85 } 86} 尽管如此,我还是认为这个类的设计仍然存在缺陷。每次测试这个类的时候,感觉数据读取的速度不是很令我满意;而且,这个类用于控制台应用程序的时候不会在屏幕上看到任何值,不明白应该如何改进代码。希望朋友们能够给我提供一些改进此类的建议。 本文来自CSDN博客,转载请标明出处: 来自:
/
本文档为【C#如何读取Excel表格数据并显示到GridView控件】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索