
16/08/2013, 10:08
|
Usuario no validado | | Fecha de Ingreso: abril-2010 Ubicación: La habana
Mensajes: 229
Antigüedad: 14 años, 10 meses Puntos: 4 | |
Respuesta: Leyendo Excel con C# Ya lo hice, con la colavoración de un amigo e internet, puede modificar un código y así quedó como protopito de solución de este tema en cuestión. Cita: using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;
namespace my_excel
{
class Program
{
static void Main(string[] args)
{
// archivo de entrada
string file = "MiExcel.xls";
string dir = Environment.GetFolderPath(Environment.SpecialFolde r.Desktop);
string xlsFilePath = Path.Combine(dir, file);
read_file(xlsFilePath);
Console.ReadKey();
}
public static void read_file(string xlsFilePath)
{
if (!File.Exists(xlsFilePath))
return;
Excel.Application xlApp ;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;
var misValue = Type.Missing;//System.Reflection.Missing.Value;
// abrir el documento
xlApp = new Microsoft.Office.Interop.Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(xlsFilePath, misValue, misValue,
misValue, misValue, misValue, misValue, misValue, misValue,
misValue, misValue, misValue, misValue, misValue, misValue);
// seleccion de la hoja de calculo
// get_item() devuelve object y numera las hojas a partir de 1
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1) ;
// seleccion rango activo
range = xlWorkSheet.UsedRange;
// leer las celdas
int rows = range.Rows.Count;
int cols = range.Columns.Count;
for (int row = 1; row <= rows; row++)
{
for (int col = 1; col <= cols; col++)
{
// lectura como cadena
string str_value = (range.Cells[row, col] as Excel.Range).Value2.ToString();
if (str_value=="?")
{
Console.WriteLine("string:{0}", str_value);
Console.WriteLine(row.ToString());
}
}
}
// cerrar
xlWorkBook.Close(false, misValue, misValue);
xlApp.Quit();
// liberar
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
public static void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComO bject(obj);
obj = null;
}
catch (Exception ex)
{
Console.WriteLine("Unable to release the object(object:{0})", obj.ToString());
}
finally
{
obj = null;
GC.Collect();
}
}
}
} Salu2 |