作者:乔山办公网日期:
返回目录:excel表格制作
可以。
C#可以使用 oledb,连接并操作excel,可以使用sql,就像操作数据库一样。
创建连接字符串,主要参数有:data source,provider,Extended properties,HDR,IMEX等等。
provider是你用来连接execl的驱动版本,
data source就是你要连接的excel文件,
例子:connStr = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + filePath + ";" + ";Extended Properties=\"Excel 12.0;HDR=NO;IMEX=1\"";
下一步就可以使用这个连接字符串,创建链接对象
objConn = new OleDbConnection(connStr);
然后就可以打开这个连接对象。
objConn.Open();
使用sql语句创建cmd对象
写sql语句时,表名称就是excel表格的sheet名称,
写法举例(以select为例)
select * from [Sheet1$] //sheet的名称写法两边要加上方括号[],表的名称末尾要加上“$”
然后就可以执行cmd命令了636f7079e799bee5baa6332。
简要介绍了开放源码项目Java Excel API,结合示例,详细叙述了有关Excel文件的读取方法,最后给出了一个例程及其运行结果.
方法如下:1、打百开要导入的Excel文件,观察第一列是为字段还是数据度。2、打开SQLServer,在需要导入的数据点击右键【任务】-【导入数据】出现导入导出向导。专3、点击下一步,进入【属选择数据源】页面,注意红框设置。4、点击下一步,进入【
//for get driver...
char szBuf[2001];
WORD cbBufMax = 2000;
WORD cbBufOut;
char *pszBuf = szBuf;
CString sDriver;
//for read the file...
CDatabase database;
CString sSql;
CString sItem1,sItem2;
CString sDsn;
CString sFile;
//-------------------在这里把sFile赋值为你要读取的xls文件的文件名.
///////////////////////////////////////
// Get the names of the installed drivers
//////////////////////////////
if(!SQLGetInstalledDrivers(szBuf,cbBufMax,& cbBufOut))
sDriver="";
else
{
// Search for the driver...
do
{
if( strstr( pszBuf, "Excel" ) != 0 )
{
// Found !
sDriver = CString( pszBuf );
break;
}
pszBuf = strchr( pszBuf, '\0' ) + 1;
}
while( pszBuf[1] != '\0' );
}
// Retrieve the name of the Excel driver. This is
// necessary because Microsoft tends to use language
// specific names like "Microsoft Excel Driver (*.xls)" versus
// "Microsoft Excel Treiber (*.xls)"
// sDriver = GetExcelDriver();
if( sDriver.IsEmpty() )
{
AfxMessageBox("No Excel ODBC driver found");
exit(0);
}
sDsn.Format("ODBC;DRIVER={%s};DSN='';DBQ=%s",sDriver,sFile);
TRY
{
// Open the database using the former created pseudo DSN
database.Open(NULL,false,false,sDsn);
// Allocate the recordset
CRecordset recset( &database );
// Build the SQL string
// Remember to name a section of data in the Excel sheet using "Insert->Names" to be
// able to work with the data like you would with a table in a "real" database. There
// may be more than one table contained in a worksheet.
sSql = "SELECT [A],[B]" //把A,B为xls文件里面的列,如果要读取多列的话,用,隔开
"FROM [Sheet1$] " //xls文件里面的第一个表,如果xls文件里的e799bee5baa6e79fa5e98193e4b893e5b19e334第一个表不是Sheet1这个名字的话,最好改为Sheet1
"ORDER BY [A],[B]"; //从小到大输出,A列为一级,B列为二级
// Execute that query (implicitly by opening the recordset)
recset.Open(CRecordset::forwardOnly,sSql,CRecordset::readOnly);
// Browse the result
while( !recset.IsEOF() )//不断读取A,B列的数据
{
//Read the result line
recset.GetFieldValue("A",sItem1); //把A列的数据传给sItem1
recset.GetFieldValue("B",sItem2); //把B列的数据传给sItem2
//注意!这里的sItem1,sItem2是不断更新的
// Close the database
database.Close();
}
CATCH(CDBException, e)
{
// A database exception occured. Pop out the details...
AfxMessageBox("Database error: "+e->m_strError);
}
END_CATCH;