如何连续写入Excel文件?

发布于 2025-01-05 01:00:24 字数 1694 浏览 4 评论 0原文

我想创建并连续写入 Excel 文件。我必须在下面编写代码,但我想添加我连续获得的所有数据。

代码:

        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();
        }
    }

在这段代码中,当启动应用程序时,它总是说你想替换它,并且当程序获取数据时它总是关闭应用程序。

这是我想要的功能:我想像这里一样附加文件。

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

对于这种问题,有什么办法可以解决呢? 谢谢...

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苯莒 2025-01-12 01:00:24

Excel 文件是二进制的,因此连续写入 Excel 文件实际上没有意义。

我建议的最佳解决方案是创建一个 CSV 文件,然后连续写入该文件。与 Excel 文件不同,CSV 文件可以被视为简单的文本流,并且可以由 Excel 本机打开。

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.

静谧 2025-01-12 01:00:24

顺便说一句 - 我知道您的 MessageBox.Show("Exception...") 可能用于调试目的。确保不要将其保留在最终应用程序中,而是考虑某种附加“重要性”枚举的日志记录功能。

除此之外,我很想问为什么使用 Excel 文件而不是其他可扩展存储介质。毕竟,您可以保存到数据库,在需要时查询它并提供包含过滤内容的 Excel 文件。这样,您就不会面临超出 Excel 行最大值的风险,除了一般的 releaseObject() try{}/catch{} 之外,错误处理中没有考虑到这一点。

只是我苏格兰腰带和牙套的态度:)

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 :)

趁年轻赶紧闹 2025-01-12 01:00:24

要附加文件,请使用以下代码:

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

否则,您将不会向文件写入任何内容,因为您在写入文件之前将其关闭。这将以正确的方式附加文件并关闭流。

此处是 linq to sql 教程的链接。这可能会帮助您找到保存和加载数据的最佳解决方案

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文