以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  [求助]求版主修改一下写入execl文件代码  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=90111)

--  作者:jyh7081
--  发布时间:2016/9/5 20:46:00
--  [求助]求版主修改一下写入execl文件代码
想不打开execl文件,将foxtable当前表静默地写入指定execl。运行代码时,感到打开了execl,另外黑体部分想换成删除表内所有数据。
请版主指教,谢谢!

Dim dt As Table = CurrentTable  \'当前表
Dim App As New MSExcel.Application
Dim Wb As MSExcel.Workbook = App.WorkBooks.Open(ProjectPath & "\\数据源.xlsm")
Dim Ws As MSExcel.WorkSheet = Wb.worksheets("数据表")

Dim Rg As MSExcel.Range \'= Ws.Rows("1:3000")    \'删除execl表内 Wb.worksheets("数据表")所有数据
Rg = Ws.Cells
Rg.Delete

For c As Integer = 0 To dt.Cols.Count -1                  \'添加列标题
    ws.cells(1,c+1).Value = dt.Cols(c).Name
Next
For r As Integer = 0 To dt.Rows.Count - 1               \'填入数据
    For c As Integer = 0 To dt.Cols.Count -1
        ws.cells(r+2, c+1).Value = dt.rows(r)(c)
    Next
Next
app.visible = False
Wb.Save
App.Quit

--  作者:有点蓝
--  发布时间:2016/9/5 20:59:00
--  
始终都要打开一个Excel进程,这是没有办法的

Dim dt As Table = CurrentTable  \'当前表
Dim App As New MSExcel.Application
Dim Wb As MSExcel.Workbook = App.WorkBooks.Open(ProjectPath & "\\数据源.xlsm")
Dim Ws As MSExcel.WorkSheet = Wb.worksheets("数据表")

Dim Rg As MSExcel.Range = Ws.UsedRange
Rg.Delete

For c As Integer = 0 To dt.Cols.Count -1                  \'添加列标题
    ws.cells(1,c+1).Value = dt.Cols(c).Name
Next
For r As Integer = 0 To dt.Rows.Count - 1               \'填入数据
    For c As Integer = 0 To dt.Cols.Count -1
        ws.cells(r+2, c+1).Value = dt.rows(r)(c)
    Next
Next
app.visible = False
Wb.Save
App.Quit

--  作者:jyh7081
--  发布时间:2016/9/5 21:06:00
--  
谢谢版主!