Mi WEB no funciona al cargar un excel en el servidor con OPEN XML pero si funciona localmente

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
todo funciona bien cuando ejecuto localmente pero cuando abro la pagina publicamente no muestra los datos , este es mi codigoImports System.Data
Imports System.Data.OleDb
Imports System.IO
Imports System.Configuration
Imports System.Text
Imports System.Web
Imports System.Net
Imports Microsoft.Office.Interop
Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Spreadsheet
Imports DocumentFormat.OpenXml.Packaging
Imports S = DocumentFormat.OpenXml.Spreadsheet.Sheets
Imports E = DocumentFormat.OpenXml.OpenXmlElement
Imports A = DocumentFormat.OpenXml.OpenXmlAttribute
Imports System.Xml
Imports System.Net.Mail
Imports System.Threading
Imports AjaxControlToolkit
Imports System.Net.Sockets.SocketException

Public Class Carga_Masiva
Inherits System.Web.UI.Page
Dim objn As New SMSRIPLEY_NEGOCIO.Class1

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Session("IdUsuario") <> Nothing) Then
If (Not Page.IsPostBack) Then



End If
Else
Response.Redirect("Index.aspx")
End If
End Sub

Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpload.Click

lblhoraarchuvo.Text = DateTime.Now.Hour.ToString("D2") + "-" + DateTime.Now.Minute.ToString("D2") + "-" + DateTime.Now.Second.ToString("D2")
lblfechaa.Text = DateTime.Today.ToString("dd-MM-yyyy")


If FileUpload1.HasFile Then
Dim FileName As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
Dim Extension As String = Path.GetExtension(FileUpload1.PostedFile.FileName)
Dim FolderPath As String = ConfigurationManager.AppSettings("FolderPath")

Dim rutaArchivo As String = Server.MapPath(FolderPath + FileUpload1.FileName)

Dim FilePath As String = Server.MapPath(FolderPath + FileName)
FileUpload1.SaveAs(FilePath)
Thread.Sleep(5000)
Import_To_Grid(FilePath, Extension, rbHDR.SelectedItem.Text)
End If

Dim dt As New DataTable

Session("paginacion") = dt
gvdetalle.DataSource = dt

Dim dt As New DataTable
dt = objn.CargaSMS(txt_nombrecarga.Text.ToString)
lblidusuario.Text = dt.Rows(0)(0).ToString()

For Each row As GridViewRow In gvdetalle.Rows

Dim Mensaje As String = row.Cells(3).Text.ToString
Dim encodemen As String = Server.HtmlEncode(Mensaje)

Dim dt2 As DataTable
dt2 = objn.DetalleCargaSMS((row.Cells(0).Text.ToString.Replace(" ", "")), (row.Cells(1).Text.ToString.Replace(" ", "")), row.Cells(2).Text.ToString, HttpUtility.HtmlDecode(Mensaje), Convert.ToInt32(lblidusuario.Text.ToString))

Next

Dim dt3 As New DataTable
dt3 = objn.Valida_carga(Convert.ToInt32(lblidusuario.Text.ToString))
gvDetallecarga.DataSource = dt3
gvDetallecarga.DataBind()


For i As Integer = 0 To gvdetalle.Rows.Count
Dim dt2 As DataTable

dt2 = objn.DetalleCargaSMS(gvdetalle.Rows(i).Cells(0).Text.ToString, gvdetalle.Rows(i).Cells(1).Text.ToString, gvdetalle.Rows(i).Cells(2).Text.ToString, gvdetalle.Rows(i).Cells(3).Text.ToString, Convert.ToInt32(txtid.Text.ToString))

Next

If (gvDetallecarga.Rows(0).Cells(2).Text >= 1 Or gvDetallecarga.Rows(0).Cells(3).Text >= 1 Or gvDetallecarga.Rows(0).Cells(4).Text >= 1) Then
warningMPE3.Show()
btnEnviar.Visible = False
btncancelar.Visible = True
panelagendar.Visible = False

warningMPE.Hide()
Else
btnEnviar.Visible = True
btncancelar.Visible = True
panelagendar.Visible = True
txt_fechacar.Visible = False
ImageButton1.Visible = False
dpdHora.Visible = False
lblpubnto.Visible = False
dpdMinuto.Visible = False
lblhora.Visible = False
chk_envio.Checked = False
panelagendar.Visible = True
warningMPE.Show()
warningMPE3.Hide()
End If




chk_envio_CheckedChanged(sender, e)

End Sub
Private Function obtenerNombrePrimeraHoja(ByVal rutaLibro As String) As String
Dim app As Excel.Application = Nothing
Try
app = New Excel.Application()
Dim wb As Excel.Workbook = app.Workbooks.Open(rutaLibro)
Dim ws As Excel.Worksheet = CType(wb.Worksheets.Item(1), Excel.Worksheet)
Dim name As String = ws.Name
ws = Nothing
wb.Close()
wb = Nothing
Return name
Catch ex As Exception
Throw
Finally
If (Not app Is Nothing) Then _
app.Quit()
Runtime.InteropServices.Marshal.ReleaseComObject(app)
app = Nothing
End Try
End Function

Public Function GetSheetInfo(ByVal fileName As String)
Open file as read-only.
Using mySpreadsheet As SpreadsheetDocument = SpreadsheetDocument.Open(fileName, False)
Dim sheets As S = mySpreadsheet.WorkbookPart.Workbook.Sheets
For each sheet, display the sheet information.
For Each sheet As E In sheets
For Each attr As A In sheet.GetAttributes()
Dim nomhoja As String = attr.LocalName
Console.WriteLine("{0}: {1}", attr.LocalName, attr.Value)
Return nomhoja
Next
Next
Return 0
End Using
End Function


Private Sub Import_To_Grid(ByVal FilePath As String, ByVal Extension As String, ByVal isHDR As String)

Try
Dim conStr As String = ""

Select Case Extension
Case ".xls"
Excel 97-03
conStr = ConfigurationManager.ConnectionStrings("Excel03ConString").ConnectionString
Exit Select
Case ".xlsx"
Excel 07-10
conStr = ConfigurationManager.ConnectionStrings("Excel07ConString").ConnectionString
Exit Select
End Select

conStr = String.Format(conStr, FilePath, isHDR)

Dim connExcel As New OleDbConnection(conStr)
Dim cmdExcel As New OleDbCommand()
Dim oda As New OleDbDataAdapter()
Dim dt As New DataTable()
Dim FolderPath As String = ConfigurationManager.AppSettings("FolderPath")
Dim rutaArchivo As String = Server.MapPath(FolderPath + FileUpload1.PostedFile.FileName)


cmdExcel.Connection = connExcel
Get the name of First Sheet
connExcel.Open()
Dim dtExcelSchema As DataTable
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
Dim SheetName As String = (dtExcelSchema.Rows(0)("TABLE_NAME").ToString())
connExcel.Close()

connExcel.Open()
cmdExcel = connExcel.CreateCommand()
Dim nombrehoja = GetSheetInfo(rutaArchivo)
cmdExcel.CommandText = String.Format(" SELECT * FROM [{0}$]", nombrehoja)
oda.SelectCommand = cmdExcel
oda.Fill(dt)
connExcel.Close()



Read Data from First Sheet
connExcel.Open()
Dim nombreArchivo As String = obtenerNombrePrimeraHoja(FilePath)
cmdExcel.CommandText = "SELECT * From [smspre$]"
cmdExcel.CommandText = String.Format(" SELECT * FROM [{0}$]", nombreArchivo)
oda.SelectCommand = cmdExcel
oda.Fill(dt)
connExcel.Close()


Bind Data to GridView
gvdetalle.Caption = Path.GetFileName(FilePath)
gvdetalle.DataSource = dt
gvdetalle.DataBind()

Catch ex As Exception
Se ha producido un error.

End Try

End Sub

Protected Sub PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvdetalle.PageIndexChanging

Dim FolderPath As String = ConfigurationManager.AppSettings("FolderPath")
Dim FileName As String = gvdetalle.Caption
Dim Extension As String = Path.GetExtension(FileName)
Dim FilePath As String = Server.MapPath(FolderPath + FileName)

Import_To_Grid(FilePath, Extension, rbHDR.SelectedItem.Text)
gvdetalle.PageIndex = e.NewPageIndex
gvdetalle.DataBind()


End Sub


Protected Sub chk_envio_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles chk_envio.CheckedChanged
If chk_envio.Checked = True Then

lblfecha.Visible = True

txt_fechacar.Visible = True
ImageButton1.Visible = True
dpdHora.Visible = True
lblpubnto.Visible = True
dpdMinuto.Visible = True
chk_envio.Text = 1
lblhora.Visible = True

ElseIf chk_envio.Checked = False Then

lblfecha.Visible = False
txt_fechacar.Visible = False
ImageButton1.Visible = False
dpdHora.Visible = False
lblpubnto.Visible = False
dpdMinuto.Visible = False
chk_envio.Text = 0
lblhora.Visible = False
txt_fechacar.Text = DateTime.Today.ToString("dd/MM/yyyy")

End If
End Sub
Private Function Listar_Tipo_Mensaje()


Return Nothing
End Function

Protected Sub btnEnviar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnEnviar.Click
chk_envio_CheckedChanged(sender, e)
Dim dt4 As New DataTable
dt4 = objn.GrabarCargaaSMS(Convert.ToInt32(lblidusuario.Text.ToString), Convert.ToInt32(Session("IDusuario")).ToString(), txt_fechacar.Text, (dpdHora.SelectedValue.ToString()) + ":" + (dpdMinuto.SelectedValue.ToString()), Convert.ToInt32(chk_envio.Text))


Dim dt As New DataTable
dt = objn.EnviarSMS(Convert.ToInt32(Session("IDusuario")).ToString(), Convert.ToInt32(lblidusuario.Text.ToString), Convert.ToInt32(chk_envio.Text), (dpdHora.SelectedValue.ToString()) + ":" + (dpdMinuto.SelectedValue.ToString()),
(txt_fechacar.Text))
warningMPE2.Show()
gvdetalle.DataSource = Nothing
gvdetalle.DataBind()
gvDetallecarga.DataSource = Nothing
gvDetallecarga.DataBind()

btnEnviar.Visible = False
panelagendar.Visible = False
btncancelar.Visible = False
txt_fechacar.Text = ""
txt_nombrecarga.Text = ""

chk_envio.Text = ""
Correo






Dim dte As New DataTable
dte = objn.EnviodeCorreo(Convert.ToInt32(lblidusuario.Text.ToString))

Dim correo As New System.Net.Mail.MailMessage()

correo.From = New System.Net.Mail.MailAddress("soporte@intico.com.pe")

correo.To.Add("soporte@intico.com.pe")
correo.To.Add("rlivia@intico.com.pe")
correo.To.Add("sgonzalez@intico.cl")
correo.IsBodyHtml = True

Prioridad del Correo Alta, Baja Normal
correo.Priority = System.Net.Mail.MailPriority.Normal
correo.Subject = "Notificaciones SMS PREMIUM "
correo.Body = "<html><head><title>SMS PREMIUM</title></head><body> <p style=font-size:16px; color: #F26000;> Se Cargo una Base de SMS en la plataforma SMS PREMIUM con las siguientes caracteristicas: <p style=font-size:16px; color: #F26000;> <table border=1><tr><td width=150 scope=col style=font-size: 13px; font-weight: normal;padding: 5px; border-bottom: 1px solid #fff;color: #FFF; background-color: #F26000; >Usuario</td><td width=150 scope=col style=font-size: 13px; font-weight: normal;padding: 5px; border-bottom: 1px solid #fff;color: #FFF; background-color: #F26000; >Archivo</td><td width=150 scope=col style=font-size: 13px; font-weight: normal;padding: 5px; border-bottom: 1px solid #fff;color: #FFF; background-color: #F26000; >Cantidad de Registros</td><td width=150 scope=col style=font-size: 13px; font-weight: normal;padding: 5px; border-bottom: 1px solid #fff;color: #FFF; background-color: #F26000; >Fecha de Carga</td><td width=150 scope=col style=font-size: 13px; font-weight: normal;padding: 5px; border-bottom: 1px solid #fff;color: #FFF; background-color: #F26000; >Compañia</td></tr><tr><td>" + dte.Rows(0)("Empresa").ToString() + "</td><td>" + dte.Rows(0)("r_archivo").ToString() + "</td><td>" + dte.Rows(0)("cantidad").ToString() + "</td><td>" + dte.Rows(0)("f_envio").ToString() + "</td><td>" + dte.Rows(0)("Compañia").ToString() + "</td></tr></table></body></html> "



Dim smtp As New System.Net.Mail.SmtpClient
Estos datos hay que llenarlos correctamente

smtp.Host = "mail.intico.com.pe"
smtp.Port = "587"

smtp.Credentials = New System.Net.NetworkCredential("soporte@intico.com.pe", "6567supp/*2012")
Y Enviamos el mensajeNot Not Not
smtp.Send(correo)
Try
smtp.Send(correo)
lblcorreo.Text = "Mensaje enviado"
Catch ex As Exception
lblcorreo.Text = "Error" & ex.Message
End Try

End Sub




Protected Sub gvdetalle_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles gvdetalle.SelectedIndexChanged

End Sub

Protected Sub btncancelar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btncancelar.Click
Response.Redirect("Carga_Masiva.aspx")
End Sub
End Class

View the full article
 
Back
Top