读取Excel记录

方法1:
 1ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
 2InBlock.gif        /// 将指定Excel文件中的数据转换成DataTable对象,供应用程序进一步处理
 3InBlock.gif        /// </summary>
 4InBlock.gif        /// <param name="filePath"></param>
 5ExpandedBlockEnd.gif        /// <returns></returns>

 6None.gif        public static System.Data.DataTable Import(string filePath)
 7ExpandedBlockStart.gifContractedBlock.gif        dot.gif{
 8InBlock.gif            System.Data.DataTable rs = new System.Data.DataTable();
 9InBlock.gif            bool canOpen=false;
10InBlock.gif            OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;"+"Data Source=" + 
11InBlock.gif
12InBlock.giffilePath + ";" + "Extended Properties=\"Excel 8.0;\"");
13InBlock.gif            try//尝试数据连接是否可用
14ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
15InBlock.gif                conn.Open();
16InBlock.gif                conn.Close();
17InBlock.gif                canOpen=true;
18ExpandedSubBlockEnd.gif            }

19ExpandedSubBlockStart.gifContractedSubBlock.gif            catchdot.gif{}
20InBlock.gif            //文件可以打开
21InBlock.gif            if(canOpen)
22ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
23InBlock.gif                try//如果数据连接可以打开则尝试读入数据
24ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
25InBlock.gif                    OleDbCommand myOleDbCommand = new OleDbCommand("SELECT * FROM [Sheet1$]",conn);
26InBlock.gif                    OleDbDataAdapter myData = new OleDbDataAdapter(myOleDbCommand);
27InBlock.gif                    myData.Fill(rs);
28InBlock.gif                    conn.Close();
29ExpandedSubBlockEnd.gif                }
        
30InBlock.gif                catch//如果数据连接可以打开但是读入数据失败,则从文件中提取出工作表的名称,再读入数据
31ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
32InBlock.gif                    string sheetName=GetSheetName(filePath);
33InBlock.gif                    if(sheetName.Length>0)
34ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
35InBlock.gif                        OleDbCommand myOleDbCommand = new OleDbCommand("SELECT * FROM 
36InBlock.gif
37InBlock.gif["+sheetName+"$]",conn);
38InBlock.gif                        OleDbDataAdapter myData = new OleDbDataAdapter(myOleDbCommand);
39InBlock.gif                        myData.Fill(rs);
40InBlock.gif                        conn.Close();
41ExpandedSubBlockEnd.gif                    }

42ExpandedSubBlockEnd.gif                }

43ExpandedSubBlockEnd.gif            }

44InBlock.gif            else
45ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
46InBlock.gif                System.IO.StreamReader tmpStream=File.OpenText(filePath);
47InBlock.gif                string tmpStr=tmpStream.ReadToEnd();
48InBlock.gif                tmpStream.Close();
49InBlock.gif                rs=GetDataTableFromString(tmpStr);
50InBlock.gif                tmpStr="";
51ExpandedSubBlockEnd.gif            }

52InBlock.gif            return rs;
53ExpandedBlockEnd.gif        }
 


 

方法2:

 

 1None.gifprivate void OnLoad(object sender, System.EventArgs e)
 2ExpandedBlockStart.gifContractedBlock.gif        dot.gif{
 3InBlock.gif            // 在此处放置用户代码以初始化页面
 4InBlock.gif            Excel.Application xApp=new Excel.ApplicationClass(); 
 5InBlock.gif
 6InBlock.gif            xApp.Visible=true
 7InBlock.gif            //得到WorkBook对象, 可以用两种方式之一: 下面的是打开已有的文件 
 8InBlock.gif            Excel.Workbook xBook=xApp.Workbooks._Open(@"D:\Sample.xls"
 9InBlock.gif                Missing.Value,Missing.Value,Missing.Value,Missing.Value 
10InBlock.gif                ,Missing.Value,Missing.Value,Missing.Value,Missing.Value 
11InBlock.gif                ,Missing.Value,Missing.Value,Missing.Value,Missing.Value); 
12InBlock.gif            //xBook=xApp.Workbooks.Add(Missing.Value);//新建文件的代码 
13InBlock.gif            //指定要操作的Sheet,两种方式: 
14InBlock.gif
15InBlock.gif            Excel.Worksheet xSheet=(Excel.Worksheet)xBook.Sheets[1]; 
16InBlock.gif            //Excel.Worksheet xSheet=(Excel.Worksheet)xApp.ActiveSheet; 
17InBlock.gif
18InBlock.gif            //读取数据,通过Range对象 
19InBlock.gif            Excel.Range rng1=xSheet.get_Range("A1",Type.Missing); 
20InBlock.gif            Console.WriteLine(rng1.Value2); 
21InBlock.gif
22InBlock.gif            //读取,通过Range对象,但使用不同的接口得到Range 
23InBlock.gif            Excel.Range rng2=(Excel.Range)xSheet.Cells[3,1]; 
24InBlock.gif            Console.WriteLine(rng2.Value2); 
25InBlock.gif
26InBlock.gif            //写入数据 
27InBlock.gif            Excel.Range rng3=xSheet.get_Range("C6",Missing.Value); 
28InBlock.gif            rng3.Value2="Hello"
29InBlock.gif            rng3.Interior.ColorIndex=6//设置Range的背景色 
30InBlock.gif
31InBlock.gif            //保存方式一:保存WorkBook 
32InBlock.gif            xBook.SaveAs(@"D:\CData.xls"
33InBlock.gif                Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value, 
34InBlock.gif                Excel.XlSaveAsAccessMode.xlNoChange,Missing.Value,Missing.Value,Missing.Value, 
35InBlock.gif                Missing.Value,Missing.Value); 
36InBlock.gif
37InBlock.gif            //保存方式二:保存WorkSheet 
38InBlock.gif            xSheet.SaveAs(@"D:\CData2.xls"
39InBlock.gif                Missing.Value,Missing.Value,Missing.Value,Missing.Value, 
40InBlock.gif                Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value); 
41InBlock.gif
42InBlock.gif
43InBlock.gif            //保存方式三 
44InBlock.gif            xBook.Save(); 
45InBlock.gif
46InBlock.gif            xSheet=null
47InBlock.gif            xBook=null
48InBlock.gif            xApp.Quit(); //这一句是非常重要的,否则Excel对象不能从内存中退出 
49InBlock.gif            xApp=null
50ExpandedBlockEnd.gif        }
 

转载于:https://www.cnblogs.com/treeyh/archive/2007/05/24/758717.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值