原地址:http://blog.csdn.net/ChengKing/archive/2005/11/29/539514.aspx
(一).内容
在操作Excel的过程中遇到了一些问题. 比如: 访问Com组件权限,无法读取Excel等
文章描述了怎样双向操作(读取和生成)Excel文件,以及怎样解决遇到的问题!
(二).代码
开始时用了下面两个方法进行生成和读取 Excel:
1.生成Excel文件方法一:
'
<summary>
' 下载Excel方法1(用流实现)
' </summary>
' <param name="dt">要转换为Excel文件的表</param>
' <param name="page">页面Page对象,用法: 将me.Page传递过来即可</param>
Public Sub DownLoadExcelToClient1(ByVal dt As DataTable, ByVal FileName As String)
Dim resp As HttpResponse
resp = Page.Response
resp.ContentEncoding = System.Text.Encoding.Default
' System.Text.Encoding.GetEncoding("GB2312")
resp.AppendHeader( " Content-Disposition " , " attachment;filename= " + FileName)
Dim colHeaders As String = "" , ls_item = ""
Dim i As Int16 = 0
' 取得数据表各列标题,各标题之间以 分割,最后一个列标题后加回车符
For i = 0 To dt.Columns.Count - 2
colHeaders += dt.Columns(i).Caption.ToString() & Chr( 9 )
Next
colHeaders += dt.Columns(i).Caption.ToString() & Chr( 13 )
' 向HTTP输出流中写入取得的数据信息
resp.Write(colHeaders)
Dim row As DataRow
' 逐行处理数据
' 下载Excel方法1(用流实现)
' </summary>
' <param name="dt">要转换为Excel文件的表</param>
' <param name="page">页面Page对象,用法: 将me.Page传递过来即可</param>
Public Sub DownLoadExcelToClient1(ByVal dt As DataTable, ByVal FileName As String)
Dim resp As HttpResponse
resp = Page.Response
resp.ContentEncoding = System.Text.Encoding.Default
' System.Text.Encoding.GetEncoding("GB2312")
resp.AppendHeader( " Content-Disposition " , " attachment;filename= " + FileName)
Dim colHeaders As String = "" , ls_item = ""
Dim i As Int16 = 0
' 取得数据表各列标题,各标题之间以 分割,最后一个列标题后加回车符
For i = 0 To dt.Columns.Count - 2
colHeaders += dt.Columns(i).Caption.ToString() & Chr( 9 )
Next
colHeaders += dt.Columns(i).Caption.ToString() & Chr( 13 )
' 向HTTP输出流中写入取得的数据信息
resp.Write(colHeaders)
Dim row As DataRow
' 逐行处理数据