16 messaggi dal 30 gennaio 2008
Ho creato una Web application con Visual studio 2005 - asp.net, vbscript (data base: SQLserver2005).
Creo files excel con OXPPIA's.
Questo è il codice

...
Imports System.Runtime.InteropServices.Marshal
Imports Microsoft.Office.Interop.Excel

...

Sub exportToExcel()

Dim numRighe As Integer = 65000, numFile As Integer = 0
Dim iRiga As Integer = 2
Dim dataRow As DataRow, ary() As Object
Dim iRow As Integer, iCol As Integer
Dim oExcel As New Microsoft.Office.Interop.Excel.Application()
Dim oBooks As Microsoft.Office.Interop.Excel.Workbooks, oBook As Microsoft.Office.Interop.Excel.Workbook
Dim oSheets As Microsoft.Office.Interop.Excel.Sheets, oSheet As Microsoft.Office.Interop.Excel.Worksheet
Dim oCells As Microsoft.Office.Interop.Excel.Range

Dim sFile As String, sTemplate As String


If cnSql.State = ConnectionState.Closed Then cnSql.Open()

queryString = "SELECT ......"

Dim dataSet As DataSet = New DataSet
Dim adapter As New SqlDataAdapter()
adapter.SelectCommand = New SqlCommand(queryString, cnSql)
adapter.Fill(dataSet, "exc")

Dim dt As System.Data.DataTable = dataSet.Tables("exc")

anno = Trim(DropDownList1.SelectedValue)

sFile = "C:/Customer/" & nameC & "_" & Year & ".xls"

sTemplate = Server.MapPath("../Report/Template.xls")

oExcel.Visible = False : oExcel.DisplayAlerts = False
oBooks = oExcel.Workbooks
oBooks.Open(Server.MapPath("../Report/Template.xls"))
oBook = oBooks.Item(1)
oSheets = oBook.Worksheets
oSheet = CType(oSheets.Item(1), Microsoft.Office.Interop.Excel.Worksheet)
oSheet.Name = year
oCells = oSheet.Cells

For iRow = 0 To dt.Rows.Count - 1
iRiga = iRiga + 1
dataRow = dt.Rows.Item(iRow)

ary = dataRow.ItemArray

For iCol = 0 To UBound(ary)
oCells(iRiga, iCol + 1) = ary(iCol).ToString
Next
If iRow = numRighe Then
numFile = numFile + 1
sFile = "C:/Customer/" & nameC & "-0" & numFile & "_" & year & ".xls"
oSheet.SaveAs(sFile)

numRighe = numRighe + 65000
iRiga = 2

oBook.Close()
oBooks = oExcel.Workbooks
oBooks.Open(Server.MapPath("../Report/Template.xls"))
oBook = oBooks.Item(1)
oSheets = oBook.Worksheets
oSheet = CType(oSheets.Item(1), Microsoft.Office.Interop.Excel.Worksheet)
oSheet.Name = year
oCells = oSheet.Cells
End If
Next

numFile = numFile + 1
sFile = "C:/Customers/" & nameC & "-0" & numFile & "_" & year & ".xls"
oSheet.SaveAs(sFile)
***
***
oBook.Close()

oExcel.Quit()
ReleaseComObject(oCells) : ReleaseComObject(oSheet)
ReleaseComObject(oSheets) : ReleaseComObject(oBook)
ReleaseComObject(oBooks) : ReleaseComObject(oExcel)
oExcel = Nothing : oBooks = Nothing : oBook = Nothing
oSheets = Nothing : oSheet = Nothing : oCells = Nothing

System.GC.Collect()

If cnSql.State = ConnectionState.Open Then cnSql.Close()

End Sub
-------------------------------------

Sul mio computer tutto ok, caricando l'applicazione sul server mi sono accorto che non funziona xchè
il codice punta al web server e quindi io tento di salvare i files sul server.
Dovrei invece creare files da inviare al client, dando la possibilità all'end user
di scegliere se aprire o salvare e dove salvare.

Ho letto su decine di siti dei riferimenti a Response.AddHeader ma non ci ho capito molto e
da alcune prove, l'unico risultato è stato di ottenere in formato excel la pagina aspx del mio sito,
anzichè i files di excel!
Evidentemente non capisco in che punto devo catturare il file creato per inviarlo al client!

Ho lasciato tutto il codice così com'era, tranne che nel punto del codice che ho asteriscato *** (vedi sopra) ci
ho aggiunto (vedi sotto):

...............
numFile = numFile + 1
sFile = "C:/Customers/" & nameC & "-0" & numFile & "_" & year & ".xls"
oSheet.SaveAs(sFile)
***

Response.Buffer = True
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("content-disposition", "inline; filename=" & nomeConto & "-0" & numFile & "_" & anno & ".xls")

***
oBook.Close()
oExcel.Quit() ecc.
...............

Cosa devo scrivere esattamente e dove lo devo inserire nella mia routine?
Qualcuno mi può aiutare? mi può con un copia/incolla riscrivere tutto il codice inserendo le
modifiche necessarie in modo che io riesca a capire ?

Grazie! Tappat

c'è altro modo per creare dinamic da database files excel senza usare il template come ho fatto io?

Torna al forum | Feed RSS

ASPItalia.com non è responsabile per il contenuto dei messaggi presenti su questo servizio, non avendo nessun controllo sui messaggi postati nei propri forum, che rappresentano l'espressione del pensiero degli autori.