• C# 读取 Excel xlsx 文件,显示在 DataGridView 中


    编写 read_excel.cs 如下

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.IO;
    5. using System.Data;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Data.OleDb;
    9. namespace ReadExcel
    10. {
    11. public partial class Program
    12. {
    13. static void Main(string[] args)
    14. {
    15. if (args.Length <1){
    16. Console.WriteLine(" usage: read_excel your_file.xlsx ");
    17. return ;
    18. }
    19. if (! File.Exists(args[0])){
    20. Console.WriteLine("Error: {0} not exists.", args[0]);
    21. return ;
    22. }
    23. if (Path.GetExtension(args[0]) != ".xlsx"){
    24. Console.WriteLine("Tip: can only read file.xlsx");
    25. }
    26. string filePath = args[0]; // your_excel_file_path
    27. string connStr = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\"", filePath);
    28. using (OleDbConnection conn = new OleDbConnection(connStr))
    29. {
    30. conn.Open();
    31. string sheet1 = "Sheet1";
    32. string query = string.Format("SELECT * FROM [{0}$]", sheet1);
    33. using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn))
    34. {
    35. DataTable dataTable = new DataTable();
    36. adapter.Fill(dataTable);
    37. int rows, cols;
    38. // 处理获取到的数据
    39. foreach (DataRow row in dataTable.Rows)
    40. {
    41. rows = row.Table.Rows.IndexOf(row) +1;
    42. foreach (DataColumn column in dataTable.Columns)
    43. {
    44. string value = row[column].ToString() ?? string.Empty;
    45. cols = column.Ordinal +1;
    46. Console.WriteLine("Cell({0:d},{1:d}): {2}", rows,cols,value);
    47. }
    48. }
    49. }
    50. }
    51. Console.ReadKey();
    52. }
    53. }
    54. }

    SET PATH=C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319;%PATH%

    编译:csc.exe  /t:exe read_excel.cs  

    环境:win10 64位系统 运行 \your\path\read_excel.exe  test1.xlsx

    错误信息:未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序。

    搜索 Microsoft Access Database Engine 2016 

    我先下载了  accessdatabaseengine.exe 安装好后,还是运行出错。

    卸载了32位版本,又下载了 AccessDatabaseEngine_X64.exe 安装好后,能运行了。

    参考:C#读取Excel表格数据到DataGridView中和导出DataGridView中的数据到Excel

    C#读取Excel表格数据到DataGridView中,代码如下

    1. private void btnShow_Click(object sender, EventArgs e)
    2. { //首先根据打开文件对话框,选择excel表格
    3. OpenFileDialog fd = new OpenFileDialog();
    4. fd.Filter = "xlsx表格|*.xlsx"; //打开文件对话框筛选器
    5. string strPath;//文件完整的路径名
    6. if (fd.ShowDialog() == DialogResult.OK)
    7. {
    8. try
    9. {
    10. strPath = fd.FileName;
    11. string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + strPath + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\"";
    12. OleDbConnection Con = new OleDbConnection(strCon);//建立连接
    13. string strSql = "select * from [Sheet1$]";//表名的写法也应注意不同,对应的excel表为sheet1,在这里要在其后加美元符号$,并用中括号
    14. OleDbCommand Cmd = new OleDbCommand(strSql, Con);//建立要执行的命令
    15. OleDbDataAdapter da = new OleDbDataAdapter(Cmd);//建立数据适配器
    16. DataSet ds = new DataSet();//新建数据集
    17. da.Fill(ds, "sheet1");//把数据适配器中的数据读到数据集中的一个表中(此处表名为sheet1,可以任取表名)
    18. //指定datagridview1的数据源为数据集ds的第一张表(也就是sheet1表),也可以写ds.Table["sheet1"]
    19. dataGridView1.DataSource = ds.Tables[0];
    20. }
    21. catch (Exception ex)
    22. {
    23. MessageBox.Show(ex.Message);//捕捉异常
    24. }
    25. }
    26. }

  • 相关阅读:
    一文让你搞懂什么是TypeScript
    驱动基石之_tasklet中断下半部_工作队列_中断线程化处理
    【CLI命令行接口和Java连接openLooKeng查询数据 】
    Python之父加入微软三年后,Python嵌入Excel!
    C++(string类)
    【Rust】环境搭建
    初探webpack之编写loader
    初识计算机网络
    MyBatis中的一二级缓存
    kicad源代码研究:symbol properties窗口中为SCH_SYMBOL添加或删除一个sch_field
  • 原文地址:https://blog.csdn.net/belldeep/article/details/133496989