博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C# 读取excel数据的两个方法

Posted on 2010-11-15 20:00  冷咖啡(子锡)  阅读(8239)  评论(0编辑  收藏  举报

oledb方法

程序代码 程序代码
using System.Data.OleDb;

            string strCon = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\\test.xls; Extended Properties=Excel 8.0";
            OleDbConnection myConn = new OleDbConnection(strCon);
            string strCom = " Select * FROM [1$A1:C100] ";
            myConn.Open();
            OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
            DataSet myDataSet = new DataSet();
            myCommand.Fill(myDataSet, "1$");
            myConn.Close();
            dataGridView1.DataSource = myDataSet.Tables[0];
            //MessageBox.Show(dataGridView1.ColumnCount.ToString());
            dataGridView1.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
            dataGridView1.Columns[1].SortMode = DataGridViewColumnSortMode.NotSortable;
            dataGridView1.Columns[2].SortMode = DataGridViewColumnSortMode.NotSortable;
            dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dataGridView1.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dataGridView1.ReadOnly = true;


Excel.Application方法

程序代码 程序代码
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;

            Excel.Application oXL = new Microsoft.Office.Interop.Excel.Application();
            oXL.Visible = true;

            Excel._Workbook oWB2=(Excel._Workbook)oXL.Workbooks.Open(@"f:\test.xls",
                Missing.Value,
                Missing.Value,
                Missing.Value,
                Missing.Value,
                Missing.Value,
                Missing.Value,
                Missing.Value,
                Missing.Value,
                Missing.Value,
                Missing.Value,
                Missing.Value,
                Missing.Value,
                Missing.Value,
                Missing.Value
                );

            Excel._Worksheet oSheet = (Excel._Worksheet)oWB.ActiveSheet;
            Excel.Shape testShape;
            Excel.Range orng;
            testShape = oSheet.Shapes.AddLine(30, 30, 100, 30);
            testShape.Line.BeginArrowheadStyle = Microsoft.Office.Core.MsoArrowheadStyle.msoArrowheadOval;
            testShape.Line.EndArrowheadStyle = Microsoft.Office.Core.MsoArrowheadStyle.msoArrowheadOpen;
            testShape.Line.Style = Microsoft.Office.Core.MsoLineStyle.msoLineThickBetweenThin;
            
            oWB2.SaveAs(@"c:\1.xls",
                Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8,
                Missing.Value,
                Missing.Value,
                Missing.Value,
                Missing.Value,
                Excel.XlSaveAsAccessMode.xlNoChange,
                Missing.Value,
                Missing.Value,
                Missing.Value,
                Missing.Value,
                Missing.Value);
            oXL.Quit();