ASP.NET - Leer y validar un archivo de excel en asp.net

Filtrado por el usuario: Marianela
<<>>
Filtrado por el usuario 'Marianela' (Eliminar fitro)
 
Vista:

Leer y validar un archivo de excel en asp.net

Publicado por omar corona (1 intervención) el 19/03/2016 18:30:51
Buenas tardes al foro, les comparto mi problema y si alguien me podria orientar a como resolverlo

Estoy creando una pagina en asp.net y necesito subir un archivo de excel con cierta estructura (adjunto documento) y requiero que valide ese documento campo por campo.

Ejemplo

Nombre fecha direccion Cp cantidad RFC

Nombre (Texto)
fecha (Numeros)
Direccion (texto)
CP (Numeros)
cantidad (numeros)
RFC(Texto, numeros y longitud de caracteres)

Si alguien haya querido hacer lo mismo y lo haya resuelto o tenga una idea de como resolverlo le agradeceria infinitamente.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Leer y validar un archivo de excel en asp.net

Publicado por Javier Lopez Vargas (10 intervenciones) el 23/07/2016 00:06:09
Hola Amigo tengo este codigo espero te pueda servir en algo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
protected bool GrabarDatos()
        {
            bool bValido = true;
            sError = "Archivo inválido";
 
            //Si se carga un archivo en el control fileUpload
            if (this.updArchivo.HasFile)
            {
                if(this.updArchivo.PostedFile.ContentLength > 0 && this.updArchivo.FileName.EndsWith(".xls"))
                {
                    sError = string.Empty;
                    DataSet DataSet = new DataSet();
 
                    try
                    {
                        //se obtiene la ruta y se guarda el archivo cargado
                        string sNombreArchivoTemp = System.IO.Path.GetTempFileName();
                        string sNombreArchivo = string.Empty;
                        sNombreArchivo = System.IO.Path.GetFileName(this.updArchivo.PostedFile.FileName);
                        this.updArchivo.PostedFile.SaveAs(sNombreArchivoTemp);
 
                        OleDbConnection Connexion = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sNombreArchivoTemp + ";Extended Properties=Excel 8.0");
                        OleDbDataAdapter DataAdapt = new OleDbDataAdapter("SELECT * FROM [Estructura$]", Connexion);
 
                        DataAdapt.Fill(DataSet);
                    }
                    catch
                    {
                        sError = "Archivo Inválido";
                        return false;
                    }
 
                    string[] Colum = { "Accion", "Horas Ejecutadas" };
                    int i = 0;
 
                    if (DataSet.Tables.Count > 0)
                    {
 
                        //verificamos que el archivo contenga la cantidad de columnas
                        if (DataSet.Tables[0].Columns.Count != Colum.Length)
                        {
                            bValido = false;
                            sError = "Archivo de carga incorrecto. Verificar número de columnas";
                        }
                        else
                        {
                            bValido = true;
                        }
                        //Se verifica la estructura del archivo
                        foreach(DataColumn Col in DataSet.Tables[0].Columns)
                        {
                            if (bValido)
                            {
                                if (Col.Caption.ToUpper().Trim() != Colum[i].ToUpper())
                                {
                                    bValido = false;
                                    sError = "Archivo de carga es incorrecto";
                                    break;
                                }
                            }
                            else
                            {
                                break;
                            }
                            i++;
                        }
                        //Se verifica que existan datos para ser cargados
                        if (DataSet.Tables[0].Rows.Count == 0)
                        {
                            sError = "Archivo no posee datos para carga";
                            bValido = false;
                        }
 
                        //Se verifican los datos del archivo
                        if (bValido)
                        {
                            BOL.BOL_ACC_CAP oBOLAcc;
                            BOL.CONTROL_ERROR oControlError;
 
                            foreach (DataRow DataRow in DataSet.Tables[0].Rows)
                            {
                                oBOLAcc = new BOL.BOL_ACC_CAP();
                                oControlError = new BOL.CONTROL_ERROR();
                                BLL.BLL_ACC_CAP oBLLAcc = new BLL.BLL_ACC_CAP();
 
                                //Se cargan las filas del excel
                                try
                                {
                                    oBOLAcc = CargarOjeto(DataRow);
                                    if (oBOLAcc.ESTADO2 > 0)
                                    {
                                        oControlError = oBLLAcc.UPDATE_ACC_CAP(oBOLAcc);
                                    }
                                    else
                                    {
                                        oControlError.EstCod = -1;
                                    }
                                    if (oControlError.EstCod < 0)
                                    {
                                        if (sError.Length == 0)
                                        {
                                            sError = "Error en: " + oBOLAcc.ID_ACC_CAP;
                                        }
                                        else
                                        {
                                            sError = sError + "</br>" + "Error en: " + oBOLAcc.ID_ACC_CAP;
                                        }
                                    }
                                }
                                catch
                                {
                                    if (sError.Length == 0)
                                    {
                                        sError = "Error al grabar objeto: " + oBOLAcc.ID_ACC_CAP;
                                    }
                                    else
                                    {
                                        sError = sError + "</br>" + "Error al grabar objeto: " + oBOLAcc.ID_ACC_CAP;
                                    }
                                }
                            }
                            //Se lee cada elemento del archivo y se graba
                            if (sError.Length > 0)
                            {
                                return false;
                            }
                            else
                            {
                                return true;
                            }
                        }
                    }
                }
            }
            return false;
 
        }
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar