DataSet ds= new DataSet();
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
using(OleDbConnection OleConn = new OleDbConnection(strConn))
{
OleConn.Open();
String sql = "SELECT * FROM [Sheet1$]";
OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
OleDaExcel.Fill(ds);
OleConn.Close();
}
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
using(OleDbConnection OleConn = new OleDbConnection(strConn))
{
OleConn.Open();
String sql = "SELECT * FROM [Sheet1$]";
OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
OleDaExcel.Fill(ds);
OleConn.Close();
}
#region 单击事件的具体操作
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";//文件以中文字符集保存
Response.ContentEncoding = System.Text.Encoding.UTF7;//将字符编码赋值给HTTP编码 Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString()); Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw); this.GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
#endregion #region 导出Excel必须的方法----确认在运行时为指定的 ASP.NET 服务器控件呈现 HtmlForm 控件。
public override void VerifyRenderingInServerForm(Control control)
{ }
#endregion #region 导出按钮事件
protected void Button1_Click(object sender, EventArgs e)
{
Export("application/ms-excel", "员工基本表.xls");
GridView1.DataSource = CreateDataSource();
GridView1.DataBind();
}
#endregion #region 读取Excel中的代码
private DataSet CreateDataSource()
{
string strCon;
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("excel.xls") + "; Extended Properties=Excel 8.0;";
sda= new SqlDataAdapter("SELECT * FROM [Sheet1$]", strCon);
DataSet myds = new DataSet();
sda.Fill(myds);
return myds;
}
#endregion
private void 导入ToolStripMenuItem_Click(object sender, EventArgs e)
{
// 打开一个选择框
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Excel文件";
ofd.FileName = "";
ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
ofd.Filter = "Excel文件(*.xls)|*.xls";
ofd.ValidateNames = true; // 文件有效性验证vValidateNames,验证用户输入的是否是一个有效的windows 文件名
ofd.CheckFileExists = true; // 验证文件的有效性
ofd.CheckPathExists = true; // 验证路径的有效性 string strName = string.Empty;
if (ofd.ShowDialog() == DialogResult.OK)
{
strName = ofd.FileName;
}
if (strName == "")
{
MessageBox.Show("没有选择Excel文件!无法进行数据导入");
return;
}
ExcelToDataGridView(strName, this.OutLookAll); } // Excel数据导入方法
public void ExcelToDataGridView(string filePath, DataGridView dgv)
{
try
{
// 根据路径打开一个Excel文件并将数据填充到DataSet中
// string strConn = @"Provider = Microsoft.Jet.OLEDB.12.0; Data Source = " + filePath + "; Extended Properties = 'Excel 8.0;HDR = NO; IMEX=1'"; string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;";
strConn += @"Data Source=" + filePath + ";";
strConn += "Persist Security Info=False";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open(); string strExcel = "select * from [sheet1$]";
OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, strConn);
DataSet ds = new DataSet();
myCommand.Fill(ds, "[Sheet1$]"); OutLookAll.DataMember = "[Sheet1$]";
OutLookAll.DataSource = ds;
conn.Close();
}
catch
{
MessageBox.Show("导入失败");
}
}
#endregion
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;";
strConn += @"Data Source=" + filePath + ";";
strConn += "Persist Security Info=False";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
连接不上,请指教下