紫龙Blog

*追求才会完美*

操作EXCEL(2)

ADO.net方式

OleDbConnection objConn = new OleDbConnection(
    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strSampleFolder +
    "new.xls;Extended Properties=Excel 8.0;");
   objConn.Open();

   // Add two records to the table named 'MyTable'.
   OleDbCommand objCmd = new OleDbCommand();
   objCmd.Connection = objConn;
   objCmd.CommandText = "CREATE TABLE MyTable2 (FirstName char(255),LastName char(255))";
   objCmd.ExecuteNonQuery();
   
//   objCmd.CommandText = "Insert into MyTable (FirstName, LastName)" +
//    " values ('shao', 'zhidong')";
//   objCmd.ExecuteNonQuery();
//   objCmd.CommandText = "Insert into MyTable (FirstName, LastName)" +
//    " values ('张', '三')";
//   objCmd.ExecuteNonQuery();


   // Close the connection.
   objConn.Close();

自动化方式

// Start a new workbook in Excel.
   objExcel = new Excel.Application();
   objBooks = (Excel.Workbooks)objExcel.Workbooks;
   objBook = (Excel._Workbook)(objBooks.Add(objOpt));

   // Add data to cells of the first worksheet in the new workbook.
   objSheets = (Excel.Sheets)objBook.Worksheets;
   objSheet = (Excel._Worksheet)(objSheets.get_Item(1));
   objRange = objSheet.get_Range("A1", objOpt);
   objRange.set_Value(objOpt,"Last Name");
   objRange = objSheet.get_Range("B1", objOpt);
   objRange.set_Value(objOpt,"First Name");
   objRange = objSheet.get_Range("A2", objOpt);
   objRange.set_Value(objOpt,"shao");
   objRange = objSheet.get_Range("B2", objOpt);
   objRange.set_Value(objOpt,"zhidong");

   // Apply bold to cells A1:B1.
   objRange = objSheet.get_Range("A1", "B1");
   objFont = objRange.Font;
   objFont.Bold=true;

   // Save the workbook and quit Excel.
   objBook.SaveAs(strSampleFolder + "Book1.xls", objOpt, objOpt,
    objOpt, objOpt, objOpt, Excel.XlSaveAsAccessMode.xlNoChange,
    objOpt, objOpt, objOpt, objOpt, objOpt);
   objBook.Close(false, objOpt, objOpt);
   objExcel.Quit();

阅读更多
个人分类: ASP.NET
想对作者说点什么? 我来说一句

计算机二级考试excel操作

2011年05月18日 38KB 下载

matlab和excel接口技术2.rar

2011年07月22日 1.1MB 下载

没有更多推荐了,返回首页

不良信息举报

操作EXCEL(2)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭