求 EXCEL 导入方法 支持导入 xlsm 格式
现在需要支持导入 xlsm格式的导入方法,请各位赐教! 谢谢!
[最优解释]
你用07意思的office组件就可以了
[其他解释]
连接没打开吧?strConn.Open()
EXCEL的DLL也需要引用,代码供参考
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.IO;
using System.Reflection;
using System.Data.OleDb;
private void Bingdgv()
{
if (FilePath == "")
{
return;
}
try
{
OleDbConnection conn = new OleDbConnection(ExcelConnStr);
string sql = "select * from [Sheet1$]";
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
OleDbDataAdapter myCommand = new OleDbDataAdapter(sql, conn);
ds = new DataSet();
myCommand.Fill(ds, "[Sheet1$]");
conn.Close();
dgvData.DataMember = "[Sheet1$]";
dgvData.DataSource = ds;
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
throw ex;
}
}
Dim dtTemp As DataTable
Dim strSql, strConn As String
Dim MyData As OleDbDataAdapter
dtTemp = New DataTable
strSql = "SELECT * FROM [" & strSheet & "$]"
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strExcelFile & ";Extended Properties=""Excel 8.0;IMEX=1"""
Try
MyData = New OleDbDataAdapter(strSql, strConn)
MyData.Fill(dtTemp)
MyData.Dispose()
Catch ex As Exception
End Try
Return dtTemp