经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C# » 查看文章
C# 读取Excel,一波华丽的操作
来源:cnblogs  作者:碧水青荷  时间:2018/10/20 15:37:57  对本文有异议

C# 读取Excel,其实有很多方法。但是今天要来一波华丽的操作。

先看效果:

以上这波操作使用了 ExcelDataReader 和 ExcelDataReader.DataSet 完成的。

ExcelDataReader 是一个快速读取 Excel 的 C# 库。使用简单,读取速度比较快,感觉比 NPOI 快一点。但是遗憾的是只能读 Excel 没有写的操作。

以上这波操作的全部代码:

  1. using ExcelDataReader;
  2. using System;
  3. using System.IO;
  4. using System.Windows.Forms;
  5. namespace ExcelFastRead
  6. {
  7. public partial class FrmMain : Form
  8. {
  9. public FrmMain()
  10. {
  11. InitializeComponent();
  12. }
  13. private void button1_Click(object sender, EventArgs e)
  14. {
  15. OpenFileDialog dialog = new OpenFileDialog();
  16. dialog.Filter = "(Excel 97-03)|*.xls|(Excel 2007)|*.xlsx|ALL|*.*";
  17. if (DialogResult.OK != dialog.ShowDialog())
  18. {
  19. return;
  20. }
  21. string strFileName = dialog.FileName;
  22. if (string.IsNullOrWhiteSpace(strFileName))
  23. {
  24. return;
  25. }
  26. using (var stream = File.Open(strFileName, FileMode.Open, FileAccess.Read))
  27. {
  28. // Auto-detect format, supports:
  29. // - Binary Excel files (2.0-2003 format; *.xls)
  30. // - OpenXml Excel files (2007 format; *.xlsx)
  31. using (var reader = ExcelReaderFactory.CreateReader(stream))
  32. {
  33. // Choose one of either 1 or 2:
  34. // 1. Use the reader methods
  35. do
  36. {
  37. while (reader.Read())
  38. {
  39. // reader.GetDouble(0);
  40. }
  41. } while (reader.NextResult());
  42. ExcelDataSetConfiguration configuration = new ExcelDataSetConfiguration()
  43. {
  44. // Gets or sets a value indicating whether to set the DataColumn.DataType
  45. // property in a second pass.
  46. //UseColumnDataType = true,
  47. // Gets or sets a callback to obtain configuration options for a DataTable.
  48. ConfigureDataTable = (tableReader) => new ExcelDataTableConfiguration()
  49. {
  50. // Gets or sets a value indicating the prefix of generated column names.
  51. //EmptyColumnNamePrefix = "Column",
  52. // Gets or sets a value indicating whether to use a row from the
  53. // data as column names.
  54. UseHeaderRow = true,
  55. // Gets or sets a callback to determine which row is the header row.
  56. // Only called when UseHeaderRow = true.
  57. //ReadHeaderRow = (rowReader) => {
  58. // // F.ex skip the first row and use the 2nd row as column headers:
  59. // rowReader.Read();
  60. //},
  61. // Gets or sets a callback to determine whether to include the
  62. // current row in the DataTable.
  63. //FilterRow = (rowReader) => {
  64. // return true;
  65. //},
  66. // Gets or sets a callback to determine whether to include the specific
  67. // column in the DataTable. Called once per column after reading the
  68. // headers.
  69. //FilterColumn = (rowReader, columnIndex) => {
  70. // return true;
  71. //}
  72. }
  73. };
  74. var result = reader.AsDataSet(configuration);
  75. // 2. Use the AsDataSet extension method
  76. //var result = reader.AsDataSet();
  77. dgvList.DataSource = result.Tables[0];
  78. // The result of each spreadsheet is in result.Tables
  79. }
  80. }
  81. }
  82. }
  83. }

 

ExcelDataReader 项目地址

ExcelDataReader nuget包管理

ExcelDataReader.DataSet nuget包管理

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号