C#如何读取Excel表格数据并显示到GridView控件.doc_第1页
C#如何读取Excel表格数据并显示到GridView控件.doc_第2页
C#如何读取Excel表格数据并显示到GridView控件.doc_第3页
C#如何读取Excel表格数据并显示到GridView控件.doc_第4页
C#如何读取Excel表格数据并显示到GridView控件.doc_第5页
已阅读5页,还剩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,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.Rows02.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.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+) 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.Write(alert(您导入的表格不合格式!); else . /LoadDataToDataBase(storedata,excelBom)/该函数主要负责将 storedata 中有用的数据写入到数据库中,在此不是问题的关键省略 运行效果如下图:选择表的路径,点确定后类别:c# | | 添加到搜藏 | 分享到i贴吧 | 浏览(1391) | 评论(7) 上一篇:验证文件上传有效类型的正则表达.下一篇:C#数值结果表(格式化字符串)相关文章:sun:将gridview控件中数据以指定.怎样将Gridview控件的内容导出为.类型GridView的控件 必须放在.用户控件上的GridView如何导入,.使用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;5Document 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 missing, 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 != null)33doc.Close(ref missing, ref missing, ref missing);34cls.Quit(ref missing, ref missing, ref missing);3536这个方法用于读取Word表格中某个单元格的数据。其中的参数分别为文件名(包括路径),行号,列号。=由于考虑到代码复用,我将代码写成了一个类。此外,通过审视代码可以发现,如果要多次读取同一文件中的不同的单元格数据会造成频繁的打开、关闭Word程序;因此,我将代码进行优化。在我做优化的时候突然想起来ADO.NET的SqlConnection和SqlCommand类。这两个类我常常用做数据库操作,一般用到的方法顺序都是:打开数据库连接,执行数据库查询,关闭数据库连接。我没有使用到两个类,我将这段代码封装于一个类中。使用Open、Close控制Word文档的打开和关闭,使用WordTableRead方法读取表格中的数据。这样对于读取多个单元格中的数据,每次只需要打开、关闭一次Word程序即可,大大的节约了资源的开销和节省了时间,提高的读取效率。此外,对于代码的优化还有可以读取指定表格中数据。下图显示了这个类的结构,代码及相应注释附在图的下方:class WordTableRead23private string fileName;4private ApplicationClass 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 Open()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 missing);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;59string 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尽管如此,我还是认为这个类的设计仍然存在缺陷。每次测试这个类的时候,感觉数据读取

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论