简体   繁体   中英

How can I write to an excel file continuously?

I would like to create and write continuosly to an Excel file. I have to code below, but I want to add all the datas which I get continuously.

CODE:

        Excel.Application xlApp ;
        Excel.Workbook xlWorkBook ;
        Excel.Worksheet xlWorkSheet ;
        object misValue = System.Reflection.Missing.Value;

        xlApp = new Excel.Application();
        xlWorkBook = xlApp.Workbooks.Add(misValue);

        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
        xlWorkSheet.Cells[1, 1] = "http://csharp.net-informations.com";

        xlWorkBook.SaveAs("csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
        xlWorkBook.Close(true, misValue, misValue);
        //xlApp.Quit();

        releaseObject(xlWorkSheet);
        releaseObject(xlWorkBook);
        releaseObject(xlApp);

        MessageBox.Show("Excel file created , you can find the file c:\\csharp-Excel.xls");
    }

    private void releaseObject(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            obj = null;
        }
        catch (Exception ex)
        {
            obj = null;
            MessageBox.Show("Exception Occured while releasing object " +     ex.ToString());
        }
        finally
        {
            GC.Collect();
        }
    }

In this code, when start the application, it always says that do you wanna replace it and it always closes the application when the program gets the data.

This is the functionality which I want to have: I want to append the file like here.

    StreamWriter fileWriter = new StreamWriter("test.txt", true);
    fileWriter.WriteLine(jointHead.Position.X);       
    fileWriter.Close();

What can be the solution for this kind of problem? Thank you...

To append the file use this code insead:

using(StreamWriter fileWriter = new StreamWriter(@"c:\test.txt", true))
{
    fileWriter.WriteLine("test");
}

Otherwise you will not write anything to the file because you close it before it has written to it. This will append the file and close the stream in the right way.

Here is a link to a tutorial on linq to sql. This might help you find a god solution for save and loading data

Excel files are binary, so writing continuously to an Excel file doesn't really make sense.

The best solution I can suggest is to create a CSV file, and continuously write to that instead. Unlike Excel files, CSV files can be treated as simple text streams and can be opened natively by Excel.

btw - i know that your MessageBox.Show("Exception...") is prolly for debugging purposes. make sure not to leave that in place in your final app and instead think of some kind of logging functionality with an 'importance' enum attached.

other than that, i would be tempted to ask why the excel file is being used rather than other scalable storage mediums. you could after all, save to a db, query it when required and present an excel file with the filtered content. that way, you don't run the risk of exceeding the excel row maximum, which isn't catered for in your error handling, other than in the general releaseObject() try{}/catch{}.

just my scottish belt and braces attitude :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM