C# 用C填充excel工作表#

C# 用C填充excel工作表#,c#,excel,data-structures,C#,Excel,Data Structures,我的程序结构出了问题。每次按下windows窗体应用程序中的按钮时,Excel工作表都应使用数据更新单元格 这是我的密码: using System; using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Th

我的程序结构出了问题。每次按下windows窗体应用程序中的按钮时,Excel工作表都应使用数据更新单元格

这是我的密码:

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace Test6attendance
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {


        Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;

        if (File.Exists("D:\\login.xlscsharp-Excel.xls"))
        {

            object misValue = System.Reflection.Missing.Value;
            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Open("D:\\login.xlscsharp-Excel.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);
            for (int i = 1; i < 55555; i++)
            {


                if (xlWorkSheet.Cells[i, 1] == null)
                {

                    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                    xlWorkSheet.Cells[i, 1] = DateTime.Now.ToString("MM/dd/ yyyy, hh:mm:ss tt");
                    xlWorkBook.SaveAs("D:\\login.xlscsharp-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 updated , you can find the file D:\\csharp-Excel.xls");

                }

            }



        }
        else
        {

            //Create New Code



            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] = DateTime.Now.ToString("MM/dd/ yyyy, hh:mm:ss tt");


            xlWorkBook.SaveAs("D:\\login.xlscsharp-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 D:\\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();
        }
    }

    private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
    {

    }
    }


}
使用系统;
使用System.IO;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用Excel=Microsoft.Office.Interop.Excel;
名称空间Test6注意
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
Excel.applicationxlapp;
Excel.工作簿;
Excel工作表;
如果(文件.Exists(“D:\\login.xlscsharp Excel.xls”))
{
对象错误值=System.Reflection.Missing.Value;
xlApp=new Excel.Application();
xlWorkBook=xlApp.Workbooks.Open(“D:\\login.xlscsharp Excel.xls”,0,true,5,”,“”,true,Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,“\t”,false,false,0,true,1,0);
xlWorkSheet=(Excel.Worksheet)xlWorkBook.Worksheets.get_项(1);
对于(int i=1;i<55555;i++)
{
if(xlsheet.Cells[i,1]==null)
{
xlWorkSheet=(Excel.Worksheet)xlWorkBook.Worksheets.get_项(1);
xlWorkSheet.Cells[i,1]=DateTime.Now.ToString(“MM/dd/yyyy,hh:MM:ss tt”);
xlWorkBook.SaveAs(“D:\\login.xlscsharp Excel.xls”,Excel.XlFileFormat.xlWorkbookNormal,misValue,misValue,misValue,misValue,Excel.XlSaveAsAccessMode.xlExclusive,misValue,misValue,misValue,misValue);
xlWorkBook.Close(true、misValue、misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
释放对象(xlApp);
MessageBox.Show(“Excel文件已更新,您可以找到文件D:\\csharp Excel.xls”);
}
}
}
其他的
{
//创建新代码
对象错误值=System.Reflection.Missing.Value;
xlApp=new Excel.Application();
xlWorkBook=xlApp.Workbooks.Add(错误值);
xlWorkSheet=(Excel.Worksheet)xlWorkBook.Worksheets.get_项(1);
xlWorkSheet.Cells[1,1]=DateTime.Now.ToString(“MM/dd/yyyy,hh:MM:ss tt”);
xlWorkBook.SaveAs(“D:\\login.xlscsharp Excel.xls”,Excel.XlFileFormat.xlWorkbookNormal,misValue,misValue,misValue,misValue,Excel.XlSaveAsAccessMode.xlExclusive,misValue,misValue,misValue,misValue);
xlWorkBook.Close(true、misValue、misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
释放对象(xlApp);
MessageBox.Show(“创建了Excel文件,您可以找到文件D:\\csharp Excel.xls”);
}
}
私有无效释放对象(对象obj)
{
尝试
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj=null;
}
捕获(例外情况除外)
{
obj=null;
Show(“释放对象时发生异常”+ex.ToString());
}
最后
{
GC.Collect();
}
}
私有无效dateTimePicker1\u值已更改(对象发送方,事件参数e)
{
}
}
}
我相信问题在于for循环,它似乎不起作用。需要一些建议或参考来修复它。

问题在于

if (xlWorkSheet.Cells[i, 1] == null)
应该是

 if (xlWorkSheet.Cells[i, 1] == null)
由于第一个if条件检查文件是否存在
if(file.Exists(“D:\\login.xlscsharp Excel.xls”)
,如果不存在,则创建一个文件并将值分配给单元格[1,1]。现在,当您第二次单击时,单元格不为null,并且您的值不会得到更新

试试这个代码

for (int i = 1; i < 55555; i++)
            {


                if (xlWorkSheet.Cells[i, 1] != null)
                {

                    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                    xlWorkSheet.Cells[i, 1] = DateTime.Now.ToString("MM/dd/ yyyy, hh:mm:ss tt");
                    xlWorkBook.SaveAs("D:\\login.xlscsharp-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 updated , you can find the file D:\\csharp-Excel.xls");
                    break;

                }

            }
for(int i=1;i<55555;i++)
{
if(xlsheet.Cells[i,1]!=null)
{
xlWorkSheet=(Excel.Worksheet)xlWorkBook.Worksheets.get_项(1);
xlWorkSheet.Cells[i,1]=DateTime.Now.ToString(“MM/dd/yyyy,hh:MM:ss tt”);
xlWorkBook.SaveAs(“D:\\login.xlscsharp Excel.xls”,Excel.XlFileFormat.xlWorkbookNormal,misValue,misValue,misValue,misValue,Excel.XlSaveAsAccessMode.xlExclusive,misValue,misValue,misValue,misValue);
xlWorkBook.Close(true、misValue、misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
释放对象(xlApp);
MessageBox.Show(“Excel文件已更新,您可以找到文件D:\\csharp Excel.xls”);
打破
}
}

您的循环非常奇怪。您应该将所有代码放在循环之外的循环中。唯一应该留在里面的是赋值的那一行。否则,您将为保存的每一行保存应用程序。更不用说在指定第一个值后释放所有对象。我猜这只能写入一个值,然后在for循环的第一行引发许多异常(您试图从已发布的工作簿对象获取工作表)

应该是这样的:

for (int i = 1; i < 55555; i++)
{
   if (xlWorkSheet.Cells[i, 1] != null)
   {
       xlWorkSheet.Cells[i, 1] = DateTime.Now.ToString("MM/dd/ yyyy, hh:mm:ss tt"); 
   }
}
xlWorkBook.Save("D:\\login.xlscsharp-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 updated , you can find the file D:\\csharp-Excel.xls");
for(int i=1;i<55555;i++)
{
if(xlsheet.Cells[i,1]!=null)
{
xlWorkSheet.Cells[i,1]=DateTime.Now.ToString(“MM/dd/yyyy,hh:MM:ss tt”);
}
}
xlWorkBook.Save(“D:\\login.xlscsharp Excel.xls”,Excel.XlFileFormat.xlWorkbookNormal,misValue,misValue,misValue,misValue,Excel.XlSaveAsAccessMode.xlExclusive,misValue,misValue,misValue,misValue);
xlWorkBook.Close(真,值错误