假如我想把Excel中的某一工作表中的内容显示在DataGrid中怎么办呢?请帮忙?
解决方案 »
- 从字符串 到类型 Double 的转换无效
- aspx页面上点击button后,下载服务器端某个文件,该怎么实现啊
- 不知道怎么弄这效果 各位大哥大姐帮帮忙
- ASP.NET实现文件下载功能
- 安装IIS时提示需要Messenger上的文件'custsat.dll'. 这是怎么回事\?
- LInq排序
- 关于GLOBAL.ASAX文件提供的时件处理程序.
- <meta>具体有什么作用啊!
- 呼唤强人!如何在DATAGRID的EditItemTemplate摸板列的问题?
- vb中的createobject在.net中用什么替换!急!
- 关于在一个有编辑功能的DataGrid里点击删除记录的列的时候弹出一个确认对话框,所出现的问题?
- 怎么对dataset中的数据进行整理.
Try
'定义连接字符串
Dim strConn As String
Dim strPath As String
strPath = Me.txtFileName.Text.ToString.Replace("\", "\\")
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strPath + ";Extended Properties=Excel 8.0;" '连接文件,取Excel数据到DataSet中
Dim connection As New OleDb.OleDbConnection(strConn) '取得 Excel workSheet 的名字
Dim xlWorkSheetName As String
xlWorkSheetName = GetExcelWorkSheet(excel文件路径包括文件名) '填充数据集
Dim adapter As New OleDb.OleDbDataAdapter("select * from [" + xlWorkSheetName + "$]", connection)
Dim ds As New DataSet
adapter.Fill(ds, "Excel") '关闭连接
connection.Close()
'绑定dataGrid
Me.dgData.DataSource = ds.Tables("Excel").DefaultView '强制垃圾回收,用于kill Excel Process
GC.Collect() Catch err As Exception
'捕获错误信息;弹出,结束
MsgBox(err.Message, MsgBoxStyle.OKOnly)
Return
End Try
End Sub Function GetExcelWorkSheet(ByVal sPath As String) As String
'返回给定路径 Excel 文件的 workSheet 名字
Dim xlApp As New Excel.Application
Dim xlWorkSheet As New Excel.Worksheet
Dim xlName As String '得到worksheet名字
xlApp.Workbooks.Open(sPath)
xlWorkSheet = xlApp.Worksheets.Item(1)
xlName = xlWorkSheet.Name '关闭excel处理
xlApp.Workbooks.Close()
xlApp.Quit()
xlWorkSheet = Nothing
xlApp = Nothing '返回名字字符串
Return xlName
End Function
为什么Worksheets没有Item属性呢?
'返回给定路径 Excel 文件的 workSheet 名字
Dim xlApp As New Excel.Application
Dim xlWorkSheet As New Excel.Worksheet
Dim xlName As String '得到worksheet名字
xlApp.Workbooks.Open(sPath)
xlWorkSheet = xlApp.Worksheets.Item(1)
xlName = xlWorkSheet.Name '关闭excel处理
xlApp.Workbooks.Close()
xlApp.Quit()
xlWorkSheet = Nothing
xlApp = Nothing '返回名字字符串
Return xlName
End Function
用c#怎么写?
{ // '返回给定路径 Excel 文件的 workSheet 名字
Excel.Application xlApp = new Excel.Application();
Excel.Worksheet xlWorkSheet = newExcel.Worksheet();
string xlName;
//'得到worksheet名字
xlApp.Workbooks.Open(sPath);
xlWorkSheet = xlApp.Worksheets.Item[1];
xlName = xlWorkSheet.Name; //'关闭excel处理
xlApp.Workbooks.Close();
xlApp.Quit();
xlWorkSheet = null;
xlApp = null;
//'返回名字字符串
return xlName;
}
xlWorkSheet = xlApp.Worksheets.Item[1];//Worksheets中无Item属性