app.Quit(); app = null; } } } 二、读取到dataset中/从dataset中写入sql server: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Microsoft.Office.Interop.Excel; using System.Data.OleDb; namespace ExcelDemo { public partial class Form2 : Form { public Form2() { InitializeComponent(); } /// <summary> /// 读取Excel文档 /// </summary> /// <param name="Path">文件名称</param> /// <returns>返回一个数据集</returns> public DataSet ExcelToDS(string Path) { string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;"; OleDbConnection conn = new OleDbConnection(strConn); conn.Open(); string strExcel = ""; OleDbDataAdapter myCommand = null; DataSet ds = null; strExcel = "select * from [Recovered_Sheet1$]"; myCommand = new OleDbDataAdapter(strExcel, strConn); ds = new DataSet(); myCommand.Fill(ds); return ds; } |