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;
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();
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();