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

上传人:rrsccc 文档编号:9093916 上传时间:2021-02-02 格式:DOC 页数:9 大小:66.50KB
返回 下载 相关 举报
C如何读取Excel表格数据并显示到GridView控.doc_第1页
第1页 / 共9页
C如何读取Excel表格数据并显示到GridView控.doc_第2页
第2页 / 共9页
C如何读取Excel表格数据并显示到GridView控.doc_第3页
第3页 / 共9页
C如何读取Excel表格数据并显示到GridView控.doc_第4页
第4页 / 共9页
C如何读取Excel表格数据并显示到GridView控.doc_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《C如何读取Excel表格数据并显示到GridView控.doc》由会员分享,可在线阅读,更多相关《C如何读取Excel表格数据并显示到GridView控.doc(9页珍藏版)》请在三一文库上搜索。

1、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,

2、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.Dat

3、a.OleDb.OleDbSchemaGuid.Tables, null); string tableName = schemaTable.Rows02.ToString().Trim();/获取 Excel 的表名,默认值是sheet1 string strSql = select * from + tableName + ; OleDbCommand objCmd = new OleDbCommand(strSql, objConn); OleDbDataAdapter myData = new OleDbDataAdapter(strSql, objConn); myData.Fill(

4、ds, tableName);/填充数据 dgBom.DataSource =ds; dgBom.DataBind(); objConn.Close(); ExcelTable = ds.TablestableName; int iColums = ExcelTable.Columns.Count;/列数 int iRows = ExcelTable.Rows.Count;/行数 /定义二维数组存储 Excel 表中读取的数据 string, storedata = new stringiRows, iColums; for(int i=0;iExcelTable.Rows.Count;i+)

5、 for (int j = 0; j ExcelTable.Columns.Count; j+) . /将Excel表中的数据存储到数组 storedatai, j = ExcelTable.Rowsij.ToString(); int excelBom = 0;/记录表中有用信息的行数,有用信息是指除去表的标题和表的栏目,本例中表的用用信息是从第三行开始 /确定有用的行数 for (int k = 2; k ExcelTable.Rows.Count; k+) if (storedatak, 1 != ) excelBom+; if (excelBom = 0) . Response.Wri

6、te(alert(您导入的表格不合格式!); else . /LoadDataToDataBase(storedata,excelBom)/该函数主要负责将 storedata 中有用的数据写入到数据库中,在此不是问题的关键省略 运行效果如下图:选择表的路径,点确定后类别:c# | | 添加到搜藏 | 分享到i贴吧 | 浏览(1391) | 评论(7) 上一篇:验证文件上传有效类型的正则表达.下一篇:C#数值结果表(格式化字符串)相关文章:sun:将gridview控件中数据以指定.怎样将Gridview控件的内容导出为.类型GridView的控件 必须放在.用户控件上的GridView如何导

7、入,.使用Gridview空间导出到Excel,Gr.ASP.net的GridView控件中的数据.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)34ApplicationClass cls = null;5Docum

8、ent doc = null;67Table table = null;8object missing = Missing.Value;910object path = fileName;11cls = new ApplicationClass();1213try1415doc = cls.Documents.Open16(ref path, ref missing, ref missing, ref missing,17ref missing, ref missing, ref missing, ref missing,18ref missing, ref missing, ref miss

9、ing, ref missing,19ref missing, ref missing, ref missing, ref missing);20table = doc.Tables1;21string text = table.Cell(rowIndex, colIndex).Range.Text.ToString();22text = text.Substring(0, text.Length - 2);/去除尾部的mark23return text;2425catch (Exception ex)262728return ex.Message;2930finally3132if (doc

10、 != null)33doc.Close(ref missing, ref missing, ref missing);34cls.Quit(ref missing, ref missing, ref missing);3536这个方法用于读取Word表格中某个单元格的数据。其中的参数分别为文件名(包括路径),行号,列号。=由于考虑到代码复用,我将代码写成了一个类。此外,通过审视代码可以发现,如果要多次读取同一文件中的不同的单元格数据会造成频繁的打开、关闭Word程序;因此,我将代码进行优化。在我做优化的时候突然想起来ADO.NET的SqlConnection和SqlCommand类。这两个类

11、我常常用做数据库操作,一般用到的方法顺序都是:打开数据库连接,执行数据库查询,关闭数据库连接。我没有使用到两个类,我将这段代码封装于一个类中。使用Open、Close控制Word文档的打开和关闭,使用WordTableRead方法读取表格中的数据。这样对于读取多个单元格中的数据,每次只需要打开、关闭一次Word程序即可,大大的节约了资源的开销和节省了时间,提高的读取效率。此外,对于代码的优化还有可以读取指定表格中数据。下图显示了这个类的结构,代码及相应注释附在图的下方:class WordTableRead23private string fileName;4private Applicati

12、onClass cls = null;5private Document doc = null;6private Table table = null;7private object missing = Missing.Value;8/Word是否处于打开状态9private bool openState;101112/*/ 13/ 自定义构造方法14/ 15/ 包含路径的文件名16public WordTableRead(string fileName)1718this.fileName = fileName;192021/*/ 22/ 打开Word文档23/ 24public void O

13、pen()2526object path = fileName;27cls = new ApplicationClass();28try2930doc = cls.Documents.Open31(ref path, ref missing, ref missing, ref missing,32ref missing, ref missing, ref missing, ref missing,33ref missing, ref missing, ref missing, ref missing,34ref missing, ref missing, ref missing, ref mi

14、ssing);35openState = true;3637catch3839openState = false;40414243/*/ 44/ 返回指定单元格中的数据45/ 46/ 表格号47/ 行号48/ 列号49/ 单元格中的数据50public string ReadWord(int tableIndex, int rowIndex, int colIndex)5152/Give the value to the tow Int32 params.5354try5556if (openState = true)5758table = doc.TablestableIndex;59str

15、ing text = table.Cell(rowIndex, colIndex).Range.Text.ToString();60text = text.Substring(0, text.Length - 2);/去除尾部的mark61return text;6263else6465return ;666768catch6970return Error;71727374/*/ 75/ 关闭Word文档76/ 77public void Close()7879if (openState = true)8081if (doc != null)82doc.Close(ref missing, ref missing, ref missing);83cls.Quit(ref missing, ref missing, ref missing);848586尽管如此,我还是认为这个类的设计仍然存在缺陷。每次测试这个类的时候,感觉数据读取的速度不是很令我满意;而且,这个类用于控制台应用程序的时候不会在屏幕上看到任何值,不明白应该如何改进代码。希望朋友们能够给我提供一些改进此类的建议。本文来自CSDN博客,转载请标明出处:http:/ 打开网站取消来自: http:/ (注:文件素材和资料部分来自网络,供参考。请预览后才下载,期待你的好评与关注。)

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1