简体   繁体   English

如何连续写入Excel文件?

[英]How can I write to an excel file continuously?

I would like to create and write continuosly to an Excel file. 我想创建并连续写入Excel文件。 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: 要附加文件,请使用以下代码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. 是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. Excel文件是二进制文件,因此连续写入Excel文件实际上没有任何意义。

The best solution I can suggest is to create a CSV file, and continuously write to that instead. 我建议的最佳解决方案是创建一个CSV文件,然后连续写入该文件。 Unlike Excel files, CSV files can be treated as simple text streams and can be opened natively by Excel. 与Excel文件不同,CSV文件可以被视为简单的文本流,并且可以由Excel本机打开。

btw - i know that your MessageBox.Show("Exception...") is prolly for debugging purposes. 顺便说一句-我知道您的MessageBox.Show(“ Exception ...”)完全用于调试目的。 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. 除此之外,我很想问为什么要使用excel文件而不是其他可伸缩存储介质。 you could after all, save to a db, query it when required and present an excel file with the filtered content. 毕竟,您可以保存到数据库,在需要时查询它,并提供带有过滤内容的excel文件。 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{}. 这样,您就不会冒超出excel行最大值的风险,除了常规的releaseObject()try {} / catch {}以外,您的错误处理都无法满足。

just my scottish belt and braces attitude :) 只是我的苏格兰腰带和牙套的态度:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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