C#打开新的Excel工作簿并添加数据

C#打开新的Excel工作簿并添加数据,c#,excel,C#,Excel,从使用C#的控制台应用程序中,我试图打开一个新的Excel工作簿并向其中添加数据。我可以很好地打开一个新工作簿,但在向工作簿添加数据时遇到了问题,因为我的范围对象为空,而且我似乎无法连接到刚刚打开的excel工作簿。我尝试了ActiveWorkbook,Sheets[1]和一些其他的变体,但我似乎无法理解 using Microsoft.Office.Interop.Excel; using System.Reflection; namespace ConsoleApplication1 {

从使用C#的控制台应用程序中,我试图打开一个新的Excel工作簿并向其中添加数据。我可以很好地打开一个新工作簿,但在向工作簿添加数据时遇到了问题,因为我的范围对象为空,而且我似乎无法连接到刚刚打开的excel工作簿。我尝试了
ActiveWorkbook
Sheets[1]
和一些其他的变体,但我似乎无法理解

using Microsoft.Office.Interop.Excel;
using System.Reflection;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Application xl = null;
            _Workbook wb = null;

            // Option 1
            xl = new Application();
            xl.Visible = true;
            wb = (_Workbook)(xl.Workbooks.Add(XlWBATemplate.xlWBATWorksheet));

            Worksheet sheet = xl.ActiveWorkbook.ActiveSheet;
            Range cell = sheet.Cells[1, 1];
            //ERROR on cell.Value("Test");
            cell.Value("Test");    
        }
    }
}

此代码可能对您有所帮助

using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "Excel Workbook|*.xls" })
            {
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    // creating Excel Application  
                    Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
                    // creating new WorkBook within Excel application  
                    Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
                    // creating new Excelsheet in workbook  
                    Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
                    // see the excel sheet behind the program  
                    app.Visible = true;
                    // get the reference of first sheet. By default its name is Sheet1.  
                    // store its reference to worksheet  
                    worksheet = workbook.Sheets["Sheet1"];
                    worksheet = workbook.ActiveSheet;
                    // changing the name of active sheet  
                    worksheet.Name = "Exported from gridview";
                    // storing header part in Excel  
                    for (int i = 1; i < DGView.Columns.Count + 1; i++)
                    {
                        worksheet.Cells[1, i] = DGView.Columns[i - 1].HeaderText;
                    }
                    // storing Each row and column value to excel sheet  
                    for (int i = 0; i < DGView.Rows.Count - 1; i++)
                    {
                        for (int j = 0; j < DGView.Columns.Count; j++)
                        {
                            worksheet.Cells[i + 2, j + 1] = DGView.Rows[i].Cells[j].Value.ToString();
                        }
                    }
                    // save the application  
                    workbook.SaveAs(sfd.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                    // Exit from the application  
                    app.Quit();
                }
            }
使用(SaveFileDialog sfd=new SaveFileDialog(){Filter=“Excel工作簿|*.xls”})
{
if(sfd.ShowDialog()==DialogResult.OK)
{
//创建Excel应用程序
Microsoft.Office.Interop.Excel.\u应用程序app=新的Microsoft.Office.Interop.Excel.Application();
//在Excel应用程序中创建新工作簿
Microsoft.Office.Interop.Excel.\u工作簿=app.Workbooks.Add(Type.Missing);
//在工作簿中创建新的Excel工作表
Microsoft.Office.Interop.Excel.\u工作表=null;
//请参见程序后面的excel表
app.Visible=true;
//获取第一张图纸的引用。默认情况下,其名称为Sheet1。
//将其引用存储到工作表
工作表=工作簿。工作表[“Sheet1”];
工作表=工作簿.ActiveSheet;
//更改活动图纸的名称
工作表.Name=“从gridview导出”;
//在Excel中存储标题部分
对于(int i=1;i

我在工作中使用此代码将datagridview保存为excel工作表,效果良好。

此处MS论坛链接编辑:我的错误,它没有得到准确回答。请尝试工作表。单元格[1,1]=“测试”@尼莫:谢谢