读书人

Winform中Excel导入Sqlserver中报“外

发布时间: 2012-09-29 10:30:01 作者: rapoo

Winform中Excel导入Sqlserver中报“外部表不是预期的格式。”怎么回事?急
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;


namespace BookMangersSYS
{
public partial class ExcelToServer : Form
{
public ExcelToServer()
{
InitializeComponent();
}

private void btnExcel_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();//打开文件对话框。
openFile.Filter = ("*.xls|*.xlsx");//后缀名。
openFile.ShowDialog();
XlsToDataTable(openFile.FileName);
}

//获取连接
private DataSet XlsToDataTable(String strpath)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strpath + ";"
+ "Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();//????????????这里报错。
string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = null;
strExcel = "select * from [sheet1$]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
ds = new DataSet();
myCommand.Fill(ds, "table1");
return ds;

}

[解决办法]
我想你用的应该是office2007吧,你只要将那个xlsx文件另存为标准格式XLS文件再读取就行了

读书人网 >.NET

热点推荐