1. 想要用VS2010做一个上位机excel导出数据,但是却发现没有excel类库,怎么办!
如果是C#程序,可使用下面方法
添加引用:Microsoft.Office.Interop.Excel
可参考这个位置 C:\Program Files (x86)\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office11\Microsoft.Office.Interop.Excel.dll
在CS文件中,using Microsoft.Office.Interop.Excel; 即可
如果是其他语言,可参考上面的方式
2. 利用vs2010、C#做一个EXCEL与dataGridView的数据相互导入导出!!
导入:
(stringfileFullPath)
{
//strConn="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="+fileFullPath+";ExtendedProperties='Excel8.0;HDR=False;IMEX=1;'";//只适合xls后缀
stringstrConn="Provider=Microsoft.ACE.OLEDB.12.0;DataSource="+fileFullPath+";ExtendedProperties='Excel12.0;HDR=NO;IMEX=1;'";
OleDbConnectionoleConn=newOleDbConnection(strConn);
try
{
oleConn.Open();
DataTablesheetNames=oleConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables,newobject[]{null,null,null,"TABLE"});
stringtableName="sheet1$";
if(sheetNames.Rows.Count>0)
{
tableName=sheetNames.Rows[0][2].ToString();
}
//foreach(DataRowdrinsheetNames.Rows)
//{
//if(dr[2].ToString().Replace("$","")!="sheet1")
//{
//continue;
//}
//else
//{
//tableName="sheet1";
//break;
//}
//}
if(tableName.Length<=0)
{
returnnull;
}
stringsql="select*from["+tableName+"]";
OleDbDataAdapteroleDaExcel=newOleDbDataAdapter(sql,oleConn);
DataSetds=newDataSet();
oleDaExcel.Fill(ds,tableName);
//CNoteFacadefacade=newCNoteFacade();
returnds.Tables[0];
}
catch(Exception)
{
returnnull;
}
finally
{
oleConn.Close();
}
}
导出:http://blog.csdn.net/anzi_peng/article/details/17414629