c#操作excel方式三:使用Microsoft.Office.Interop.Excel.dll读取Excel文件

c#操作excel方式三:使用Microsoft.Office.Interop.Excel.dll读取Excel文件

1、引用Microsoft.Office.Interop.Excel.dll

2、引用命名空间、使用别名

  1. using System.Reflection;  
  2.   
  3. using Excel = Microsoft.Office.Interop.Excel;  

写入按钮函数:

[csharp] view plain copy

  1. private void button1_Click(object sender, EventArgs e)  
  2.         {  
  3.             ToExcel("方式3");  
  4.   
  5.         }  

结果:

 

4.读取excel

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

private void OpenExcel(string strFileName) 

    object missing = System.Reflection.Missing.Value; 

    Excel.Application excel = new Excel.ApplicationClass();//lauch excel application 

    if (excel == null

    

       this.label1.Text="Can't access excel"

    

    else 

    

        excel.Visible = false; excel.UserControl = true

        // 以只读的形式打开EXCEL文件 

        Excel.Workbook wb = excel.Application.Workbooks.Open(strFileName, missing, true, missing, missing, missing, 

         missing, missing, missing, true, missing, missing, missing, missing, missing); 

        //取得第一个工作薄 

        Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets.get_Item(1); 

        //取得总记录行数    (包括标题列) 

        int rowsint = ws.UsedRange.Cells.Rows.Count; //得到行数 

        //int columnsint = mySheet.UsedRange.Cells.Columns.Count;//得到列数 

        //取得数据范围区域   (不包括标题列)   

        Excel.Range rng1 = ws.Cells.get_Range("A2""A" + rowsint);   

        Excel.Range rng2 = ws.Cells.get_Range("B2""B" + rowsint); 

        Excel.Range rng3 = ws.Cells.get_Range("C2""C" + rowsint);   

        Excel.Range rng4 = ws.Cells.get_Range("D2""D" + rowsint);   

        object[,] arry1 = (object[,])rng1.Value2;   //get range's value 

        object[,] arry2 = (object[,])rng2.Value2; 

        object[,] arry3 = (object[,])rng3.Value2;   //get range's value 

        object[,] arry4 = (object[,])rng4.Value2; 

        //将新值赋给一个数组 

        string[,] arry = new string[rowsint - 1, 4]; 

        //for (int i = 1; i <= rowsint - 1; i++) 

        for (int i = 1; i <= rowsint - 2; i++) 

        

              

            arry[i - 1, 0] = arry1[i, 1].ToString(); 

             

            arry[i - 1, 1] = arry2[i, 1].ToString(); 

   

            arry[i - 1, 2] = arry3[i, 1].ToString(); 

              

            arry[i - 1, 3] = arry4[i, 1].ToString(); 

        

        string a = ""

        for (int i = 0; i <= rowsint - 3; i++) 

        

            a += arry[i, 0] + "|" + arry[i, 1] + "|" + arry[i, 2] + "|" + arry[i, 3]+"\n"

              

        

       this.label1.Text=a; 

    

    excel.Quit(); excel = null

  Process[] procs = Process.GetProcessesByName("excel"); 

    foreach (Process pro in procs) 

    

        pro.Kill();//没有更好的方法,只有杀掉进程 

    

    GC.Collect(); 

  

读取按钮代码:

[csharp] view plain copy

 

  1. private void button1_Click(object sender, EventArgs e)  
  2.       {  
  3.           if (openFileDialog.ShowDialog() == DialogResult.OK)  
  4.           {  
  5.               OpenExcel(openFileDialog.FileName);  
  6.           }  
  7.   
  8.       }  

 

添加一个opendiog用于选择要读取的excel

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值