『易坊知识库摘要_如何|C如何读取Excel表格数据并显示到GridView控』2、View 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();Data...
按关键词阅读: 读取 如何 GridView 显示 数据 表格 Excel
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,Grid 。
2、View 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.Ole 。
3、DbSchemaGuid.Tables, null);
string tableName = schemaTable.Rows02.ToString().Trim();
/获取 Excel 的表名 , 默认值是sheet1string strSql = select * from + tableName + ;
OleDbCommand objCmd = new OleDbCommand(strSql, objConn);
OleDbDataAdapter myData = https://www.renrendoc.com/paper/new OleDbDataAdapter(strSql, objConn);
myData.Fill(ds, tableName);
/ 。
4、填充数据dgBom.DataSource =ds;
dgBom.DataBind();
objConn.Close();
ExcelTable = ds.TablestableName;
int iColums = ExcelTable.Columns.Count;
/列数int iRows = ExcelTable.Rows.Count;
/行数/定义二维数组存储 Excel 表中读取的数据string, storedata = https://www.renrendoc.com/paper/new stringiRows, iColums;
for(int i=0;
i使用C#读取Word表格数据 读取Word表格数据的方法1/将读取Word表格封装与方法中 。
2pub 。
5、lic 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, r 。
6、ef 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 。
7、 - 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表格中某个单元格的数据 。
其中的参数分别为文件名(包括路径) , 行号 , 列号 。
=由于考虑到代码复用 , 我将代码写成了一个类 。
此外 , 通过审视代码可以发现 , 如果 。
8、要多次读取同一文件中的不同的单元格数据会造成频繁的打开、关闭Word程序;因此 , 我将代码进行优化 。
在我做优化的时候突然想起来ADO.NET的SqlConnection和SqlCommand类 。
这两个类我常常用做数据库操作 , 一般用到的方法顺序都是:打开数据库连接 , 执行数据库查询 , 关闭数据库连接 。
我没有使用到两个类 , 我将这段代码封装于一个类中 。
使用Open、Close控制Word文档的打开和关闭 , 使用WordTableRead方法读取表格中的数据 。
这样对于读取多个单元格中的数据 , 每次只需要打开、关闭一次Word程序即可 , 大大的节约了资源的开销和节省了时间 , 提高的读取效率 。
此外 , 对于代码的优化还有可 。
9、以读取指定表格中数据 。
下图显示了这个类的结构 , 代码及相应注释附在图的下方: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。
10、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, 。
11、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。
【如何|C如何读取Excel表格数据并显示到GridView控】
来源:(未知)
【学习资料】网址:/a/2021/0201/0021260792.html
标题:如何|C如何读取Excel表格数据并显示到GridView控