Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88054

How can i read a range on csv file on c# [closed]

$
0
0

I'm making an app to order data generated by a logo8. The logo8 generate a csv file like this file generated by logo8 The logo8 create a new file when he generate 20000 rows of data.

So the application have to create an Excel file where it would be all the data ordered by date, the expect result have to look like this

Expected result

My code is this:

  private void Button1_Click(object sender, EventArgs e)
    {

        if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            ruta = folderBrowserDialog1.SelectedPath;
        arreglado = new Excel("Informe_general", hojas);
        DirectoryInfo dir = new DirectoryInfo(@ruta);
        FileInfo[] f = dir.GetFiles();
        foreach (var item in f)  
            openfile(item.FullName);

        MessageBox.Show("Se termino");


    }
    int ccc=0;
    Excel excel=new Excel();
    public void openfile(string ruta)
    {

            excel = new Excel(@ruta, 1);

        vitacora("Se abrio " + @ruta + " con exito");
            leerdatos();




    }

    private void Button2_Click(object sender, EventArgs e)
    {


        leerdatos();
    }
    string[,] todo;

    public bool arreglardatos(string[,] todo)
    {
        try
        {

            for (int i = 1; i < 19999; i++)
            {

                double d = Convert.ToDouble(todo[i, 0]);
                DateTime a = DateTime.FromOADate(d);
                todo[i, 0] = a.ToString();

            }
            DateTime s = Convert.ToDateTime(todo[1, 0]);
            DateTime s2 = Convert.ToDateTime(todo[19998, 0]);

            if (s.ToShortDateString() != s2.ToShortDateString())
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        catch
        {
            return false;
        }


    }
    int cambiodefecha = 0;
    public int busquedabinaria(int f1, int f2)
    {
        bool seguir = true;


        for (int i = 1; i < 19999 && seguir; i++)
        {

            DateTime a = Convert.ToDateTime(todo[i, 0]);
            DateTime b = Convert.ToDateTime(todo[i+1, 0]);
            if (a.Day == f1 && b.Day == f2)
            {
                cambiodefecha = i;
                seguir = false;
            }
        }

        return cambiodefecha+1;
    }





    public void leerdatos()
    {
        bool valido = true;
        todo= excel.Readrange(1, 1, excel.getRowCount(), excel.getcolumncount());
        foreach (var item in todo)
        {
            if (item == "error")
                valido = false;
        }
        vitacora("Se leyeron los datos de " + ruta);

        if (valido)
        {
            if (!arreglardatos(todo))
            {
                vitacora("Corrigiendo campos");
                escribirdatos(false);

            }
            else
            {
                DateTime a = Convert.ToDateTime(todo[1, 0]);
                DateTime b = Convert.ToDateTime(todo[19998, 0]);

                vitacora("cambiode fecha en " + busquedabinaria(a.Day, b.Day));
                escribirdatos(true);
                cambiodefecha = 0;

            }

            ccc++;
        }
        else
        {
            vitacora("El archivo " + ruta + " no es valido");
            ccc++;
        }
    }
    public void vitacora(string d)
    {
        listBox1.Items.Add(d);
    }


    public void escribirdatos(bool cambiofecha)
    {



        if (!cambiofecha)
        {

            arreglado.Writerange(2, 2, 21000,78, todo);
            DateTime a = Convert.ToDateTime(todo[1, 0]);
            arreglado.setname(a.ToShortDateString()+"("+hojas+")");
            vitacora("Se transfirieron los datos a Informe general.xlsx");


        }
        else
        {


            arreglado.Writerange(2, 2, cambiodefecha + 1, 78, todo);
            DateTime b = Convert.ToDateTime(todo[1, 0]);
            arreglado.setname(b.ToShortDateString() + " ("+ hojas +")");
            arreglado.createnewsheet();
            arreglado.Writerange(2, 2, 20000-cambiodefecha, 78, todo);
            DateTime a = Convert.ToDateTime(todo[cambiodefecha+9, 0]);
            hojas++;
            arreglado.setname(a.ToShortDateString() + " (" + hojas + ")");
            vitacora("Se transfirieron los datos a Informe general.xlsx");
        }

        arreglado.createnewsheet();
        hojas++;
        arreglado.save();


    }

and my class excel is this:

public class Excel
{
    string path = "";
    _Application excel = new _Excel.Application();


    Workbook wb;
    Worksheet ws;



    public Excel(string path,int sheet)
    {
        try
        {

            this.path = path;
            wb = excel.Workbooks.Open(Filename: path);
            ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[sheet];


        }
        catch
        {
            wb.Close();
        }

    }

    public Excel()
    {

    }

    public int getsheetscount()
    {
        return wb.Sheets.Count;
    }
    public string[] getsheetsname()
    {

        string[] nombres = new string[wb.Sheets.Count];
        for (int i = 1; i < wb.Sheets.Count; i++)
        {
            nombres[i - 1] = wb.Worksheets[i].Name;
        }
        return nombres;
    }


       public void createnewsheet()
    {
        Worksheet tempSheet = wb.Worksheets.Add(After: ws);
        ws = tempSheet;


    }


    public async void Writerange(int starti, int starty, int endi, int endy, string[,] datos)
    {

        Range range = (Range)ws.Range[ws.Cells[starti, starty], ws.Cells[endi, endy]];
         range.Value2 = datos;            
    }



    public void focussheet(int t)
    {
       ws= wb.Worksheets[t];
    }
    public void setname(string d)
    {
        d = d.Replace('/', '-');
        ws.Name = d;

    }
    public string[,] Readrange(int starti,int starty,int endi,int endy)
    {

            Range range = (Range)ws.Range[ws.Cells[starti, starty], ws.Cells[endi, endy]];
            object[,] holder = range.Value2;

             string[,] returnstring = new string[endi - starti, endy - starty];
             for (int p = 1; p <= endi - starti; p++)
             {
                 for (int q = 1; q <= endy - starty; q++)
                 {
                     returnstring[p - 1, q - 1] = holder[p, q].ToString();
                 }

             }
             return returnstring;
         }




    public void createnewfile()
    {

        this.wb = excel.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
        this.ws = wb.Worksheets[1];





    }


    public void save()
    {
        wb.Save();

    }
    public void saveas(string path)
    {
        object misValue = System.Reflection.Missing.Value;


        wb.SaveAs(path);

    }
    public int getcolumncount()
    {
        return ws.UsedRange.Columns.Count;
    }
    public int getRowCount()
    {
        int a=Convert.ToInt32( ws.UsedRange.Rows.Count);
        return a;
    }
    public void close()
    {
        wb.Close(true);

    }

Viewing all articles
Browse latest Browse all 88054

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>