请问如何将DataGrid所搜到显示的数据导入到一个新的Excel里?
2个回答
展开全部
一个成功案例
Private Sub command2_Click()
Dim I As Integer, r As Integer, c As Integer
Dim XB As Excel.Application, FS1 As Object
Dim XGZB As Excel.Workbook
Dim XWJ As Excel.Worksheet
Set XB = CreateObject("Excel.Application") '创建excel应用程序,打开excel2000
Set XGZB = XB.Workbooks.Add '创建工作簿
Set XWJ = XGZB.Worksheets(1) '创建工作表
Set FS1 = CreateObject("Scripting.FileSystemObject")
For J = 0 To 23
For I = 0 To SST
XWJ.Cells(I + 1, J + 1) = Trim(DataGrid1.TextMatrix(I, J))
Next I
Next J
Dim myval As Long
Dim SQ1 As String
On Error GoTo ErrSave
SQ1 = App.Path & "\转换文件.xls"'文件路径和文件名
If FS1.FileExists(SQ1) = True Then
FS1.DeleteFile SQ1, True
End If
XWJ.SaveAs SQ1 '导入文件路径和文件名
MsgBox "Excel文件保存成功!" & vbCrLf & vbCrLf & "你现在可以在“" & App.Path & "”目录下打开“转换文件.xls”文件!" & vbCrLf & vbCrLf & _
"添加到数据文件中!",vbOKOnly, "文件保存成功!"
XB.Quit
ErrSave:
Exit Sub
MsgBox Err.Description,vbOKOnly, "提示窗口"
End Sub
Private Sub command2_Click()
Dim I As Integer, r As Integer, c As Integer
Dim XB As Excel.Application, FS1 As Object
Dim XGZB As Excel.Workbook
Dim XWJ As Excel.Worksheet
Set XB = CreateObject("Excel.Application") '创建excel应用程序,打开excel2000
Set XGZB = XB.Workbooks.Add '创建工作簿
Set XWJ = XGZB.Worksheets(1) '创建工作表
Set FS1 = CreateObject("Scripting.FileSystemObject")
For J = 0 To 23
For I = 0 To SST
XWJ.Cells(I + 1, J + 1) = Trim(DataGrid1.TextMatrix(I, J))
Next I
Next J
Dim myval As Long
Dim SQ1 As String
On Error GoTo ErrSave
SQ1 = App.Path & "\转换文件.xls"'文件路径和文件名
If FS1.FileExists(SQ1) = True Then
FS1.DeleteFile SQ1, True
End If
XWJ.SaveAs SQ1 '导入文件路径和文件名
MsgBox "Excel文件保存成功!" & vbCrLf & vbCrLf & "你现在可以在“" & App.Path & "”目录下打开“转换文件.xls”文件!" & vbCrLf & vbCrLf & _
"添加到数据文件中!",vbOKOnly, "文件保存成功!"
XB.Quit
ErrSave:
Exit Sub
MsgBox Err.Description,vbOKOnly, "提示窗口"
End Sub
展开全部
Response.Clear();
Response.Buffer= true;
Response.Charset="GB2312";
Response.AppendHeader("Content-Disposition","attachment;filename=FileName.xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.DataGrid1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
Response.Buffer= true;
Response.Charset="GB2312";
Response.AppendHeader("Content-Disposition","attachment;filename=FileName.xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.DataGrid1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询