using System;
using System.IO;
using System.Collections;
using System.Globalization;
using Excel = Microsoft.Office.Interop.Excel;
namespace WindowsApplication1
{
public class excel_n
{
static void Main()
{
string source = "E:\\archivos\\source.xls"; //debe existir
string destiny = "E:\\archivos\\destiny.xls"; //debe existir
string temp = "E:\\archivos\\destiny_temp.xls";
string filePath = "E:\\archivos\\file.txt";
//copia las celdas
Console.WriteLine("Copiando...\n");
copy(source,destiny,temp);
createFile(filePath);
//lee las celdas activas y devuelve un arraylist
Console.WriteLine("Leyendo...");
getOrderRows(temp,filePath);
//escribir orden CSV
//Console.WriteLine("Escribiendo csv");
//createOrden(records);
//Listo
Console.WriteLine("Listo...");
Console.ReadKey();
}
/*
| Copia las celdas de la orden a la hoja BASE
| @param string source Hoja de la orden
| @param string destiny Hoja BASE
| @param string temp Archivo temporal de destino
*/
public static void copy(string source,string destiny,string temp)
{
//source
Excel.Application xlAppSource;
Excel.Workbook xlWorkBookSource;
Excel.Worksheet xlWorkSheetSource;
Excel.Range rangeSource;
//destiny
Excel.Application xlAppDestiny;
Excel.Workbook xlWorkBookDestiny;
Excel.Worksheet xlWorkSheetDestiny;
Excel.Range rangeDestiny;
string str;
int rCnt = 0;
int cCnt = 0;
//Apps
xlAppSource = new Excel.ApplicationClass();
xlAppDestiny = new Excel.ApplicationClass();
//Source
xlWorkBookSource = xlAppSource.Workbooks.Open(source, 0, false, 5, null, null, false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, null, true, false, 0, true, false, false);
xlWorkSheetSource = (Excel.Worksheet)xlWorkBookSource.Worksheets.get_Item(1);
rangeSource = xlWorkSheetSource.UsedRange;
//Destiny
xlWorkBookDestiny = xlAppDestiny.Workbooks.Open(destiny, 0, false, 5, null, null, false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, null, true, false, 0, true, false, false);
xlWorkSheetDestiny = (Excel.Worksheet)xlWorkBookDestiny.Worksheets.get_Item(1);//hoja BASE
rangeDestiny = xlWorkSheetDestiny.UsedRange;
for (rCnt = 1; rCnt <= rangeSource.Rows.Count; rCnt++)
{
for (cCnt = 1; cCnt <= rangeSource.Columns.Count; cCnt++)
{
str = (string)(rangeSource.Cells[rCnt, cCnt] as Excel.Range).Text;
//xlWorkSheetDestiny.Cells[rCnt, cCnt] = 10;
xlWorkSheetDestiny.Cells[rCnt, cCnt] = xlWorkSheetSource.Cells[rCnt, cCnt];
Console.WriteLine(str);
}
}
//Source
xlWorkBookSource.Close(false,null,null);
xlAppSource.Quit();
//Destiny
xlWorkBookDestiny.Close(true, temp, null); //guarda destiny con otro nombre
xlAppDestiny.Quit();
releaseObject(xlWorkSheetSource);
releaseObject(xlWorkBookSource);
releaseObject(xlAppSource);
releaseObject(xlWorkSheetDestiny);
releaseObject(xlWorkBookDestiny);
releaseObject(xlAppDestiny);
}
/*
| Devuelve un arreglo con los valores de las celdas
| que corresponden a las filas de la orden generada por copy()
| Se lee a partir del archivo generado en temp
| @param string order Es el mismo valor que temp en copy()
| @return array values Los valores de las celdas para generar el CSV
*/
public static void getOrderRows(string order, string filePath)
{
//Abre el archivo en temp y selecciona la hoja 2
Excel.Application xlApp = new Excel.ApplicationClass();
Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(order,0,true,5,null,null,false,Excel.XlPlatform.xlWindows,null,true,false,0,true,false,false);
Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
Excel.Range range = xlWorkSheet.UsedRange;
int value = 0;
string str;
int num_rows = 1;
int j=1;
ArrayList records = new ArrayList(); //guarda todos los registros validos
string[] record = new string[21]; //guarda un registro valido
//No cuenta los titulos de la columna
for (num_rows = 2; num_rows <= range.Rows.Count; num_rows++)
{
//value = (int)(range.Cells[num_rows, 7] as Excel.Range).Value2;
str = (string)(range.Cells[num_rows, 16] as Excel.Range).Text;
try
{
//ENCUENTRA CEROS
//value = int.Parse(str, NumberStyles.Number);
value = Convert.ToInt32(str,10);
//value = System.Int32.Parse(str);
//Console.WriteLine("Success " + value +" en row = "+num_rows);
}
catch(System.Exception e){
//ENCUENTRA LAS CELDAS Y #¡REF!
if (str.CompareTo("#¡REF!") != 0)
{
//CELDA ENCONTRADA
for (j = 0; j < 21; j++)
{
record[j] = "";
record[j] = (string)(range.Cells[num_rows, j+1] as Excel.Range).Text;
//Console.WriteLine("j = " + j + " - " + record[j] + " \n");
}
//Console.ReadKey();
//records.Add(record);
writeOrder(filePath,record);
}
}
}
xlWorkBook.Close(false, null, null);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
//return records;
}
public static void createFile(string filePath)
{
TextWriter file = new StreamWriter(filePath);
file.Close();
}
public static void writeOrder(string filePath,string[] values)
{
StreamWriter file;
file = File.AppendText(filePath);
for (int i = 0; i < values.Length; i++)
{
if (i == values.Length - 1)
{
file.Write(values[i]);
}
else
{
file.Write(values[i]+";");
}
}
file.WriteLine("\n");
file.Close();
}
public static void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
Console.WriteLine("Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
}
}