如何在不传递文件名的情况下创建新的Excel文件并使用C#复制某些数据 [英] How Do I create a New Excel File without passing file name And Copy Some Data Using C#

查看:88
本文介绍了如何在不传递文件名的情况下创建新的Excel文件并使用C#复制某些数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想创建一个新的excel文件而不使用任何文件名,并使用c#复制excel文件中的一些数据...请帮助我这个。



请帮助我真的需要它



谢谢



I want to create a new excel file without using any file name and copy some data in excel file using c# ...pls help me on this.

pls help i realy need it

thank you

推荐答案

您无法在没有文件路径的情况下打开新的Excel文件,但是您可以创建新的Excel文件并将数据写入其中。两者都有所不同。另请注意,您始终需要提供打开文件的文件路径才能正常工作。



本文可能会帮助您使用Excel使用C#,使用C#使用Excel [ ^ ]
You cannot Open a new Excel file without file path, however you can create a new Excel file and write the data to it. There is a difference in both. Also note that you will always require to provide file path to open the file for working.

This article might help you to work with Excel using C#, Working with Excel Using C#[^]


private void button1_Click(object sender, EventArgs e)
        {
            Excel.Application xlApp ;
            Excel.Workbook xlWorkBook ;
            Excel.Worksheet xlWorkSheet ;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Open("csharp.net-informations.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            MessageBox.Show(xlWorkSheet.get_Range("A1","A1").Value2.ToString());

            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

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

        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Unable to release the Object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        } 


这篇关于如何在不传递文件名的情况下创建新的Excel文件并使用C#复制某些数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆