asp.net(C#)如何读取Excel的文件的实例详解


当前第2页 返回上一页

下面列出按钮单击事件中的3个方法

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

///<summary>

  ///上传文件到临时目录中

  ///</ummary>

  private void Upload()

  {

  HttpPostedFile file = this.fileSelect.PostedFile;

  string fileName = file.FileName;

  string tempPath = System.IO.Path.GetTempPath(); //获取系统临时文件路径

  fileName = System.IO.Path.GetFileName(fileName); //获取文件名(不带路径)

  this.currFileExtension = System.IO.Path.GetExtension(fileName); //获取文件的扩展名

  this.currFilePath = tempPath + fileName; //获取上传后的文件路径 记录到前面声明的全局变量

  file.SaveAs(this.currFilePath); //上传

  }

 

  ///<summary>

    ///读取xls\xlsx格式的Excel文件的方法

    ///</ummary>

    ///<param name="path">待读取Excel的全路径</param>

    ///<returns></returns>

    private DataTable ReadExcelToTable(string path)

    {

    //连接字符串

    string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; // Office 07及以上版本 不能出现多余的空格 而且分号注意

    //string connstring = Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; //Office 07以下版本 因为本人用Office2010 所以没有用到这个连接字符串 可根据自己的情况选择 或者程序判断要用哪一个连接字符串

    using(OleDbConnection conn = new OleDbConnection(connstring))

    {

    conn.Open();

    DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object[]{null,null,null,"Table"}); //得到所有sheet的名字

    string firstSheetName = sheetsName.Rows[0][2].ToString(); //得到第一个sheet的名字

    string sql = string.Format("SELECT * FROM [{0}],firstSheetName); //查询字符串

    OleDbDataAdapter ada =new OleDbDataAdapter(sql,connstring);

    DataSet set = new DataSet();

    ada.Fill(set);

    return set.Tables[0];

    }

    }

 

    ///<summary>

      ///读取csv格式的Excel文件的方法

      ///</ummary>

      ///<param name="path">待读取Excel的全路径</param>

      ///<returns></returns>

      private DataTable ReadExcelWithStream(string path)

      {

      DataTable dt = new DataTable();

      bool isDtHasColumn = false; //标记DataTable 是否已经生成了列

      StreamReader reader = new StreamReader(path,System.Text.Encoding.Default); //数据流

      while(!reader.EndOfStream)

      {

      string meaage = reader.ReadLine();

      string[] splitResult = message.Split(new char[]{','},StringSplitOption.None); //读取一行 以逗号分隔 存入数组

      DataRow row = dt.NewRow();

      for(int i = 0;i<splitResult.Length;i++)

      {

      if(!isDtHasColumn) //如果还没有生成列

      {

      dt.Columns.Add("column" + i,typeof(string));

      }

      row[i] = splitResult[i];

      }

      dt.Rows.Add(row); //添加行

      isDtHasColumn = true; //读取第一行后 就标记已经存在列 再读取以后的行时,就不再生成列

      }

      return dt;

      }


以上就是asp.net(C#)如何读取Excel的文件的实例详解的详细内容!

返回前面的内容

相关阅读 >>

asp.net core mvc应用程度中如何读取当前url请求参数(querystring)的值?

常用的asp.net 技巧总结

asp.net数据库密码:md5加密算法详解

asp.net关于cookie跨域的问题

asp.net下的中文分词检索工具分享

学习asp.net core 2遇到的问题分享

如何使用会话状态(asp.net web 服务)

mvc页面重定向的asp代码讲解

分享19个asp脚本语言的基本技巧

asp.net core razor页面路由的详细介绍

更多相关阅读请进入《asp.net》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...